How to apply a function can test whether an element in a list in data frame with python-pandas?

后端 未结 2 720
轮回少年
轮回少年 2021-01-21 18:52

For a data frame df, it has a column col contains list value:

 id    col
 1     [1, 10, 23]
 2     [2, 11, 19, 29]
 ..


        
相关标签:
2条回答
  • 2021-01-21 19:03

    option using apply
    df.col.apply(lambda x: 1 in x)

    demo
    df[df.col.apply(lambda x: 1 in x)]

    0 讨论(0)
  • 2021-01-21 19:09

    try read the "apply" function in pandas document.

    df['has_element'] = df['col'].apply(lambda x: element in x)
    
    0 讨论(0)
提交回复
热议问题