简体   繁体   English

PYTHON:更改列名称

[英]PYTHON: Changing Column names

I am reading a excel sheet by using pandas as - pd.read_excel() and then putting it in a list and appending it to the final Data-frame. 我正在通过将pandas用作-pd.read_excel()来读取Excel工作表,然后将其放入列表中并将其附加到最终的数据帧中。

The sheet which i am reading have a column name Sales and in the final data-frame i have column with name Item . 我正在阅读的工作表的列名称为Sales,而在最后一个数据框中,我的列名称为Item

AllFields is the Data-frame with all the list of columns. AllFields是具有所有列列表的数据框架。

So my question is while appending the list to the final data-frame the records of the Sales Columns comes under the column name Item . 所以我的问题是,在将列表追加到最终数据框时,“ 销售列”的记录位于列名称“ 项目”下

Example of data which i am reading from sheet 我从工作表中读取的数据示例

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

and then appending to the data-frame which have columns 然后附加到具有列的数据框

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) 

You need to use pd.concat 您需要使用pd.concat

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

And then change the name of Sales column to Item column with 然后使用以下命令将“ Sales列的名称更改为“ Item

data.rename(columns={'Sales':'Item'}, inplace=True)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM