将数据帧与熊猫连接后如何添加索引?

时间:2021-06-10 10:02:00

标签: python pandas

我连接文件并尝试重命名列。但它向我展示了错误。如果我想在连接后添加索引,我应该如何修复代码?

total = [df01,df02,df03,df04,df05,df06,df07,df08,df09,df10,df11,df12,df13,df14,df15]
date_count = pd.concat(total, axis=0)
date_count

第一次输出

                        new_sentiment
date    new_sentiment   
2020-03-06  POSITIVE    1
2020-03-17  NEGATIVE    1
            NEUTRAL     1
2020-03-18  NEUTRAL     1
2020-03-23  NEUTRAL     1
... ... ...
2021-05-30  NEGATIVE    89
            POSITIVE    13
2021-05-31  NEGATIVE    13
            NEUTRAL     11
            POSITIVE    2
760 rows × 1 columns
date_count.columns(['date','new_sentiment','count'])

二次输出

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-174-57eb7952ea0a> in <module>
----> 1 date_count.columns(['date','new_sentiment','count'])

TypeError: 'Index' object is not callable

2 个答案:

答案 0 :(得分:0)

This ?
date_count.columns=['date']
date_count.rename_axis(index=['date','new_sentiment'])

答案 1 :(得分:0)

如果你想从索引中创建列,你应该使用:

df.reset_index(inplace=True)  

这一行将从多索引的每个部分创建列