如何检查DataFrame单元格中是否存在字符 [英] How to check if character exists in DataFrame cell

查看:1816
本文介绍了如何检查DataFrame单元格中是否存在字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建三行数据框后:

import pandas as pd
df = pd.DataFrame({'a': ['1-2', '3-4', '5-6']})

我检查是否存在等于"3-4"的单元格:

I check if there is any cell equal to '3-4':

df['a']=='3-4'

由于df['a']=='3-4'命令的结果是pandas.core.series.Series对象,因此可以使用它来创建原始DataFrame的过滤"版本,如下所示:

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:

filtered = df[ df['a']=='3-4' ]

在Python中,我可以使用以下命令检查另一个字符串中是否存在字符串字符:

In Python I can check for the occurrence of the string character in another string using:

string_value = '3-4'
print('-' in string_value)

在使用DataFrames时实现相同目的的方法是什么?

What would be a way to accomplish the same while working with DataFrames?

因此,我可以通过以下方式创建原始DataFrame的过滤后的版本: 检查每行单元格中是否有-"字符,例如:

So, I could create the filtered version of the original DataFrame by checking if '-' character in every row's cell, like:

filtered = df['-' in df['a']]

但是上面的语法无效,并抛出KeyError: False错误消息.

But this syntax above is invalid and throws KeyError: False error message.

推荐答案

使用strcontains:

In [5]: df['a'].str.contains('-')
Out[5]: 
0    True
1    True
2    True
Name: a, dtype: bool

这篇关于如何检查DataFrame单元格中是否存在字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆