TypeError:to_frame()获得了意外的关键字参数'index'

时间:2019-11-26 17:09:46

标签: python pandas

我指的是this文档页面。这是我的代码:

top_sectors = firm['Sectors'].value_counts().to_frame(index = False)

firm是一个数据框。我需要Dataframe格式的 请让我知道我在做什么错。

1 个答案:

答案 0 :(得分:0)

@Scott Boston指出,pd.Series.to_frame()没有index参数。但是我需要将索引分开。所以这是我想出的解决方法:

top_sectors = firm['Sectors'].value_counts().to_frame()
top_sectors() = top_sectors.reset_index()

它将添加一个新的索引列,并将先前的索引视为一列。这是屏幕截图:

enter image description here

如果要更改名称:

top_sectors.rename(columns = {'index':'new_name'})