gpt4 book ai didi

python - 获取 value_count() 的结果以从 Pandas 中脱颖而出

转载 作者:太空宇宙 更新时间:2023-11-04 10:10:39 24 4
gpt4 key购买 nike

我有一个数据框 "df",其中有一列名为 "column1"。通过运行以下代码:

df.column1.value_counts()

我得到的输出包含 column1 中的值及其频率。我想要这个结果在 excel 中。当我通过运行以下代码尝试这样做时:

df.column1.value_counts().to_excel("result.xlsx",index=None)

我收到以下错误:

AttributeError: 'Series' object has no attribute 'to_excel'

我怎样才能完成上述任务?

最佳答案

您正在使用 index = None,您需要索引,它是值的名称。

pd.DataFrame(df.column1.value_counts()).to_excel("result.xlsx") 

关于python - 获取 value_count() 的结果以从 Pandas 中脱颖而出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38575246/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com