![](/img/trans.png)
[英]deleting rows having a particular date in the datetime column of dataframe in pandas
[英]Deleting/dropping rows in pandas DataFrame with particular string in ANY column
可能是一个简单的答案,所以提前道歉(最少的编码经验)。
我试图从任何列中删除具有特定字符串(经济 7)的任何行,并且一直试图离开这个线程:
如何从包含特定列中的特定字符串的 Pandas 数据框中删除行?
无法让它工作,但在以前的数据帧(现在是 df = energy)上尝试了这段代码,它似乎可以工作,尽管现在出现了一个错误:
no_eco = energy[~energy.apply(lambda series: series.str.contains('Economy 7')).any(axis=1)]
AttributeError: ('Can only use .str accessor with string values, which use np.object_ dtype in pandas', 'occurred at index existingProductCodeGas')
有什么建议? ps DataFrame 非常大。
谢谢
您只能通过select_dtypes
选择对象列,显然是字符串:
df = energy.select_dtypes(object)
#added regex=False for improve performance like mentioned @jpp, thank you
mask = ~df.apply(lambda series: series.str.contains('Economy 7', regex=False)).any(axis=1)
no_eco = energy[mask]
样品:
energy = pd.DataFrame({
'A':list('abcdef'),
'B':[4,5,4,5,5,4],
'C':[7,8,9,4,2,3],
'D':[1,3,5,7,1,0],
'E':[5,3,6,9,2,4],
'F':list('adabbb')
})
print (energy)
A B C D E F
0 a 4 7 1 5 a
1 b 5 8 3 3 d
2 c 4 9 5 6 a
3 d 5 4 7 9 b
4 e 5 2 1 2 b
5 f 4 3 0 4 b
df = energy.select_dtypes(object)
mask = ~df.apply(lambda series: series.str.contains('d')).any(axis=1)
no_eco = energy[mask]
print (no_eco)
A B C D E F
0 a 4 7 1 5 a
2 c 4 9 5 6 a
4 e 5 2 1 2 b
5 f 4 3 0 4 b
如果任何列包含特定字符串,我们可以使用 to_string 方法删除行
df.drop(df[df.apply(lambda row: 'Tony' in row.to_string(header=False), axis=1)].index, inplace=True)
完整的例子是
import pandas as pd
df = pd.DataFrame(columns = ['Name', 'Location'])
df.loc[len(df)] = ['Mathew', 'Houston']
df.loc[len(df)] = ['Tony', 'New York']
df.loc[len(df)] = ['Jerom', 'Los Angeles']
df.loc[len(df)] = ['Aby', 'Dallas']
df.loc[len(df)] = ['Elma', 'Memphis']
df.loc[len(df)] = ['Zack', 'Chicago']
df.loc[len(df)] = ['Lisa', 'New Orleans']
df.loc[len(df)] = ['Nita', 'Las Vegas']
df.drop(df[df.apply(lambda row: 'Tony' in row.to_string(header=False), axis=1)].index, inplace=True)
print(df)
输出:
Name Location
0 Mathew Houston
2 Jerom Los Angeles
3 Aby Dallas
4 Elma Memphis
5 Zack Chicago
6 Lisa New Orleans
7 Nita Las Vegas
[Finished in 1.4s]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.