我想从df1中删除某些行。我确实以这种方式编写了条件,并向我显示了我想要删除的确切行。但是,当我尝试对此数据应用drop时,它不起作用:
to_be deleted = df1.loc[df1['barcode'].str.contains('....-..-....-11.', regex=True)]当我使用
to_be deleted.head()
print(len(to_be deleted))我可以看到我想要删除的数据,这意味着代码起作用了。但是,当我尝试删除这些行时,它不起作用
df2 = df1.drop([df1['barcode'].str.contains('....-..-....-11.', regex=True)], axis=1, inplace=True)我也试过了
df2 = df1.drop(to_be_deleted, axis=1, inplace=True)但它要么显示:
'Series' objects are mutable, thus they cannot be hashed或
/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py:1: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame如何删除我在(to_be_deleted) data frame中指定的这些行?
谢谢
发布于 2018-04-14 19:48:15
为此,您不需要使用pd.DataFrame.drop:
mask = df1['barcode'].str.contains('....-..-....-11.', regex=True)
df1 = df1[~mask]~运算符表示否定。因为mask是一个布尔数组,所以它被取反并用作df1上的行过滤器。
https://stackoverflow.com/questions/49831072
复制相似问题