2024-03-28 16:49:44 发布
网友
我有一个带4列的DataFrame,其中2列包含字符串值。我想知道是否有方法根据特定列的部分字符串匹配来选择行?
DataFrame
换言之,一个函数或lambda函数可以做如下的事情
re.search(pattern, cell_in_question)
返回布尔值。我熟悉df[df['A'] == "hello world"]的语法,但似乎找不到对部分字符串匹配(如'hello')执行相同操作的方法。
df[df['A'] == "hello world"]
'hello'
有人能给我指一个正确的方向吗?
基于github问题#620,看起来您很快就能执行以下操作:
df[df['A'].str.contains("hello")]
更新:vectorized string methods (i.e., Series.str)在pandas 0.8.1及更高版本中可用。
我在ipython笔记本的macos上使用pandas 0.14.1。我试过上面的建议:
df[df['A'].str.contains("Hello|Britain")]
并得到一个错误:
"cannot index with vector containing NA / NaN values"
但是当添加“==True”条件时,它工作得很好,比如:
df[df['A'].str.contains("Hello|Britain")==True]
基于github问题#620,看起来您很快就能执行以下操作:
更新:vectorized string methods (i.e., Series.str)在pandas 0.8.1及更高版本中可用。
我在ipython笔记本的macos上使用pandas 0.14.1。我试过上面的建议:
并得到一个错误:
但是当添加“==True”条件时,它工作得很好,比如:
相关问题 更多 >
编程相关推荐