Adding and Deleting¶
In [2]:
Copied!
import pandas as pd
df_cars = pd.read_csv("data/cars.csv", sep=";", skiprows=[1])
import pandas as pd
df_cars = pd.read_csv("data/cars.csv", sep=";", skiprows=[1])
Adding Columns¶
In [3]:
Copied!
df_cars["Index"] = df_cars.index
df_cars
df_cars["Index"] = df_cars.index
df_cars
Out[3]:
Car | MPG | Cylinders | Displacement | Horsepower | Weight | Acceleration | Model | Origin | Index | |
---|---|---|---|---|---|---|---|---|---|---|
0 | Chevrolet Chevelle Malibu | 18.0 | 8 | 307.0 | 130.0 | 3504.0 | 12.0 | 70 | US | 0 |
1 | Buick Skylark 320 | 15.0 | 8 | 350.0 | 165.0 | 3693.0 | 11.5 | 70 | US | 1 |
2 | Plymouth Satellite | 18.0 | 8 | 318.0 | 150.0 | 3436.0 | 11.0 | 70 | US | 2 |
3 | AMC Rebel SST | 16.0 | 8 | 304.0 | 150.0 | 3433.0 | 12.0 | 70 | US | 3 |
4 | Ford Torino | 17.0 | 8 | 302.0 | 140.0 | 3449.0 | 10.5 | 70 | US | 4 |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
401 | Ford Mustang GL | 27.0 | 4 | 140.0 | 86.0 | 2790.0 | 15.6 | 82 | US | 401 |
402 | Volkswagen Pickup | 44.0 | 4 | 97.0 | 52.0 | 2130.0 | 24.6 | 82 | Europe | 402 |
403 | Dodge Rampage | 32.0 | 4 | 135.0 | 84.0 | 2295.0 | 11.6 | 82 | US | 403 |
404 | Ford Ranger | 28.0 | 4 | 120.0 | 79.0 | 2625.0 | 18.6 | 82 | US | 404 |
405 | Chevy S-10 | 31.0 | 4 | 119.0 | 82.0 | 2720.0 | 19.4 | 82 | US | 405 |
406 rows × 10 columns
Combining Columns¶
You can combine columns to create a new Series and then add the Series as a new column.
In [4]:
Copied!
df_cars["Car"] + "with a " + str(df_cars["Cylinders"]) + " cylinder engine"
df_cars["Car"] + "with a " + str(df_cars["Cylinders"]) + " cylinder engine"
Out[4]:
0 Chevrolet Chevelle Malibuwith a 0 8\n1 ... 1 Buick Skylark 320with a 0 8\n1 8\n2 ... 2 Plymouth Satellitewith a 0 8\n1 8\n2... 3 AMC Rebel SSTwith a 0 8\n1 8\n2 ... 4 Ford Torinowith a 0 8\n1 8\n2 8... ... 401 Ford Mustang GLwith a 0 8\n1 8\n2 ... 402 Volkswagen Pickupwith a 0 8\n1 8\n2 ... 403 Dodge Rampagewith a 0 8\n1 8\n2 ... 404 Ford Rangerwith a 0 8\n1 8\n2 8... 405 Chevy S-10with a 0 8\n1 8\n2 8\... Name: Car, Length: 406, dtype: object
In [5]:
Copied!
df_cars["CarText"] = df_cars["Car"] + "with a " + str(df_cars["Cylinders"]) + " cylinder engine"
df_cars["CarText"] = df_cars["Car"] + "with a " + str(df_cars["Cylinders"]) + " cylinder engine"
In [6]:
Copied!
df_cars
df_cars
Out[6]:
Car | MPG | Cylinders | Displacement | Horsepower | Weight | Acceleration | Model | Origin | Index | CarText | |
---|---|---|---|---|---|---|---|---|---|---|---|
0 | Chevrolet Chevelle Malibu | 18.0 | 8 | 307.0 | 130.0 | 3504.0 | 12.0 | 70 | US | 0 | Chevrolet Chevelle Malibuwith a 0 8\n1 ... |
1 | Buick Skylark 320 | 15.0 | 8 | 350.0 | 165.0 | 3693.0 | 11.5 | 70 | US | 1 | Buick Skylark 320with a 0 8\n1 8\n2 ... |
2 | Plymouth Satellite | 18.0 | 8 | 318.0 | 150.0 | 3436.0 | 11.0 | 70 | US | 2 | Plymouth Satellitewith a 0 8\n1 8\n2... |
3 | AMC Rebel SST | 16.0 | 8 | 304.0 | 150.0 | 3433.0 | 12.0 | 70 | US | 3 | AMC Rebel SSTwith a 0 8\n1 8\n2 ... |
4 | Ford Torino | 17.0 | 8 | 302.0 | 140.0 | 3449.0 | 10.5 | 70 | US | 4 | Ford Torinowith a 0 8\n1 8\n2 8... |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
401 | Ford Mustang GL | 27.0 | 4 | 140.0 | 86.0 | 2790.0 | 15.6 | 82 | US | 401 | Ford Mustang GLwith a 0 8\n1 8\n2 ... |
402 | Volkswagen Pickup | 44.0 | 4 | 97.0 | 52.0 | 2130.0 | 24.6 | 82 | Europe | 402 | Volkswagen Pickupwith a 0 8\n1 8\n2 ... |
403 | Dodge Rampage | 32.0 | 4 | 135.0 | 84.0 | 2295.0 | 11.6 | 82 | US | 403 | Dodge Rampagewith a 0 8\n1 8\n2 ... |
404 | Ford Ranger | 28.0 | 4 | 120.0 | 79.0 | 2625.0 | 18.6 | 82 | US | 404 | Ford Rangerwith a 0 8\n1 8\n2 8... |
405 | Chevy S-10 | 31.0 | 4 | 119.0 | 82.0 | 2720.0 | 19.4 | 82 | US | 405 | Chevy S-10with a 0 8\n1 8\n2 8\... |
406 rows × 11 columns
Deleting columns¶
In [7]:
Copied!
df_cars.drop(columns=["Car", "CarText"])
df_cars.drop(columns=["Car", "CarText"])
Out[7]:
MPG | Cylinders | Displacement | Horsepower | Weight | Acceleration | Model | Origin | Index | |
---|---|---|---|---|---|---|---|---|---|
0 | 18.0 | 8 | 307.0 | 130.0 | 3504.0 | 12.0 | 70 | US | 0 |
1 | 15.0 | 8 | 350.0 | 165.0 | 3693.0 | 11.5 | 70 | US | 1 |
2 | 18.0 | 8 | 318.0 | 150.0 | 3436.0 | 11.0 | 70 | US | 2 |
3 | 16.0 | 8 | 304.0 | 150.0 | 3433.0 | 12.0 | 70 | US | 3 |
4 | 17.0 | 8 | 302.0 | 140.0 | 3449.0 | 10.5 | 70 | US | 4 |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
401 | 27.0 | 4 | 140.0 | 86.0 | 2790.0 | 15.6 | 82 | US | 401 |
402 | 44.0 | 4 | 97.0 | 52.0 | 2130.0 | 24.6 | 82 | Europe | 402 |
403 | 32.0 | 4 | 135.0 | 84.0 | 2295.0 | 11.6 | 82 | US | 403 |
404 | 28.0 | 4 | 120.0 | 79.0 | 2625.0 | 18.6 | 82 | US | 404 |
405 | 31.0 | 4 | 119.0 | 82.0 | 2720.0 | 19.4 | 82 | US | 405 |
406 rows × 9 columns
Split columns¶
In [8]:
Copied!
df_cars["Car"].str.split(" ")
df_cars["Car"].str.split(" ")
Out[8]:
0 [Chevrolet, Chevelle, Malibu] 1 [Buick, Skylark, 320] 2 [Plymouth, Satellite] 3 [AMC, Rebel, SST] 4 [Ford, Torino] ... 401 [Ford, Mustang, GL] 402 [Volkswagen, Pickup] 403 [Dodge, Rampage] 404 [Ford, Ranger] 405 [Chevy, S-10] Name: Car, Length: 406, dtype: object
In [9]:
Copied!
tokens_series = df_cars["Car"].str.split(" ", expand=True)
tokens_series
tokens_series = df_cars["Car"].str.split(" ", expand=True)
tokens_series
Out[9]:
0 | 1 | 2 | 3 | 4 | 5 | |
---|---|---|---|---|---|---|
0 | Chevrolet | Chevelle | Malibu | None | None | None |
1 | Buick | Skylark | 320 | None | None | None |
2 | Plymouth | Satellite | None | None | None | None |
3 | AMC | Rebel | SST | None | None | None |
4 | Ford | Torino | None | None | None | None |
... | ... | ... | ... | ... | ... | ... |
401 | Ford | Mustang | GL | None | None | None |
402 | Volkswagen | Pickup | None | None | None | None |
403 | Dodge | Rampage | None | None | None | None |
404 | Ford | Ranger | None | None | None | None |
405 | Chevy | S-10 | None | None | None | None |
406 rows × 6 columns
In [10]:
Copied!
df_cars["Make"] = tokens_series[0]
df_cars
df_cars["Make"] = tokens_series[0]
df_cars
Out[10]:
Car | MPG | Cylinders | Displacement | Horsepower | Weight | Acceleration | Model | Origin | Index | CarText | Make | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | Chevrolet Chevelle Malibu | 18.0 | 8 | 307.0 | 130.0 | 3504.0 | 12.0 | 70 | US | 0 | Chevrolet Chevelle Malibuwith a 0 8\n1 ... | Chevrolet |
1 | Buick Skylark 320 | 15.0 | 8 | 350.0 | 165.0 | 3693.0 | 11.5 | 70 | US | 1 | Buick Skylark 320with a 0 8\n1 8\n2 ... | Buick |
2 | Plymouth Satellite | 18.0 | 8 | 318.0 | 150.0 | 3436.0 | 11.0 | 70 | US | 2 | Plymouth Satellitewith a 0 8\n1 8\n2... | Plymouth |
3 | AMC Rebel SST | 16.0 | 8 | 304.0 | 150.0 | 3433.0 | 12.0 | 70 | US | 3 | AMC Rebel SSTwith a 0 8\n1 8\n2 ... | AMC |
4 | Ford Torino | 17.0 | 8 | 302.0 | 140.0 | 3449.0 | 10.5 | 70 | US | 4 | Ford Torinowith a 0 8\n1 8\n2 8... | Ford |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
401 | Ford Mustang GL | 27.0 | 4 | 140.0 | 86.0 | 2790.0 | 15.6 | 82 | US | 401 | Ford Mustang GLwith a 0 8\n1 8\n2 ... | Ford |
402 | Volkswagen Pickup | 44.0 | 4 | 97.0 | 52.0 | 2130.0 | 24.6 | 82 | Europe | 402 | Volkswagen Pickupwith a 0 8\n1 8\n2 ... | Volkswagen |
403 | Dodge Rampage | 32.0 | 4 | 135.0 | 84.0 | 2295.0 | 11.6 | 82 | US | 403 | Dodge Rampagewith a 0 8\n1 8\n2 ... | Dodge |
404 | Ford Ranger | 28.0 | 4 | 120.0 | 79.0 | 2625.0 | 18.6 | 82 | US | 404 | Ford Rangerwith a 0 8\n1 8\n2 8... | Ford |
405 | Chevy S-10 | 31.0 | 4 | 119.0 | 82.0 | 2720.0 | 19.4 | 82 | US | 405 | Chevy S-10with a 0 8\n1 8\n2 8\... | Chevy |
406 rows × 12 columns
Deleting rows¶
You can delete a row by using its index or delete multiple using a filter.
In [11]:
Copied!
df_cars.drop(index=2)
df_cars.drop(index=2)
Out[11]:
Car | MPG | Cylinders | Displacement | Horsepower | Weight | Acceleration | Model | Origin | Index | CarText | Make | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | Chevrolet Chevelle Malibu | 18.0 | 8 | 307.0 | 130.0 | 3504.0 | 12.0 | 70 | US | 0 | Chevrolet Chevelle Malibuwith a 0 8\n1 ... | Chevrolet |
1 | Buick Skylark 320 | 15.0 | 8 | 350.0 | 165.0 | 3693.0 | 11.5 | 70 | US | 1 | Buick Skylark 320with a 0 8\n1 8\n2 ... | Buick |
3 | AMC Rebel SST | 16.0 | 8 | 304.0 | 150.0 | 3433.0 | 12.0 | 70 | US | 3 | AMC Rebel SSTwith a 0 8\n1 8\n2 ... | AMC |
4 | Ford Torino | 17.0 | 8 | 302.0 | 140.0 | 3449.0 | 10.5 | 70 | US | 4 | Ford Torinowith a 0 8\n1 8\n2 8... | Ford |
5 | Ford Galaxie 500 | 15.0 | 8 | 429.0 | 198.0 | 4341.0 | 10.0 | 70 | US | 5 | Ford Galaxie 500with a 0 8\n1 8\n2 ... | Ford |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
401 | Ford Mustang GL | 27.0 | 4 | 140.0 | 86.0 | 2790.0 | 15.6 | 82 | US | 401 | Ford Mustang GLwith a 0 8\n1 8\n2 ... | Ford |
402 | Volkswagen Pickup | 44.0 | 4 | 97.0 | 52.0 | 2130.0 | 24.6 | 82 | Europe | 402 | Volkswagen Pickupwith a 0 8\n1 8\n2 ... | Volkswagen |
403 | Dodge Rampage | 32.0 | 4 | 135.0 | 84.0 | 2295.0 | 11.6 | 82 | US | 403 | Dodge Rampagewith a 0 8\n1 8\n2 ... | Dodge |
404 | Ford Ranger | 28.0 | 4 | 120.0 | 79.0 | 2625.0 | 18.6 | 82 | US | 404 | Ford Rangerwith a 0 8\n1 8\n2 8... | Ford |
405 | Chevy S-10 | 31.0 | 4 | 119.0 | 82.0 | 2720.0 | 19.4 | 82 | US | 405 | Chevy S-10with a 0 8\n1 8\n2 8\... | Chevy |
405 rows × 12 columns
In [15]:
Copied!
df_cars.drop(index=df_cars[df_cars["Cylinders"] == 8].index)
df_cars.drop(index=df_cars[df_cars["Cylinders"] == 8].index)
Out[15]:
Car | MPG | Cylinders | Displacement | Horsepower | Weight | Acceleration | Model | Origin | Index | CarText | Make | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
10 | Citroen DS-21 Pallas | 0.0 | 4 | 133.0 | 115.0 | 3090.0 | 17.5 | 70 | Europe | 10 | Citroen DS-21 Pallaswith a 0 8\n1 8\... | Citroen |
20 | Toyota Corolla Mark ii | 24.0 | 4 | 113.0 | 95.0 | 2372.0 | 15.0 | 70 | Japan | 20 | Toyota Corolla Mark iiwith a 0 8\n1 ... | Toyota |
21 | Plymouth Duster | 22.0 | 6 | 198.0 | 95.0 | 2833.0 | 15.5 | 70 | US | 21 | Plymouth Dusterwith a 0 8\n1 8\n2 ... | Plymouth |
22 | AMC Hornet | 18.0 | 6 | 199.0 | 97.0 | 2774.0 | 15.5 | 70 | US | 22 | AMC Hornetwith a 0 8\n1 8\n2 8\... | AMC |
23 | Ford Maverick | 21.0 | 6 | 200.0 | 85.0 | 2587.0 | 16.0 | 70 | US | 23 | Ford Maverickwith a 0 8\n1 8\n2 ... | Ford |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
401 | Ford Mustang GL | 27.0 | 4 | 140.0 | 86.0 | 2790.0 | 15.6 | 82 | US | 401 | Ford Mustang GLwith a 0 8\n1 8\n2 ... | Ford |
402 | Volkswagen Pickup | 44.0 | 4 | 97.0 | 52.0 | 2130.0 | 24.6 | 82 | Europe | 402 | Volkswagen Pickupwith a 0 8\n1 8\n2 ... | Volkswagen |
403 | Dodge Rampage | 32.0 | 4 | 135.0 | 84.0 | 2295.0 | 11.6 | 82 | US | 403 | Dodge Rampagewith a 0 8\n1 8\n2 ... | Dodge |
404 | Ford Ranger | 28.0 | 4 | 120.0 | 79.0 | 2625.0 | 18.6 | 82 | US | 404 | Ford Rangerwith a 0 8\n1 8\n2 8... | Ford |
405 | Chevy S-10 | 31.0 | 4 | 119.0 | 82.0 | 2720.0 | 19.4 | 82 | US | 405 | Chevy S-10with a 0 8\n1 8\n2 8\... | Chevy |
298 rows × 12 columns