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 convert column values to Int
- How to convert column object values to String
- How to replace values in dataframe
- What is opposite of melt() in Pandas
- How to load dataframe from Excel (XSL)
- Get first N rows (head) from data frame
- How to use multiple conditions in mask()
- How to set data frame index
- How to count dataframe NaN rows
- How to sort dataframe in descending order
See more codes...