Accessing using .loc[] [3/3]
Two chapters ago it was mentioned that boolean array can be used to access rows with the .loc[] property. It means you can filter rows without using if/else statements!
A condition you want to use for filtering must be passed as the first parameter. For example,
1234567# Importing library import pandas as pd # Reading csv file df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/67798cef-5e7c-4fbc-af7d-ae96b4443c0a/audi.csv') # Get the cars manufactured before 2000 print(df.loc[df.year < 2000])
If you want to filter based on several conditions, you need to put them within separate parentheses, and combine them by either the & (and), or | (or) operator. You use the & operator if you need all the conditions to be hold. If you are interested in at least one of the conditions, use the | operator.
1234567# Importing library import pandas as pd # Reading csv file df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/67798cef-5e7c-4fbc-af7d-ae96b4443c0a/audi.csv') # Get the Diesel cars cheaper than 15k print(df.loc[(df.fuelType == 'Diesel') & (df.price < 15000)])
Bedankt voor je feedback!
Vraag AI
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.
Stel mij vragen over dit onderwerp
Vat dit hoofdstuk samen
Toon voorbeelden uit de praktijk
Awesome!
Completion rate improved to 3.33
Accessing using .loc[] [3/3]
Veeg om het menu te tonen
Two chapters ago it was mentioned that boolean array can be used to access rows with the .loc[] property. It means you can filter rows without using if/else statements!
A condition you want to use for filtering must be passed as the first parameter. For example,
1234567# Importing library import pandas as pd # Reading csv file df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/67798cef-5e7c-4fbc-af7d-ae96b4443c0a/audi.csv') # Get the cars manufactured before 2000 print(df.loc[df.year < 2000])
If you want to filter based on several conditions, you need to put them within separate parentheses, and combine them by either the & (and), or | (or) operator. You use the & operator if you need all the conditions to be hold. If you are interested in at least one of the conditions, use the | operator.
1234567# Importing library import pandas as pd # Reading csv file df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/67798cef-5e7c-4fbc-af7d-ae96b4443c0a/audi.csv') # Get the Diesel cars cheaper than 15k print(df.loc[(df.fuelType == 'Diesel') & (df.price < 15000)])
Bedankt voor je feedback!