【问题标题】:python removing empty list inside a dataframepython删除数据框中的空列表
【发布时间】:2021-10-19 00:13:00
【问题描述】:

我已经尝试了很多组合如何从数据框中删除这个空列表,但没有奏效。

index_names = self.df[self.df['stopwords'].len()==0].index
self.df.drop(index_names, inplace=True)

名为 df['stopwords'] 的数据框看起来像这样

目标是用 [] 列表删除数据框的整行

【问题讨论】:

标签: python python-3.x pandas list dataframe


【解决方案1】:

尝试astypebool,因为[] 将返回False

df = df[df['stopwords'].astype(bool)]

【讨论】:

    【解决方案2】:

    IIUC:

    尝试它们是否是实际的列表对象:

    self.df.loc[~df['stopwords'].map(lambda x:not x)]
    

    如果它们是字符串,则使用:

    self.df.loc[df['stopwords'].ne('[]')]
    

    【讨论】:

      猜你喜欢
      • 2021-11-06
      • 2021-01-12
      • 1970-01-01
      • 2021-07-18
      • 2011-03-16
      • 1970-01-01
      • 2012-09-21
      • 1970-01-01
      • 2016-03-13
      相关资源
      最近更新 更多