简体   繁体   English

根据列中的多个条件删除行(使用 Python)

[英]Delete rows based on multiple conditions within a column (using Python)

I have a dataset, df, where I wish to remove the rows from if the column contains a certain value(s)我有一个数据集 df,如果列包含某个值,我希望从中删除行

  value   pod

  1       hi
  2       ok
  3       no
  4       sure

Desired output:期望的输出:

  value   pod

  2       ok
  4       sure

I wish to remove the row if the pod column contains the word 'hi' or the word 'no'如果 pod 列包含单词“hi”或单词“no”,我希望删除该行

This is what I am doing这就是我正在做的

df1 = df.drop(df.index[df['pod'] == 'hi', 'no'], inplace = True)

I keep getting this error:我不断收到此错误:

A value is trying to be set on a copy of a slice from a DataFrame试图在来自 DataFrame 的切片副本上设置值

I am still researching this, any suggestion is appreciated我仍在研究这个,任何建议表示赞赏

I believe you should use isin :我相信你应该使用isin

df1 = df[~df['pod'].isin(['hi','no']) ]

print(df1)

Output:输出:

   value   pod
1      2    ok
3      4  sure

On the other note, look at the following:另一方面,请查看以下内容:

df.drop(df.index[df['pod'] == 'hi', 'no'], inplace = True)

if it were to work, inplace=True forces the drop command to work inplace , and return None .如果要工作, inplace=True强制drop命令就地工作,并返回None So所以

df1 = df.drop(df.index[df['pod'] == 'hi', 'no'], inplace = True)

would mean that df1 is None , not a dataframe.将意味着df1None ,而不是数据帧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM