如何在pandas中更改列名称的位置?

时间:2018-09-25 19:35:27

标签: python-3.x pandas

code and output

answ = pd.DataFrame()
answ['price_max_variety'] = reviews.groupby('variety').price.max()
answ['price_min_variety'] = reviews.groupby('variety').price.min()
answ.head()

请参考图片。

嘿,在我看来,这很挑剔,但我希望它做对。我不知道如何使它看起来正确,并且由于通用词找不到相关信息。

1 个答案:

答案 0 :(得分:0)

您已经指出的空白灰色行是为DataFrame的索引名称variety腾出空间。该索引来自df.groupby()的默认行为:分组的列最终出现在结果DataFrame的索引中。

要覆盖此内容,请尝试df.groupby('variety', as_index=False)。或者,如果您有一个带有要移入列的索引的DataFrame,请运行df.reset_index()