Alex Borg
Alex Borg

Reputation: 141

How to reset Pandas DataFrame indices?

I'm trying to "refresh" the indices of my DataFrame. I'm trying:

df3 = df2.reindex()
df3.head()

But this still gives me:

        Dose
13539.0 1.0
13539.0 2.0
13539.0 5.0
13539.0 3.0
13539.0 4.0

I need to keep the "Dose" column in the same order, but make its indices 0 -> len(Dose).

In other words, my desired output is:

    Dose
0   1.0
1   2.0
2   5.0
3   3.0
4   4.0

Clearly I've pre-processed something to mess this up, but I'd appreciate any insight as to how I can amend it :-)

Upvotes: 1

Views: 8140

Answers (3)

Manoj Khatua
Manoj Khatua

Reputation: 153

df3.reset_index(drop=True, inplace=True)

When you reset the index, a new sequential index is used. Normally the old index is added as a column, but you can use the drop parameter to avoid that.

Refer to this Pandas documentation: pandas.DataFrame.reset_index()

Upvotes: 6

Alex Borg
Alex Borg

Reputation: 141

This solution does the job...

df2 = df2.reset_index()
df2 = df2['Dose']

Upvotes: 0

sander
sander

Reputation: 1440

You might want to lookup the documentation for reset_index. In your case:

df3.reset_index(drop=True)

Upvotes: 1

Related Questions