Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

pandas

I read an Excel Sheet into a pandas DataFrame this way:

import pandas as pd

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

the first cell's value of each column is selected as the column name for the dataFrame, I want to specify my own column names, How do I do this?

like image 316
Rakesh Adhikesavan Avatar asked Jun 27 '13 06:06

Rakesh Adhikesavan


Video Answer


1 Answers

I think setting 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 what the headers in the Excelfile are its probably better to rename them, this would rename W into A, etc:

df.rename(columns={'W':'A', 'X':'B', etc})
like image 124
Rutger Kassies Avatar answered Sep 18 '22 15:09

Rutger Kassies