user11756724
user11756724

Reputation:

Python | Pandas | Read Excel table with different format

I've a excel file with the following format:

enter image description here

I'm trying read this table as a dataframe in order to get the following format:

Name, Age, Job
Peter, 25, Analyst

For that I am using the following code:

df = pd.read_excel(filename, sheet_name='Sheet1' ,index_col=False, header=0, squeeze=True)

I was thinking that the squeeze would solve it but I am getting Name and Peter as columns names :(

How can I resolve this?

Thanks!

Upvotes: 2

Views: 146

Answers (1)

Heikura
Heikura

Reputation: 1089

You can transpose it like a matrix. In addition, set index_col to be "Name" if you don't want indexes.

df = pd.read_excel(filename, sheet_name='Sheet1' ,index_col="Name", header=0)

df = df.T
print(df)
Name   Age      Job
Peter   25  Analyst

See more from here: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.transpose.html

Upvotes: 1

Related Questions