【问题标题】:pandas.Series.explode throws AttributeErrorpandas.Series.explode 抛出 AttributeError
【发布时间】:2020-03-28 12:26:18
【问题描述】:

我正在尝试 count the number of unique values in a dataframe column 使用 pandas.Series.explode 因为我对爆炸整个数据框不感兴趣。 我试图爆炸的数据框列看起来像

df["lists"].head()

0    [cl5, bn6, sp1]  
1    [cl5, bn6, sp1]  
2    [cl5, bn6, sp1]  
3         [bn6, sp1]  
4         [bn6, sp1]  
Name: lists, dtype: object

我尝试了df.lists.explode().value_counts(),它抛出了一个AttributeError:“Series”对象没有“explode”属性。 在 exploding the entire dataframedf.explode('lists') 时观察到相同的错误。我看不到它是否已被弃用,所以我不确定源示例也不起作用时出了什么问题。

【问题讨论】:

  • df.to_frame().explode('lists') 可以
  • 您使用的是什么版本的 Pandas 包?在多个环境的情况下,如果您不确定,只需在 python 代码中导入 pandas 后打印pandas.__version__。您能否编辑您的问题并将该信息添加到您的问题中?谢谢。

标签: python pandas dataframe series


【解决方案1】:

根据Pandas official documentation about Series.explode(),这个函数是

0.25.0 版中的新功能。

我认为您的问题可能是由于软件包版本较旧。更新您的 pandas 包可能会解决您的问题。

【讨论】:

    猜你喜欢
    • 2019-10-03
    • 2020-09-28
    • 2014-07-05
    • 2012-08-05
    • 1970-01-01
    • 1970-01-01
    • 2022-07-01
    • 1970-01-01
    • 2020-12-29
    相关资源
    最近更新 更多