【问题标题】:Select row using the length of list in pandas cell [duplicate]使用熊猫单元格中列表的长度选择行[重复]
【发布时间】:2018-05-23 01:22:35
【问题描述】:

我有一张桌子 df

    a   b    c
1   x   y   [x]

2   x   z   [c,d]

3   x   t   [e,f,g]

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

比如

df.loc[len(df.c) >1]

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

【问题讨论】:

    标签: python pandas list pandas-loc


    【解决方案1】:

    您还可以创建第四列,其中包含“c”列中每个列表的长度。然后过滤掉新列中的条目大于 1 的行。

    df["length"] = df["c"].apply(lambda x: len(x) > 1)
    df = df.loc[df["length"], :].drop(["length"], axis=1)
    

    【讨论】:

      【解决方案2】:

      应用:

      df[df.c.apply(lambda x: len(x) > 1)]
      

      【讨论】:

        【解决方案3】:

        试试这个:

        df[df.c.map(len)>1]
        

        【讨论】:

        • df[df['class'].map(len) >1 ]
        【解决方案4】:

        你可以使用

        df.loc[np.array(list(map(len,df.c.values)))>1]
        

        【讨论】:

          猜你喜欢
          • 2019-09-22
          • 2022-01-27
          • 2021-07-15
          • 2021-11-04
          • 2021-10-01
          • 2018-10-21
          • 2020-08-01
          • 1970-01-01
          • 2021-05-18
          相关资源
          最近更新 更多