在浮动对象不可迭代的地方出现错误

嗨,我是python and pandas的新手。这里我有以下格式的数据

A                             B
[2000.0,2000.0]          [2200.0,0.0,2200.0]
[2200.0,0.0]        [2200.0,2200.0,2200.0]  [2200.0,2200.0]
[200.0,200.0,200.0]     [200.0,200.0]
[160.0,160.0,160.0]     NaN

在这里,我尝试比较大小写相同且唯一的两个数组

[2200.0,2200.0] and [2200.0,2200.0]

应返回true

但是 [2200.0,0.0]和[2200.0,0.0]应该返回false。那么,有什么方法可以做到这一点?

----> 2     if set(A) == set(B):

有人可以帮我吗?

xiaoxianchao 回答:在浮动对象不可迭代的地方出现错误

我认为您可以在比较之前用空列表替换缺失值:

df_out[['A','B']] = df_out[['A','B']].applymap(lambda x: [] if x != x else x)

或者:

df_out[['A','B']].applymap(lambda x: x if isinstance(x,list) else [])
#alternative
#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]  [2200.0,2200.0,2200.0]
2  [2200.0,2200.0]     [200.0,200.0,200.0]
3     [200.0,200.0]       [200.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
,

如果您要在每一行中知道AB列的唯一值是否相同,请使用:

df_out['is_same'] = df_out.apply(lambda x: set(x['A']) == set(x['B']),axis=1)
本文链接:https://www.f2er.com/3147142.html

大家都在问