我认为您可以在比较之前用空列表替换缺失值:
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
,
如果您要在每一行中知道A
和B
列的唯一值是否相同,请使用:
df_out['is_same'] = df_out.apply(lambda x: set(x['A']) == set(x['B']),axis=1)
本文链接:https://www.f2er.com/3147142.html