How to reset both index and columns in a dataframe?

Viewed 33

I have a DataFrame in pandas like the one below.

After deleting several columns and rows, the index and columns are not longer a continuous range.

How could it set it back to 0 -> n? I tried reset_index without success.

     4    12    15     22
5
9
14
26
31
37

expected output:

    0      1      2      3
0
1
2
3
4
5
1 Answers

It looks like you want to reset both indexes.

You can use reset_index and set_axis as reset_index does not handle columns:

df = df.reset_index(drop=True).set_axis(range(df.shape[1]), axis=1)

example input:

    4 12 15 22
5   x  x  x  x
9   x  x  x  x
14  x  x  x  x
26  x  x  x  x
31  x  x  x  x
37  x  x  x  x

matching output:

   0  1  2  3
0  x  x  x  x
1  x  x  x  x
2  x  x  x  x
3  x  x  x  x
4  x  x  x  x
5  x  x  x  x
Related