Pandas数据帧:使用python

时间:2016-08-10 00:49:12

标签: pandas dataframe python-3.5

我有一个数据帧df如下,我想删除或删除名称为tom的行,我使用以下代码(python3):

df1[~df1['name'].str.contains('tom')]

但有错误:

AttributeError: Cannot access attribute 'str' of 'SeriesGroupBy'   
     objects, try using the 'apply' method


name    age weight 
tom     10   40
lucy    15   50
john    20   60
tom     10   40
lucy    15   50
john    20   60
tom     10   40
kate    30   70
tick    40   75
bruce   50   75

请帮我解决一下,这只是示例数据,因为实际的数据帧很大。如果你们有快速解决方案,请告诉我。提前谢谢!

1 个答案:

答案 0 :(得分:2)

试试这个:

df[df["name"] != 'tom']

 or 

df[~df['name'].str.contains('tom')]

To remove on multiple criteria  -- "~" is return opposite of True/False

df2[~(df2["name"].isin(['tom','lucy']))]