Combining Your Knowledge
If you remember, several chapters ago you were provided with information on how to write several conditions simultaneously. With the .isin()
statement, you can use the same rules. For instance, an example from the previous chapter could look like this:
# The initial example
import pandas as pd
data = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/4bf24830-59ba-4418-969b-aaf8117d522e/cars.csv', index_col = 0)
models = ['HONDA', 'FORD', 'MERCEDES-BENZ', 'HYUNDAI']
data_extracted = data.loc[data['Manufacturer'].isin(models)]
print(data_extracted.head())
# The modified example
import pandas as pd
data = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/4bf24830-59ba-4418-969b-aaf8117d522e/cars.csv', index_col = 0)
models = ['HONDA', 'FORD', 'MERCEDES-BENZ', 'HYUNDAI']
condition = data['Manufacturer'].isin(models)
data_extracted = data.loc[condition]
print(data_extracted.head())
The output in these two cases will be the same.
Takk for tilbakemeldingene dine!
Spør AI
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår
Awesome!
Completion rate improved to 3.03
Combining Your Knowledge
Sveip for å vise menyen
If you remember, several chapters ago you were provided with information on how to write several conditions simultaneously. With the .isin()
statement, you can use the same rules. For instance, an example from the previous chapter could look like this:
# The initial example
import pandas as pd
data = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/4bf24830-59ba-4418-969b-aaf8117d522e/cars.csv', index_col = 0)
models = ['HONDA', 'FORD', 'MERCEDES-BENZ', 'HYUNDAI']
data_extracted = data.loc[data['Manufacturer'].isin(models)]
print(data_extracted.head())
# The modified example
import pandas as pd
data = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/4bf24830-59ba-4418-969b-aaf8117d522e/cars.csv', index_col = 0)
models = ['HONDA', 'FORD', 'MERCEDES-BENZ', 'HYUNDAI']
condition = data['Manufacturer'].isin(models)
data_extracted = data.loc[condition]
print(data_extracted.head())
The output in these two cases will be the same.
Takk for tilbakemeldingene dine!