python-pandasHow to sort dataframe by index
Usage example
import pandas as pd
df = pd.DataFrame({
'phone': ['ip5', 'ip6', 'ip8', 'sms', 'xi'],
'price': [204, 704, 404, 405, 305]
})
df = df.sort_values(by=['price'])
print(df)
df = df.sort_index()
print(df)
output
phone price
0 ip5 204
4 xi 305
2 ip8 404
3 sms 405
1 ip6 704
phone price
0 ip5 204
1 ip6 704
2 ip8 404
3 sms 405
4 xi 305
Related
More of Python Pandas
- How to join dataframes on multiple columns
- How to convert column values to Int
- How to left join dataframes
- How to convert column values to Float
- How to convert column object values to String
- How to save dataframe to XML file
- How to replace strings in dataframe column
- What is opposite of melt() in Pandas
- How to inner join dataframes
- How to replace values in dataframe
See more codes...