使用python选择特定的Excel行。因此,在excel中,我会做
setEvents
用于获取非封闭或空白数据框中的那些列。尝试使用
If(And(Or(A<>({"Closed",""}),Or(B<>({"Closed",""})))
问题是python正在删除列,例如:
df = df[(~df.A.isin([Closed","No Data"])) &(~df.B.isin([Closed","No Data"]))]
我不想要 正如其中一个链接所建议,也尝试过
A B
Approved Closed
No Data Restrict
Restrict No Data
得到与我尝试.isin时相同的结果
答案 0 :(得分:1)
我将使用以下示例数据:
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
列显示~df.A.isin(["Closed","No Data"])
的值,对于A既不包含True
也不包含Closed
No Data
~df.B.isin
列显示~df.B.isin(["Closed","No Data"])
的值,对于B既不包含True
也不包含Closed
No Data
~A & ~B
列显示(~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"]))
您第一次尝试在"
的开头缺少Closed"
。添加它,我们就有
df[(~df.A.isin(["Closed","No Data"])) &(~df.B.isin(["Closed","No Data"]))]
这给了我们
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
结果仅显示完全没有Closed
和没有No Data
的那些行。
文本评论中的建议:
df[(~df.A.isin(["Closed","No Data"])) |(~df.B.isin(["Closed","No Data"]))]
给予我们
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
这里我们有|
(or
)而不是&
(and
),所以行可以包含Closed
或No Data
,但同时不在A和B中。这表示具有以下内容的行:
A B
Approved Closed
No Data Restrict
Restrict No Data
将包含,但不行包含:
A B
Closed Closed
Closed No Data
您的第二次尝试:
df.loc[(df[A] != "Closed") & (df[B] != "Closed") &
(df[A] != "No data") & (df[B] != "No data")
需要在列标签周围加上引号。您可以使用df.A
或df['A']
,但不能 df[A]
此外,您在data
中将No data
拼写为小写d
,而在其他地方,您将其拼写为大写D
-No Data
。在python中,这是不一样的。如果我们解决该问题:
df.loc[(df['A'] != "Closed") & (df['B'] != "Closed") &
(df['A'] != "No Data") & (df['B'] != "No Data")]
这给了我们与第一次尝试相同的东西:
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
如果稍微重新排列此表达式,请使用括号和|
(or
):
df.loc[((df['A'] != "Closed") & (df['A'] != "No Data")) |
((df['B'] != "Closed") & (df['B'] != "No Data"))]
我们得到:
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