How to specify column names when reading an Excel file using Pandas?

I read the Excel spreadsheet in pandas DataFrame as follows:

import pandas as pd xl = pd.ExcelFile("Path + filename") df = xl.parse("Sheet1") 

the first cell value for each column is selected as the column name for the dataFrame, I want to specify my own column names. How to do it?

+6
source share
2 answers

calling .parse with the keyword argument header=None .

 df = xl.parse("Sheet1", header=None) 
+6
source

I think that installing them afterwards is the only way in this case, so if you have, for example, four columns in your DataFrame:

 df.columns = ['W','X','Y','Z'] 

If you know in advance that the headers in the Excel file are probably best renamed, this renamed W to etc:

 df.rename(columns={'W':'A', 'X':'B', etc}) 
+6
source

Source: https://habr.com/ru/post/948190/


All Articles