DataFrame sort (sort_values)



examples/pandas/planets_sort.py
import sys
import pandas as pd

filename = "planets.csv"
if len(sys.argv) == 2:
    filename = sys.argv[1]

df = pd.read_csv(filename)

sorted_df = df.sort_values('Planet name', ascending=True)
print(sorted_df)
# df remains unchanged

   Planet name  Distance (AU)        Mass
4        Ceres           2.77    0.000150
10      Charon          39.00    0.000254
2        Earth           1.00    1.000000
5      Jupiter           5.20  318.000000
3         Mars           1.50    0.107000
0      Mercury           0.40    0.055000
8      Neptune          30.00   17.000000
9        Pluto          39.00    0.002180
6       Saturn           9.50   95.000000
7       Uranus          19.60   14.000000
1        Venus           0.70    0.815000