Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (17.6k points)

Take the following data-frame:

x = np.tile(np.arange(3),3)

y = np.repeat(np.arange(3),3)

df = pd.DataFrame({"x": x, "y": y})

   x  y

0  0  0

1  1  0

2  2  0

3  0  1

4  1  1

5  2  1

6  0  2

7  1  2

8  2  2

I need to sort it by x first, and only second by y:

df2 = df.sort(["x", "y"])

   x  y

0  0  0

3  0  1

6  0  2

1  1  0

4  1  1

7  1  2

2  2  0

5  2  1

8  2  2

How can I change the index such that it is ascending again. I.e. how do I get this:

   x  y

0  0  0

1  0  1

2  0  2

3  1  0

4  1  1

5  1  2

6  2  0

7  2  1

8  2  2

I have tried the following. Unfortunately, it doesn't change the index at all:

df2.reindex(np.arange(len(df2.index)))

1 Answer

0 votes
by (41.4k points)

Using reset_index reset the index. 

In [19]: df2 = df2.reset_index(drop=True)

In [20]: df2

Out[20]:

   x  y

0  0  0

1  0  1

2  0  2

3  1  0

4  1  1

5  1  2

6  2  0

7  2  1

8  2  2

Here, drop=True is used to indicate that we want to drop the existing index instead of adding it as an additional column to your dataframe.

Related questions

0 votes
1 answer
0 votes
1 answer

Browse Categories

...