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

use*_*747 8 python python-3.x pandas

给定一个Pandas DataFrame,列表存储在几个列中,是否有一种简单的方法可以找到包含每行最长列表的列名?

例如,使用此数据:

                          positive                 negative          neutral
1   [marvel, moral, bold, destiny]                       []   [view, should]
2                      [beautiful]      [complicated, need]               []
3                      [celebrate]   [crippling, addiction]            [big]
Run Code Online (Sandbox Code Playgroud)

我想将"肯定"标识为第1行的最长列和第2行和第3行的"负"列.

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

Max*_*axU 15

IIUC:

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


Ale*_*der 5

>>> df.apply(lambda row: row.apply(len).argmax(), axis=1)
0    positive
1    negative
2    negative
dtype: object
Run Code Online (Sandbox Code Playgroud)