np.where多个逻辑语句 pandas [英] np.where multiple logical statements pandas
问题描述
我知道有关使用np.where.
我有2个数据框:
df1
A B C D E F Postset
0 1 2 3 4 5 6 yes
1 1 2 3 4 5 6 no
2 1 2 3 4 5 6 yes
df2
A B C D E F Preset
0 1 2 3 4 5 6 yes
1 1 2 3 4 5 6 yes
2 1 2 3 4 5 6 yes
我想比较每个数据帧中行的唯一性.为此,我需要检查许多选定列的所有值是否相等.
I want to compare the uniqueness of the rows in each dataframe. To do this, I need to check that all values are equal for a number of selected columns.
来自此问题:如果我正在检查a
b
列c
d
e
f
我可以做到:
From this question: if I am checking columns a
b
c
d
e
f
I can do:
np.where((df1.A != df2.A) | (df1.B != df2.B) | (df1.C != df2.C) | (df1.D != df2.D) | (df1.E != df2.E) | (df1.F != df2.F))
正确给出的信息:
(array([], dtype=int64),)
即这两个数据帧的所有列中的值都独立相等.
i.e. the values in all columns are independently equal for both dataframes.
这对于较小的数据框来说很好,但是我的实际数据框具有大量必须检查的列. np.where
条件太长,无法准确写出.
This is fine for a small dataframe, but my real dataframe has a high number of columns that I must check. The np.where
condition is too long to write out with accuracy.
相反,我想将我的专栏放在列表中:
Instead, I would like to put my columns into a list:
columns_check_list = ['A','B','C','D','E','F']
并使用我的np.where
语句自动检查所有列.
And use my np.where
statement to perform my check over all columns automatically.
这显然行不通,但是它是我要查找的表格类型.像这样:
This obviously doesn't work, but its the type of form I am looking for. Something like:
check = np.where([df[column) != df[column] | for column in columns_check_list])
我该如何实现?
注意事项:
- 我有很多专栏
- 我的数据格式是固定的.
- 列中的值可以包含
strings
或floats
.
- I have many columns
- The format of my data is fixed.
- The values within columns may contain either
strings
orfloats
.
推荐答案
似乎您需要 any
,如果至少每行一个值是True
:
mask= ~(df1[columns_check_list] == df2[columns_check_list]).all(axis=1).values
print (mask)
[False False False]
或更可读,谢谢 IanS :
mask= (df1[columns_check_list] != df2[columns_check_list]).any(axis=1).values
print (mask)
[False False False]
也可以比较numpy array
s:
mask= (df1[columns_check_list].values != df2[columns_check_list].values).any(axis=1)
print (mask)
[False False False]
这篇关于np.where多个逻辑语句 pandas 的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!