Pat*_*rdo 4 python numpy python-2.7 pandas
如果数据帧的行中包含特定字符串,我必须删除它们。问题是该行很长并且包含文本。
循环不起作用,将索引放入列表中,然后在索引上使用 .drop 也不起作用。
column1
8
8
8
8 total <-------- This must be deleted
8
8
8
8
8
...
Run Code Online (Sandbox Code Playgroud)
谢谢
假设您的数据框名为df. 然后使用:
df_filtered = df[~df['column1'].str.contains('total')]
Run Code Online (Sandbox Code Playgroud)
解释:
df['column1'].str.contains('total')True将为您提供一个包含df['column1']的数据帧列长度的数组'total'。与~您交换该数组的True和False值。最后,df_filtered = df[...]您只需要线条,'total'不包括在内。