AdR
AdR

Reputation: 305

Set the first column of pandas dataframe as header

This is my output DataFrame from reading an excel file

I would like my first column to be index/header

     one               Entity
0    two                   v1
1  three                 Prod
2   four  2015-05-27 00:00:00
3   five  2018-04-27 00:00:00
4    six                 Both
5  seven                   id
6  eight                hello

Upvotes: 1

Views: 10230

Answers (2)

user9518134
user9518134

Reputation:

To Set the first column of pandas data frame as header

set "header=1" while reading file

eg: df = pd.read_csv(inputfilePath, header=1)

set skiprows=1 while reading the file

eg: df = df.read_csv(inputfilepath, skiprows=1)

set iloc[0] in dataframe

eg: df.columns = df.iloc[0]

I hope this will help.

Upvotes: 4

BENY
BENY

Reputation: 323226

One way is using T twice

df=df.T.set_index(0).T

Upvotes: 3

Related Questions