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
- What is opposite of melt() in Pandas
- How to join dataframes
- Select dataframe rows between two values
- How to convert column values to Float
- How to filter dataframe by column value
- How to convert yyyymmdd string to datetime
- How to drop duplicates from dataframe and keep last found
- How to convert datetime to timestamp
- How to use multiple conditions in mask()
- How to map values in data frame using mapping function
See more codes...