简体   繁体   English

Python 等效于 excel 嵌套 if 过滤 Pandas DataFrame 行的条件

[英]Python equivalent of excel nested if condition for filtering Pandas DataFrame rows

Selecting specific excel rows using python.使用python选择特定的excel行。 So in excel I would do所以在excel中我会做

If(And(Or(A<>({"Closed",""}),Or(B<>({"Closed",""})))

For obtaining those columns in a data frame that is neither Closed or blank.用于获取既不是 Closed 也不是空白的数据框中的那些列。 Tried using尝试使用

df = df[(~df.A.isin([Closed","No Data"])) &(~df.B.isin([Closed","No Data"]))]

The problem is python is removing columns which are for example:问题是 python 正在删除列,例如:

A                        B
Approved       Closed
No Data          Restrict
Restrict           No Data

Which I don't want As suggested in one of the links also tried我不想要的正如其中一个链接中的建议也尝试过

df.loc[(df[A] != "Closed") & (df[B] != "Closed") & (df[A] != "No data") & (df[B] != "No data")

Got the same result as when I tried .isin得到了与我尝试 .isin 时相同的结果

I will use this sample data:我将使用此示例数据:

           A         B  ~df.A.isin  ~df.B.isin  ~A & ~B  ~A | ~B
0     Closed    Closed       False       False    False    False
1     Closed   No Data       False       False    False    False
2   Approved    Closed        True       False    False     True
3    No Data   No Data       False       False    False    False
4     Closed  Approved       False        True    False     True
5    No Data  Restrict       False        True    False     True
6   Approved   No Data        True       False    False     True
7     Closed  Restrict       False        True    False     True
8   Approved  Approved        True        True     True     True
9    No Data  Approved       False        True    False     True
10  Restrict   No Data        True       False    False     True
11  Restrict  Approved        True        True     True     True

~df.A.isin column shows the value of ~df.A.isin(["Closed","No Data"]) , which is True for rows where A contains neither Closed nor No Data ~df.A.isin列显示~df.A.isin(["Closed","No Data"]) ,对于 A 既不包含Closed也不包含No Data行,该值为True

~df.B.isin column shows the value of ~df.B.isin(["Closed","No Data"]) , which is True for rows where B contains neither Closed nor No Data ~df.B.isin列显示~df.B.isin(["Closed","No Data"]) ,对于 B 既不包含Closed也不包含No Data行,该值为True

~A & ~B column shows the value of (~df.A.isin(["Closed","No Data"])) &(~df.B.isin(["Closed","No Data"])) ~A & ~B列显示值(~df.A.isin(["Closed","No Data"])) &(~df.B.isin(["Closed","No Data"]))

~A | ~B ~A | ~B column shows the value of (~df.A.isin(["Closed","No Data"])) |(~df.B.isin(["Closed","No Data"])) ~A | ~B列显示(~df.A.isin(["Closed","No Data"])) |(~df.B.isin(["Closed","No Data"]))

You first attempt lacks a " at the beginning of Closed" .您第一次尝试缺少"Closed"的开头Closed" Adding it we have添加它我们有

df[(~df.A.isin(["Closed","No Data"])) &(~df.B.isin(["Closed","No Data"]))]

which gives us:这给了我们:

           A         B  ~df.A.isin  ~df.B.isin  ~A & ~B  ~A | ~B
8   Approved  Approved        True        True     True     True
11  Restrict  Approved        True        True     True     True

The result shows only those rows that are completely without Closed and without No Data .结果只显示那些完全没有ClosedNo Data

The suggestion in comments by Wen-Ben:文本评论中的建议:

df[(~df.A.isin(["Closed","No Data"])) |(~df.B.isin(["Closed","No Data"]))]

gives us:给我们:

           A         B  ~df.A.isin  ~df.B.isin  ~A & ~B  ~A | ~B
2   Approved    Closed        True       False    False     True
4     Closed  Approved       False        True    False     True
5    No Data  Restrict       False        True    False     True
6   Approved   No Data        True       False    False     True
7     Closed  Restrict       False        True    False     True
8   Approved  Approved        True        True     True     True
9    No Data  Approved       False        True    False     True
10  Restrict   No Data        True       False    False     True
11  Restrict  Approved        True        True     True     True

Here we have |我们有| ( or ) instead of & ( and ), so the rows can contain Closed or No Data , but not in both A and B. This means the rows that have: ( or ) 而不是& ( and ),因此行可以包含ClosedNo Data ,但不能同时包含在 A 和 B 中。这意味着具有以下内容的行:

       A         B
Approved    Closed
 No Data  Restrict
Restrict   No Data

will be included, but not rows that have:将包括在内,但包括具有以下内容的行:

     A         B
Closed    Closed
Closed   No Data

Your second attempt:你的第二次尝试:

df.loc[(df[A] != "Closed") & (df[B] != "Closed") &
       (df[A] != "No data") & (df[B] != "No data")

needs quotes around column labels.需要在列标签周围加上引号。 You can either use df.A or df['A'] , but not df[A]您可以使用df.Adf['A'] ,但不能使用df[A]

Also, you spelled data in No data with lowercase d , while in other places you have it with uppercase D - No Data .此外,您在No data中用小写d拼写data ,而在其他地方则用大写D - No Data拼写data In python, that's not the same.在python中,这不一样。 If we fix that:如果我们解决这个问题:

df.loc[(df['A'] != "Closed") & (df['B'] != "Closed") &
       (df['A'] != "No Data") & (df['B'] != "No Data")]

which gives us the same thing as the first attempt:这给了我们与第一次尝试相同的东西:

           A         B  ~df.A.isin  ~df.B.isin  ~A & ~B  ~A | ~B
8   Approved  Approved  True  True  True  True  True  True  True
11  Restrict  Approved  True  True  True  True  True  True  True

If you rearrange this expression a little, use parentheses and |如果您稍微重新排列此表达式,请使用括号和| ( or ): or ):

df.loc[((df['A'] != "Closed") & (df['A'] != "No Data")) | 
       ((df['B'] != "Closed") & (df['B'] != "No Data"))]

we get:我们得到:

           A         B  ~df.A.isin  ~df.B.isin  ~A & ~B  ~A | ~B
2   Approved    Closed        True       False    False     True
4     Closed  Approved       False        True    False     True
5    No Data  Restrict       False        True    False     True
6   Approved   No Data        True       False    False     True
7     Closed  Restrict       False        True    False     True
8   Approved  Approved        True        True     True     True
9    No Data  Approved       False        True    False     True
10  Restrict   No Data        True       False    False     True
11  Restrict  Approved        True        True     True     True

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

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