1

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

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, could you please explain why df.iloc[0] returns 0 start_date 2019 end_date 2021 Name: 1, dtype: object in my question? I think it suppose to return first row of df.
@ahbon - yes, because there is only one [] it convert first row to Series, if use one element list like df.iloc[[0]] it return one row DataFrame

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.