Intellipaat Back

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

When there is a DataFrame like the following:

import pandas as pd

df = pd.DataFrame([1, 1, 1, 1, 1], index=[100, 29, 234, 1, 150], columns=['A'])

How can I sort this dataframe by index with each combination of index and column value intact?

1 Answer

0 votes
by (41.4k points)

Use sort_index method with inplace=True to operate in place.

import pandas as pd

df = pd.DataFrame([1, 2, 3, 4, 5], index=[100, 29, 234, 1, 150], columns=['A'])

df.sort_index(inplace=True)

print(df.to_string())

Output:

     A

1    4

29   2

100  1

150  5

234  3

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...