ah bon
ah bon

Reputation: 10011

Set index starting from the second row for a dataframe in Pandas

Given a toy dataset as follows:

0  start_date  end_date
1      2019      2021

Read the second row:

df.iloc[0]

Out:

0
start_date    2019
end_date      2021
Name: 1, dtype: object

Read columns:

df.columns

Out:

Out[218]: Index(['start_date', 'end_date'], dtype='object', name=0)

How could I reset index 0 starting from the secode row? Thanks.

   start_date  end_date
0      2019      2021

Upvotes: 1

Views: 2175

Answers (1)

jezrael
jezrael

Reputation: 862511

Use DataFrame.reset_index with drop=True for default index and then remove (set to None) index and columns names by DataFrame.rename_axis:

df = df.reset_index(drop=True).rename_axis(index=None, columns=None)
print(df)
   start_date  end_date
0        2019      2021

Upvotes: 2

Related Questions