在包含最长列表的Pandas DF中找到列的名称


问题内容

给定一个Pandas DataFrame,其中的列表存储在多个列中,是否有一种简单的方法来查找包含每行最长列表的列名?

例如,使用以下数据:

                          positive                 negative          neutral
1   [marvel, moral, bold, destiny]                       []   [view, should]
2                      [beautiful]      [complicated, need]               []
3                      [celebrate]   [crippling, addiction]            [big]

我想将“正”标识为行1最长的列表,将“负”标识为行2和3的最长。

我以为我可以str.len()用来计算列表长度并idmax()获取列名,但无法弄清楚如何组合它们。


问题答案:

IIUC:

In [227]: df.applymap(len).idxmax(axis=1)
Out[227]:
0    positive
1    negative
2    negative
dtype: object