Pandas:df['A'] == df['B'] 或 df['B'] == [] 的行数

Pandas: number of rows where df['A'] == df['B'] or df['B'] == []

考虑以下包含列 AB 的数据框 df。我正在尝试查找 df['A'] == df['B']df['B'] == [] 的行数。我该怎么做?

                  A                          B
m:QueryId       
970000000   [0, 1, 2, 3, 4, 5]      [0, 1, 2, 3, 4, 5]
970000001   [0]                                    [0]
970000002   [1, 2, 3, 4, 5]                         []
970000003   [0, 1, 2, 3]                            []
970000004   [1, 2, 4]                            [5,6]

试试 :

df[df['A'].eq(df['B'])|~df['B'].astype(bool)]

此类行数:

(df['A'].eq(df['B'])|~df['B'].astype(bool)).sum()

IIUC,如果只是比较list identity,做一个简单的比较,对于空list,如果所有元素都是list,只要检查其长度为0即可:

((df['A']==df['B'])|df['B'].str.len().eq(0)).sum()

输出:4