简体   繁体   English

基于python中的多列删除具有多个条件的行

[英]drop rows with multiple conditions based on multiple column in python

I have a dataset (df) as below:我有一个数据集(df)如下:

在此处输入图片说明

I want to drop rows based on condition when SKU is "abc" and packing is "1KG" & "5KG".当 SKU 为“abc”且包装为“1KG”和“5KG”时,我想根据条件删除行。

I have tried using following code:我尝试使用以下代码:

df.drop( df[ (df['SKU'] == "abc") & (df['Packing'] == "10KG") & (df['Packing'] == "5KG") ].index, inplace=True)

Getting following error while trying above code:尝试上述代码时出现以下错误:

NameError                                 Traceback (most recent call last)
<ipython-input-1-fb4743b43158> in <module>
----> 1 df.drop( df[ (df['SKU'] == "abc") & (df['Packing'] == "10KG") & (df['Packing'] == "5KG") ].index, inplace=True)

NameError: name 'df' is not defined

Any help on this will be greatly appreciated.对此的任何帮助将不胜感激。 Thanks.谢谢。

I suggest trying this:我建议试试这个:

df = df.loc[~((df['SKU'] == 'abc') & (df['packing'].isin(['1KG', '5KG']))]

The .loc is to help define the conditions and using ~ basically means 'NOT' those conditions. .loc 用于帮助定义条件,使用 ~ 基本上意味着“不是”这些条件。

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

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