pandas 比较多列中列表的大小

3hvapo4f  于 2023-01-07  发布在  其他
关注(0)|答案(2)|浏览(151)

我有以下数据框:

df = pd.DataFrame([([40.33, 40.34, 40.22],[-71.11, -71.21, -71.14],[12, 45, 10]), ([41.23, 41.40, 41.22],[-72.01, -72.01, -72.01],[11, 23, 15]), ([43.33, 43.34],[-70.11, -70.21],[12, 40]), ([41.23, 41.40], [-72.01, -72.01, -72.01], [11, 23, 15])], columns=['long', 'lat', 'accuracy'])
long                       lat                           accuracy
[40.33, 40.34, 40.22]      [-71.11, -71.21, -71.14]     [12, 45, 10]
[41.23, 41.40, 41.22]      [-72.01, -72.01, -72.01]     [11, 23, 15]
[43.33, 43.34]             [-70.11, -70.21]             [12, 40]
[41.23, 41.40]             [-72.01, -72.01, -72.01]     [11, 23, 15]
...

每一列都包含一个浮点数列表。我想检查所有三列的每一行中,这些列表的大小是否相同。最好的方法是什么?如果所有列表的大小相同,则返回另一个名为sanity的列,返回TRUE;如果至少一个列表的大小与其他列表不同,则返回FALSE
预期输出为:

long                       lat                           accuracy      sanity
[40.33, 40.34, 40.22]      [-71.11, -71.21, -71.14]     [12, 45, 10]    TRUE
[41.23, 41.40, 41.22]      [-72.01, -72.01, -72.01]     [11, 23, 15]    TRUE
[43.33, 43.34]             [-70.11, -70.21]             [12, 40]        TRUE
[41.23, 41.40]             [-72.01, -72.01, -72.01]     [11, 23, 15]    FALSE
pb3s4cty

pb3s4cty1#

您可以使用applymapnunique来解决此问题:

df["sanity"] = df.applymap(len).nunique(axis=1).eq(1)
输出:
print(df) 

                    long                       lat      accuracy  sanity
0  [40.33, 40.34, 40.22]  [-71.11, -71.21, -71.14]  [12, 45, 10]    True
1   [41.23, 41.4, 41.22]  [-72.01, -72.01, -72.01]  [11, 23, 15]    True
2         [43.33, 43.34]          [-70.11, -70.21]      [12, 40]    True
3          [41.23, 41.4]  [-72.01, -72.01, -72.01]  [11, 23, 15]   False
vngu2lb8

vngu2lb82#

df['new_col'] = df.stack().str.len().unstack().nunique(axis=1).eq(1)

相关问题