如何使用关键字从 Pandas 中的列中删除行
How to delete rows using a key word from columns in Pandas
我们如何删除整行,该行在该行的任何列中包含关键字?我有 250 个这样的行和 28 列,我想使用 pandas
从数据框中的任何列中删除所有具有 "income" 作为键字符串的行
假设您的 df 中没有其他 NA 值,这将可以将 'DELETE' 替换为 'income'
df[df.applymap(lambda x: False if 'DELETE' in str(x) else True)].dropna()
例如,您想要删除列中包含 'c' 的任何行
In [5]: import pandas as pd
In [7]: data = [['a', 'b'], ['a', 'c'], ['c', 'd']]
df = pd.DataFrame(data, columns=['col1', 'col2'])
In [9]: df
Out[9]:
col1 col2
0 a b
1 a c
2 c d
In [10]: df.loc[~(df == 'c').sum(axis=1).astype(bool)]
Out[10]:
col1 col2
0 a b
比较这个问题的不同解决方案。我首先创建了一个大型测试数据集:
In [11]: data = [['a', 'b'], ['a', 'c'], ['c', 'd']]*10000
df = pd.DataFrame(data, columns=['col1', 'col2'])
这是一个更新的解决方案,我认为目前为止最好的解决方案:
In [6]: %timeit df.loc[~(df == 'c').any(axis=1)]
100 loops, best of 3: 3.85 ms per loop
这是我的原始解决方案:
In [13]: %timeit df.loc[~(df == 'c').sum(axis=1).astype(bool)]
100 loops, best of 3: 3.92 ms per loop
这是与其他解决方案的比较:
In [14]: %timeit df[df.applymap(lambda x: False if 'c' in str(x) else True)].dropna()
10 loops, best of 3: 43 ms per loop
In [5]: %timeit df[~df.apply(lambda series: series.str.contains('c')).any(axis=1)]
10 loops, best of 3: 60.7 ms per loop
虽然原来的问题比较简单,但也说明了 lambda 可以很慢。多次调用一个函数会产生很高的开销,例如,如果数据框中有很多行。在处理大型数据帧时避免它们通常是值得的。
您可以使用 apply
和 lambda 表达式来检查每一列中的目标词。然后使用 any(axis=1)
定位包含该词的任何行。最后,使用带波浪号 (~
) 的布尔索引来定位收入不在行中的所有行。
df = pd.DataFrame({'A': ['a', 'income', 'c'], 'B': ['a', 'b', 'income'], 'C': ['a', 'b', 'c']})
>>> df
A B C
0 a a a
1 income b b
2 c income c
# A check for which cells contain the target word.
>>> df.apply(lambda series: series.str.contains('income'))
A B C
0 False False False
1 True False False
2 False True False
# Remove the offending rows.
>>> df[~df.apply(lambda series: series.str.contains('income')).any(axis=1)]
A B C
0 a a a
我们如何删除整行,该行在该行的任何列中包含关键字?我有 250 个这样的行和 28 列,我想使用 pandas
从数据框中的任何列中删除所有具有 "income" 作为键字符串的行假设您的 df 中没有其他 NA 值,这将可以将 'DELETE' 替换为 'income'
df[df.applymap(lambda x: False if 'DELETE' in str(x) else True)].dropna()
例如,您想要删除列中包含 'c' 的任何行
In [5]: import pandas as pd
In [7]: data = [['a', 'b'], ['a', 'c'], ['c', 'd']]
df = pd.DataFrame(data, columns=['col1', 'col2'])
In [9]: df
Out[9]:
col1 col2
0 a b
1 a c
2 c d
In [10]: df.loc[~(df == 'c').sum(axis=1).astype(bool)]
Out[10]:
col1 col2
0 a b
比较这个问题的不同解决方案。我首先创建了一个大型测试数据集:
In [11]: data = [['a', 'b'], ['a', 'c'], ['c', 'd']]*10000
df = pd.DataFrame(data, columns=['col1', 'col2'])
这是一个更新的解决方案,我认为目前为止最好的解决方案:
In [6]: %timeit df.loc[~(df == 'c').any(axis=1)]
100 loops, best of 3: 3.85 ms per loop
这是我的原始解决方案:
In [13]: %timeit df.loc[~(df == 'c').sum(axis=1).astype(bool)]
100 loops, best of 3: 3.92 ms per loop
这是与其他解决方案的比较:
In [14]: %timeit df[df.applymap(lambda x: False if 'c' in str(x) else True)].dropna()
10 loops, best of 3: 43 ms per loop
In [5]: %timeit df[~df.apply(lambda series: series.str.contains('c')).any(axis=1)]
10 loops, best of 3: 60.7 ms per loop
虽然原来的问题比较简单,但也说明了 lambda 可以很慢。多次调用一个函数会产生很高的开销,例如,如果数据框中有很多行。在处理大型数据帧时避免它们通常是值得的。
您可以使用 apply
和 lambda 表达式来检查每一列中的目标词。然后使用 any(axis=1)
定位包含该词的任何行。最后,使用带波浪号 (~
) 的布尔索引来定位收入不在行中的所有行。
df = pd.DataFrame({'A': ['a', 'income', 'c'], 'B': ['a', 'b', 'income'], 'C': ['a', 'b', 'c']})
>>> df
A B C
0 a a a
1 income b b
2 c income c
# A check for which cells contain the target word.
>>> df.apply(lambda series: series.str.contains('income'))
A B C
0 False False False
1 True False False
2 False True False
# Remove the offending rows.
>>> df[~df.apply(lambda series: series.str.contains('income')).any(axis=1)]
A B C
0 a a a