PYTHON:更改列名称

时间:2018-05-09 15:19:17

标签: python pandas dataframe

我正在使用pandas作为 - pd.read_excel()读取excel表,然后将其放入列表并将其附加到最终的数据框。

我正在阅读的表格中有一个列名销售,在最终的数据框中,我的列名为项目

AllFields 是包含所有列列表的数据框架。

所以我的问题是在将列表附加到最终数据框时,销售列的记录位于列名下。

我正在从表单

中读取的数据示例

Sales 2013 2014 2015 2016 2017 2018 2019 Units Sold 0 0 0 0 0 0 0 Unit Sale Price $900 $900 $900 $900 $900 $900 $900 Unit Profit $500 $500 $500 $500 $500 $500 $500

然后附加到具有列

的数据框

Full Project Item Market Project Project Step Round Sponsor Subproduct 2013 2014 2015 2016 2017 2018 2019

reading_book1 = pd.read_excel(file, sheet_name="1-Rollout", skiprows=restvalue).iloc[:10]

EmptyList1 = [reading_book1]

RestDataframe = RestDataframe.append(AllFields).append(EmptyList1)
RestDataframe['Project'] = read_ProjectNumber
RestDataframe['Full Project'] = read_fullProject
RestDataframe['Sponsor'] = read_Sponsor
RestDataframe['Round'] = read_round
RestDataframe['Project Step'] = read_projectstep
RestDataframe['Market'] = "Rest of the World Market"


FinalDataframe = FinalDataframe.append(CADataframe).append(RestDataframe) 

1 个答案:

答案 0 :(得分:0)

您需要使用pd.concat

RestDataFrame= pd.concat([AllFields,EmptyList1], axis=1)

然后使用

Sales列的名称更改为Item
data.rename(columns={'Sales':'Item'}, inplace=True)