如果要在Python中比较两个数据框的条件 [英] If condition for comparison between two dataframes in Python
本文介绍了如果要在Python中比较两个数据框的条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是python编程的新手.任何人都可以检查下面的语法是否满足条件-
Am new to python programming. Can anyone pls check the below syntax for if condition-
if df1[A]<= df2[B]):
print("")
else:
print("")
获取此异常- ValueError:系列的真值不明确.使用a.empty,a.bool(),a.item(),a.any()或a.all().
推荐答案
您比较数组,没有标量,因此Camparing的输出是另一个数组.因此需要 any
或
You compare arrays, no scalar, so output of camparing is another array. So need any
or all
. Also need length of both Series
is same:
df1 = pd.DataFrame({'A':[1,2,3]})
print (df1)
A
0 1
1 2
2 3
df2 = pd.DataFrame({'B':[1,2,0]})
print (df2)
B
0 1
1 2
2 0
print (df1['A']<= df2['B'])
0 True
1 True
2 False
dtype: bool
#check if at least one True
print ((df1['A']<= df2['B']).any())
True
#check if all values are True
print ((df1['A']<= df2['B']).all())
False
if (df1['A']<= df2['B']).any():
print("at least one value True")
else:
print("no False values")
at least one value True
if (df1['A']<= df2['B']).all():
print("all values True")
else:
print("not all values True")
not all values True
df1 = pd.DataFrame({'A':[1,2,3]})
print (df1)
A
0 1
1 2
2 3
df2 = pd.DataFrame({'B':[1,2,3]})
print (df2)
B
0 1
1 2
2 3
print (df1['A']<= df2['B'])
0 True
1 True
2 True
dtype: bool
#check if at least one True
print ((df1['A']<= df2['B']).any())
True
#check if all values are True
print ((df1['A']<= df2['B']).all())
True
if (df1['A']<= df2['B']).any():
print("at least one value True")
else:
print("no False values")
at least one value True
if (df1['A']<= df2['B']).all():
print("all values True")
else:
print("not all values True")
all values True
这篇关于如果要在Python中比较两个数据框的条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文