使用熊猫单元格中的列表长度选择行

Zho*_*Xie 4 python select list loc pandas

我有一张桌子 df

    a   b    c
1   x   y   [x]

2   x   z   [c,d]

3   x   t   [e,f,g]
Run Code Online (Sandbox Code Playgroud)

只是想知道如何使用 c 列的长度选择行

df.loc[len(df.c) >1]
Run Code Online (Sandbox Code Playgroud)

我知道这是不对的......什么应该是正确的?

小智 19

尝试这个:

df[df.c.map(len)>1]
Run Code Online (Sandbox Code Playgroud)

  • `df[df['class'].map(len) >1 ] ` (2认同)

WeN*_*Ben 3

您可以使用

df.loc[np.array(list(map(len,df.c.values)))>1]
Run Code Online (Sandbox Code Playgroud)