Sorting¶
In [1]:
Copied!
import pandas as pd
df_cars = pd.read_csv("data/cars.csv", sep=";")
import pandas as pd
df_cars = pd.read_csv("data/cars.csv", sep=";")
In [2]:
Copied!
df_cars.sort_values(by="Car")
df_cars.sort_values(by="Car")
Out[2]:
Car | MPG | Cylinders | Displacement | Horsepower | Weight | Acceleration | Model | Origin | |
---|---|---|---|---|---|---|---|---|---|
104 | AMC Ambassador Brougham | 13.0 | 8 | 360.0 | 175.0 | 3821. | 11.0 | 73 | US |
10 | AMC Ambassador DPL | 15.0 | 8 | 390.0 | 190.0 | 3850. | 8.5 | 70 | US |
74 | AMC Ambassador SST | 17.0 | 8 | 304.0 | 150.0 | 3672. | 11.5 | 72 | US |
265 | AMC Concord | 19.4 | 6 | 232.0 | 90.00 | 3210. | 17.2 | 78 | US |
323 | AMC Concord | 24.3 | 4 | 151.0 | 90.00 | 3003. | 20.1 | 80 | US |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
84 | Volvo 145e (sw) | 18.0 | 4 | 121.0 | 112.0 | 2933. | 14.5 | 72 | Europe |
187 | Volvo 244DL | 22.0 | 4 | 121.0 | 98.00 | 2945. | 14.5 | 75 | Europe |
215 | Volvo 245 | 20.0 | 4 | 130.0 | 102.0 | 3150. | 15.7 | 76 | Europe |
283 | Volvo 264gl | 17.0 | 6 | 163.0 | 125.0 | 3140. | 13.6 | 78 | Europe |
369 | Volvo Diesel | 30.7 | 6 | 145.0 | 76.00 | 3160. | 19.6 | 81 | Europe |
407 rows × 9 columns
In [3]:
Copied!
df_cars.sort_values(by="Car", ascending=False)
df_cars.sort_values(by="Car", ascending=False)
Out[3]:
Car | MPG | Cylinders | Displacement | Horsepower | Weight | Acceleration | Model | Origin | |
---|---|---|---|---|---|---|---|---|---|
369 | Volvo Diesel | 30.7 | 6 | 145.0 | 76.00 | 3160. | 19.6 | 81 | Europe |
283 | Volvo 264gl | 17.0 | 6 | 163.0 | 125.0 | 3140. | 13.6 | 78 | Europe |
215 | Volvo 245 | 20.0 | 4 | 130.0 | 102.0 | 3150. | 15.7 | 76 | Europe |
187 | Volvo 244DL | 22.0 | 4 | 121.0 | 98.00 | 2945. | 14.5 | 75 | Europe |
84 | Volvo 145e (sw) | 18.0 | 4 | 121.0 | 112.0 | 2933. | 14.5 | 72 | Europe |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
323 | AMC Concord | 24.3 | 4 | 151.0 | 90.00 | 3003. | 20.1 | 80 | US |
265 | AMC Concord | 19.4 | 6 | 232.0 | 90.00 | 3210. | 17.2 | 78 | US |
74 | AMC Ambassador SST | 17.0 | 8 | 304.0 | 150.0 | 3672. | 11.5 | 72 | US |
10 | AMC Ambassador DPL | 15.0 | 8 | 390.0 | 190.0 | 3850. | 8.5 | 70 | US |
104 | AMC Ambassador Brougham | 13.0 | 8 | 360.0 | 175.0 | 3821. | 11.0 | 73 | US |
407 rows × 9 columns
In [4]:
Copied!
df_cars.sort_values(by=["Car", "Model"])
df_cars.sort_values(by=["Car", "Model"])
Out[4]:
Car | MPG | Cylinders | Displacement | Horsepower | Weight | Acceleration | Model | Origin | |
---|---|---|---|---|---|---|---|---|---|
104 | AMC Ambassador Brougham | 13.0 | 8 | 360.0 | 175.0 | 3821. | 11.0 | 73 | US |
10 | AMC Ambassador DPL | 15.0 | 8 | 390.0 | 190.0 | 3850. | 8.5 | 70 | US |
74 | AMC Ambassador SST | 17.0 | 8 | 304.0 | 150.0 | 3672. | 11.5 | 72 | US |
265 | AMC Concord | 19.4 | 6 | 232.0 | 90.00 | 3210. | 17.2 | 78 | US |
323 | AMC Concord | 24.3 | 4 | 151.0 | 90.00 | 3003. | 20.1 | 80 | US |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
84 | Volvo 145e (sw) | 18.0 | 4 | 121.0 | 112.0 | 2933. | 14.5 | 72 | Europe |
187 | Volvo 244DL | 22.0 | 4 | 121.0 | 98.00 | 2945. | 14.5 | 75 | Europe |
215 | Volvo 245 | 20.0 | 4 | 130.0 | 102.0 | 3150. | 15.7 | 76 | Europe |
283 | Volvo 264gl | 17.0 | 6 | 163.0 | 125.0 | 3140. | 13.6 | 78 | Europe |
369 | Volvo Diesel | 30.7 | 6 | 145.0 | 76.00 | 3160. | 19.6 | 81 | Europe |
407 rows × 9 columns
In [5]:
Copied!
df_cars.sort_values(by=["Car", "Model"], ascending=[False, True])
df_cars.sort_values(by=["Car", "Model"], ascending=[False, True])
Out[5]:
Car | MPG | Cylinders | Displacement | Horsepower | Weight | Acceleration | Model | Origin | |
---|---|---|---|---|---|---|---|---|---|
369 | Volvo Diesel | 30.7 | 6 | 145.0 | 76.00 | 3160. | 19.6 | 81 | Europe |
283 | Volvo 264gl | 17.0 | 6 | 163.0 | 125.0 | 3140. | 13.6 | 78 | Europe |
215 | Volvo 245 | 20.0 | 4 | 130.0 | 102.0 | 3150. | 15.7 | 76 | Europe |
187 | Volvo 244DL | 22.0 | 4 | 121.0 | 98.00 | 2945. | 14.5 | 75 | Europe |
84 | Volvo 145e (sw) | 18.0 | 4 | 121.0 | 112.0 | 2933. | 14.5 | 72 | Europe |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
265 | AMC Concord | 19.4 | 6 | 232.0 | 90.00 | 3210. | 17.2 | 78 | US |
323 | AMC Concord | 24.3 | 4 | 151.0 | 90.00 | 3003. | 20.1 | 80 | US |
74 | AMC Ambassador SST | 17.0 | 8 | 304.0 | 150.0 | 3672. | 11.5 | 72 | US |
10 | AMC Ambassador DPL | 15.0 | 8 | 390.0 | 190.0 | 3850. | 8.5 | 70 | US |
104 | AMC Ambassador Brougham | 13.0 | 8 | 360.0 | 175.0 | 3821. | 11.0 | 73 | US |
407 rows × 9 columns
In [6]:
Copied!
df_cars.sort_index
df_cars.sort_index
Out[6]:
<bound method DataFrame.sort_index of Car MPG Cylinders Displacement Horsepower \ 0 STRING DOUBLE INT DOUBLE DOUBLE 1 Chevrolet Chevelle Malibu 18.0 8 307.0 130.0 2 Buick Skylark 320 15.0 8 350.0 165.0 3 Plymouth Satellite 18.0 8 318.0 150.0 4 AMC Rebel SST 16.0 8 304.0 150.0 .. ... ... ... ... ... 402 Ford Mustang GL 27.0 4 140.0 86.00 403 Volkswagen Pickup 44.0 4 97.00 52.00 404 Dodge Rampage 32.0 4 135.0 84.00 405 Ford Ranger 28.0 4 120.0 79.00 406 Chevy S-10 31.0 4 119.0 82.00 Weight Acceleration Model Origin 0 DOUBLE DOUBLE INT CAT 1 3504. 12.0 70 US 2 3693. 11.5 70 US 3 3436. 11.0 70 US 4 3433. 12.0 70 US .. ... ... ... ... 402 2790. 15.6 82 US 403 2130. 24.6 82 Europe 404 2295. 11.6 82 US 405 2625. 18.6 82 US 406 2720. 19.4 82 US [407 rows x 9 columns]>
In [7]:
Copied!
df_cars["Car"].sort_values()
df_cars["Car"].sort_values()
Out[7]:
104 AMC Ambassador Brougham 10 AMC Ambassador DPL 74 AMC Ambassador SST 265 AMC Concord 323 AMC Concord ... 84 Volvo 145e (sw) 187 Volvo 244DL 215 Volvo 245 283 Volvo 264gl 369 Volvo Diesel Name: Car, Length: 407, dtype: object
In [7]:
Copied!