python - 在浮动对象不可迭代的情况下出现错误
问题描述
嗨,我是新手python and pandas
。这里我有以下格式的数据
A B
[2000.0, 2000.0] [2200.0, 0.0, 2200.0]
[2200.0, 0.0, 0.0] [2200.0, 2200.0, 2200.0]
[2200.0, 2200.0, 2200.0] [2200.0, 2200.0, 2200.0]
[200.0, 200.0, 200.0] [200.0, 0.0, 200.0]
[160.0, 160.0, 160.0] NaN
在这里,我试图将两个数组与相等且唯一的情况进行比较
[2200.0,2200.0,2200.0] and [2200.0, 2200.0, 2200.0]
应该返回true
但是 [2200.0,0.0,0.0] 和 [2200.0,0.0,0.0] 应该返回给我false
. 那么,有没有办法做到这一点?
----> 2 if set(A) == set(B):
谁能帮我这个 ?
解决方案
我认为您可以在比较之前用空列表替换缺失值:
df_out[['A','B']] = df_out[['A','B']].applymap(lambda x: [] if x != x else x)
或者:
df_out[['A','B']] = df_out[['A','B']].applymap(lambda x: x if isinstance(x, list) else [])
#alternative
#df_out[['A','B']] = df_out[['A','B']].applymap(lambda x: [] if isinstance(x, float) else x)
print (df_out)
A B
0 [2000.0, 2000.0, 0.0] [2200.0, 0.0, 2200.0]
1 [2200.0, 0.0, 0.0] [2200.0, 2200.0, 2200.0]
2 [2200.0, 2200.0, 2200.0] [200.0, 200.0, 200.0]
3 [200.0, 200.0, 200.0] [200.0, 0.0, 200.0]
4 [160.0, 160.0, 160.0] []
测试:
def comp(A,B):
try:
a= set(A)
b= set(B)
return ((a == b) and (len(a) == 1) and (len(b) == 1))
except TypeError:
return False
或者:
def comp(A,B):
try:
return (set(A) == set(B)) and (len(set(A)) == 1) and (len(set(B)) == 1)
except TypeError:
return False
for ins, rw in df_out.iterrows():
val = comp(rw.Previous_Three, rw.Next_Three)
print (val)
False
False
True
False
False