使用重复的列名重命名Pandas数据框中的列? [英] Renaming columns in a Pandas dataframe with duplicate column names?

查看:851
本文介绍了使用重复的列名重命名Pandas数据框中的列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个df X,其列名重复:

I have a df X with columns with duplicate names:

In [77]: X_R
Out[77]: 
      dollars  dollars
   0   0.7085   0.5000

我想重命名它,以便拥有:

I want to rename it so that I have:

In [77]: X_R
Out[77]: 
       Retail   Cost
   0   0.7085   0.5000

使用Pandas重命名功能确实有效:

Using the Pandas rename function does' work:

X_R.rename(index=str, columns={"dollars": "Retail", "dollars": "Cost"})

请给我两列名为Cost的列.

Just gives me two columns named Cost.

在这种情况下如何重命名列?

How can I rename the columns in this case?

推荐答案

X_R.columns = ['Retail','Cost']

这篇关于使用重复的列名重命名Pandas数据框中的列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆