pandas 如何检查DataFrame单元格中是否存在字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39299703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if character exists in DataFrame cell
提问by alphanumeric
After creating the three-rows DataFrame:
创建三行 DataFrame 后:
import pandas as pd
df = pd.DataFrame({'a': ['1-2', '3-4', '5-6']})
I check if there is any cell equal to '3-4':
我检查是否有任何等于“3-4”的单元格:
df['a']=='3-4'
Since df['a']=='3-4'
command results to pandas.core.series.Series
object I can use it to create a "filtered" version of the original DataFrame like so:
由于df['a']=='3-4'
命令结果到pandas.core.series.Series
对象,我可以使用它来创建原始 DataFrame 的“过滤”版本,如下所示:
filtered = df[ df['a']=='3-4' ]
In Python I can check for the occurrence of the string character in another string using:
在 Python 中,我可以使用以下方法检查另一个字符串中字符串字符的出现:
string_value = '3-4'
print('-' in string_value)
What would be a way to accomplish the same while working with DataFrames?
在使用 DataFrames 时,有什么方法可以完成相同的任务?
So, I could create the filtered version of the original DataFrame by checking if '-' character in every row's cell, like:
因此,我可以通过检查每一行单元格中是否有“-”字符来创建原始 DataFrame 的过滤版本,例如:
filtered = df['-' in df['a']]
But this syntax above is invalid and throws KeyError: False
error message.
但是上面的这个语法是无效的并且会抛出KeyError: False
错误信息。
回答by juanpa.arrivillaga
Use str
and contains
:
使用str
和contains
:
In [5]: df['a'].str.contains('-')
Out[5]:
0 True
1 True
2 True
Name: a, dtype: bool
回答by Oren
This is how to do it using query:
这是使用查询的方法:
In [4]: df.query('a.str.contains("1")')
Out[4]:
a
0 1-2
In [5]: df.query('a.str.contains("-")')
Out[5]:
a
0 1-2
1 3-4
2 5-6