0% found this document useful (0 votes)
8 views19 pages

Pandas Filtering

The document provides an extensive overview of filtering techniques in pandas, including basic filtering with boolean indexing, using multiple conditions, and filtering with methods like isin() and query(). It also covers how to filter rows and columns using loc and iloc, as well as string methods and custom functions for more complex filtering. Additionally, the document includes practice questions for each filtering technique to enhance understanding.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views19 pages

Pandas Filtering

The document provides an extensive overview of filtering techniques in pandas, including basic filtering with boolean indexing, using multiple conditions, and filtering with methods like isin() and query(). It also covers how to filter rows and columns using loc and iloc, as well as string methods and custom functions for more complex filtering. Additionally, the document includes practice questions for each filtering technique to enhance understanding.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Sure!

Filtering in pandas is a powerful way to select specific rows or


columns from a DataFrame based on certain conditions. Let's go through
some common filtering techniques.

1. Basic Filtering with Boolean Indexing

You can filter rows by applying a condition that returns a boolean Series.

import pandas as pd
# Sample DataFrame
data = {
'Name': ['Alice', 'Bob', 'Charlie', 'David'],
'Age': [25, 30, 35, 40],
'City': ['New York', 'Los Angeles', 'Chicago', 'Houston']
}
df = pd.DataFrame(data)

# Filter rows where Age is greater than 30


filtered_df = df[df['Age'] > 30]
print(filtered_df)
Output:

Name Age City

2 Charlie 35 Chicago

3 David 40 Houston

2. Multiple Conditions

You can combine multiple conditions using & (and), | (or), and ~ (not).

# Filter rows where Age is greater than 30 and City is 'Chicago'


filtered_df = df[(df['Age'] > 30) & (df['City'] == 'Chicago')]
print(filtered_df)
Output:

Name Age City


2 Charlie 35 Chicago

3. Filtering with isin()

You can filter rows where a column's value is in a list of values.

# Filter rows where City is either 'New York' or 'Houston'


filtered_df = df[df['City'].isin(['New York', 'Houston'])]
print(filtered_df)
Output:
Name Age City
0 Alice 25 New York
3 David 40 Houston

4. Filtering with query() Method

The query() method allows you to filter rows using a query string.

# Filter rows where Age is greater than 30


filtered_df = df.query('Age > 30')
print(filtered_df)
Output:

Name Age City


2 Charlie 35 Chicago
3 David 40 Houston

5. Filtering Columns

You can also filter columns by selecting specific columns.

# Select only the 'Name' and 'City' columns


filtered_df = df[['Name', 'City']]
print(filtered_df)
Output:

Name City
0 Alice New York
1 Bob Los Angeles
2 Charlie Chicago
3 David Houston

6. Filtering with loc and iloc

 loc is used for label-based indexing.

 iloc is used for integer-based indexing.

# Using loc to filter rows and columns


filtered_df = df.loc[df['Age'] > 30, ['Name', 'City']]
print(filtered_df)
Output:

Name City
2 Charlie Chicago
3 David Houston

# Using iloc to filter rows and columns by index


filtered_df = df.iloc[1:3, 0:2] # Rows 1 to 2 and columns 0 to 1
print(filtered_df)
Output:

Name Age
1 Bob 30
2 Charlie 35

7. Filtering with str Methods

You can filter rows based on string operations.

# Filter rows where the Name starts with 'A'


filtered_df = df[df['Name'].str.startswith('A')]
print(filtered_df)
Output:

Name Age City


0 Alice 25 New York

8. Filtering with apply()

You can apply a custom function to filter rows.

# Filter rows where the length of the Name is greater than 5


filtered_df = df[df['Name'].apply(lambda x: len(x) > 5)]
print(filtered_df)
Output:

Name Age City


2 Charlie 35 Chicago

Summary

 Boolean indexing is the most common way to filter rows.

 Multiple conditions can be combined using &, |, and ~.

 isin() is useful for filtering based on a list of values.

 query() provides a SQL-like syntax for filtering.

 loc and iloc are used for label-based and integer-based indexing,
respectively.

 String methods and apply() allow for more complex filtering.

Let me know if you need further clarification or more examples!


Sure! Below are 10 examples for each filtering technique in pandas.
I'll cover the most common filtering methods to give you a comprehensive
understanding.

1. Basic Filtering with Boolean Indexing

Filter rows based on a condition.

import pandas as pd

# Sample DataFrame
data = {
'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Eva', 'Frank', 'Grace', 'Hank',
'Ivy', 'Jack'],
'Age': [25, 30, 35, 40, 45, 50, 55, 60, 65, 70],
'City': ['New York', 'Los Angeles', 'Chicago', 'Houston', 'Phoenix',
'Philadelphia', 'San Antonio', 'San Diego', 'Dallas', 'San Jose']
}
df = pd.DataFrame(data)

# Example 1: Filter rows where Age > 30


print(df[df['Age'] > 30])

# Example 2: Filter rows where City is 'Chicago'


print(df[df['City'] == 'Chicago'])

# Example 3: Filter rows where Name starts with 'A'


print(df[df['Name'].str.startswith('A')])

# Example 4: Filter rows where Age is between 30 and 50


print(df[(df['Age'] >= 30) & (df['Age'] <= 50)])

# Example 5: Filter rows where City is not 'New York'


print(df[df['City'] != 'New York'])

# Example 6: Filter rows where Name contains 'a'


print(df[df['Name'].str.contains('a')])

# Example 7: Filter rows where Age is even


print(df[df['Age'] % 2 == 0])

# Example 8: Filter rows where City is in a list of cities


print(df[df['City'].isin(['Chicago', 'Houston', 'San Diego'])])
# Example 9: Filter rows where Name length is greater than 4
print(df[df['Name'].apply(lambda x: len(x) > 4)])

# Example 10: Filter rows where Age is greater than the mean age
print(df[df['Age'] > df['Age'].mean()])

2. Multiple Conditions

Combine multiple conditions using & (and), | (or), and ~ (not).

# Example 1: Filter rows where Age > 30 and City is 'Chicago'


print(df[(df['Age'] > 30) & (df['City'] == 'Chicago')])

# Example 2: Filter rows where Age < 40 or City is 'Houston'


print(df[(df['Age'] < 40) | (df['City'] == 'Houston')])

# Example 3: Filter rows where Age is not 30 and City is not 'New York'
print(df[(df['Age'] != 30) & ~(df['City'] == 'New York')])

# Example 4: Filter rows where Age is between 30 and 50 and City is not
'Phoenix'
print(df[(df['Age'] >= 30) & (df['Age'] <= 50) & (df['City'] != 'Phoenix')])

# Example 5: Filter rows where Name starts with 'A' or 'B'


print(df[df['Name'].str.startswith('A') | df['Name'].str.startswith('B')])

# Example 6: Filter rows where Age is even and City starts with 'S'
print(df[(df['Age'] % 2 == 0) & (df['City'].str.startswith('S'))])

# Example 7: Filter rows where Name contains 'a' and Age > 40
print(df[df['Name'].str.contains('a') & (df['Age'] > 40)])

# Example 8: Filter rows where City is 'Chicago' or 'Houston' and Age > 30
print(df[df['City'].isin(['Chicago', 'Houston']) & (df['Age'] > 30)])

# Example 9: Filter rows where Name length is greater than 4 and Age is
odd
print(df[(df['Name'].apply(lambda x: len(x) > 4)) & (df['Age'] % 2 != 0)])

# Example 10: Filter rows where Age is greater than the mean age and
City is not 'San Jose'
print(df[(df['Age'] > df['Age'].mean()) & (df['City'] != 'San Jose')])

3. Filtering with isin()

Filter rows where a column's value is in a list.


# Example 1: Filter rows where City is 'Chicago' or 'Houston'
print(df[df['City'].isin(['Chicago', 'Houston'])])

# Example 2: Filter rows where Name is 'Alice' or 'Bob'


print(df[df['Name'].isin(['Alice', 'Bob'])])

# Example 3: Filter rows where Age is in [30, 40, 50]


print(df[df['Age'].isin([30, 40, 50])])

# Example 4: Filter rows where City starts with 'S'


print(df[df['City'].str.startswith('S')])

# Example 5: Filter rows where Name ends with 'e'


print(df[df['Name'].str.endswith('e')])

# Example 6: Filter rows where City contains 'San'


print(df[df['City'].str.contains('San')])

# Example 7: Filter rows where Age is in the top 3 ages


print(df[df['Age'].isin(df['Age'].nlargest(3))])

# Example 8: Filter rows where Name is in a list of names


print(df[df['Name'].isin(['Charlie', 'Eva', 'Grace'])])

# Example 9: Filter rows where City is not in ['New York', 'Los Angeles']
print(df[~df['City'].isin(['New York', 'Los Angeles'])])

# Example 10: Filter rows where Age is in a range of values


print(df[df['Age'].isin(range(30, 51))])

4. Filtering with query()

Use SQL-like syntax to filter rows.

# Example 1: Filter rows where Age > 30


print(df.query('Age > 30'))

# Example 2: Filter rows where City == "Chicago"


print(df.query('City == "Chicago"'))

# Example 3: Filter rows where Name starts with 'A'


print(df.query('Name.str.startswith("A")', engine='python'))

# Example 4: Filter rows where Age is between 30 and 50


print(df.query('30 <= Age <= 50'))

# Example 5: Filter rows where City is not 'New York'


print(df.query('City != "New York"'))
# Example 6: Filter rows where Name contains 'a'
print(df.query('Name.str.contains("a")', engine='python'))

# Example 7: Filter rows where Age is even


print(df.query('Age % 2 == 0'))

# Example 8: Filter rows where City is in a list of cities


print(df.query('City in ["Chicago", "Houston", "San Diego"]'))

# Example 9: Filter rows where Name length is greater than 4


print(df.query('Name.str.len() > 4', engine='python'))

# Example 10: Filter rows where Age is greater than the mean age
print(df.query('Age > Age.mean()'))

5. Filtering with loc and iloc

Filter rows and columns using labels or indices.

# Example 1: Filter rows where Age > 30 and select Name and City
columns
print(df.loc[df['Age'] > 30, ['Name', 'City']])

# Example 2: Filter rows where City is 'Chicago' and select all columns
print(df.loc[df['City'] == 'Chicago', :])

# Example 3: Filter rows by index (rows 2 to 5)


print(df.iloc[2:6])

# Example 4: Filter rows by index and select specific columns (columns 0


and 2)
print(df.iloc[2:6, [0, 2]])

# Example 5: Filter rows where Age > 30 and select columns by name
print(df.loc[df['Age'] > 30, 'Name':'City'])

# Example 6: Filter rows where Name starts with 'A' and select the first 3
columns
print(df.loc[df['Name'].str.startswith('A'), :3])

# Example 7: Filter rows by index and select all columns


print(df.iloc[[1, 3, 5], :])

# Example 8: Filter rows where Age is even and select specific columns
print(df.loc[df['Age'] % 2 == 0, ['Name', 'Age']])

# Example 9: Filter rows by index and select columns by index


print(df.iloc[[0, 2, 4], [0, 1]])
# Example 10: Filter rows where City is 'San Diego' and select all columns
print(df.loc[df['City'] == 'San Diego', :])

Let me know if you need further clarification or more examples!


Sure! Below are 20 practice questions for each filtering technique in
pandas. I'll first provide the questions, followed by the answers.

1. Basic Filtering with Boolean Indexing

1. Filter rows where Age is greater than 30.

2. Filter rows where City is "Chicago".

3. Filter rows where Name starts with 'A'.

4. Filter rows where Age is between 30 and 50.

5. Filter rows where City is not "New York".

6. Filter rows where Name contains the letter 'a'.

7. Filter rows where Age is even.

8. Filter rows where City is in the list ['Chicago', 'Houston', 'San Diego'].

9. Filter rows where the length of Name is greater than 4.

10. Filter rows where Age is greater than the mean age.

11. Filter rows where City starts with 'S'.

12. Filter rows where Name ends with 'e'.

13. Filter rows where Age is less than 40 and City is "Los Angeles".

14. Filter rows where Name contains 'a' and Age is greater than
40.

15. Filter rows where City is not in ['New York', 'Los Angeles'].

16. Filter rows where Age is odd.

17. Filter rows where Name is exactly 5 characters long.

18. Filter rows where City contains 'San'.

19. Filter rows where Age is in the top 3 highest ages.

20. Filter rows where Name is in the list ['Alice', 'Bob', 'Charlie'].

2. Multiple Conditions

1. Filter rows where Age > 30 and City is "Chicago".

2. Filter rows where Age < 40 or City is "Houston".

3. Filter rows where Age is not 30 and City is not "New York".
4. Filter rows where Age is between 30 and 50 and City is not
"Phoenix".

5. Filter rows where Name starts with 'A' or 'B'.

6. Filter rows where Age is even and City starts with 'S'.

7. Filter rows where Name contains 'a' and Age > 40.

8. Filter rows where City is "Chicago" or "Houston" and Age > 30.

9. Filter rows where Name length is greater than 4 and Age is odd.

10. Filter rows where Age > mean age and City is not "San Jose".

11. Filter rows where City is "New York" or "Los Angeles"


and Age < 50.

12. Filter rows where Name contains 'a' or 'e' and Age > 30.

13. Filter rows where City is not "Chicago" and Age is even.

14. Filter rows where Name starts with 'C' or 'D' and Age > 35.

15. Filter rows where City is "San Diego" or "San Jose" and Age <
60.

16. Filter rows where Name length is 5 and Age is between 30 and
50.

17. Filter rows where City contains 'San' and Age > 40.

18. Filter rows where Name ends with 'e' and Age is odd.

19. Filter rows where City is not in ['New York', 'Los


Angeles'] and Age > 30.

20. Filter rows where Name is in ['Alice', 'Bob',


'Charlie'] and Age < 50.

3. Filtering with isin()

1. Filter rows where City is in ['Chicago', 'Houston'].

2. Filter rows where Name is in ['Alice', 'Bob'].

3. Filter rows where Age is in [30, 40, 50].

4. Filter rows where City starts with 'S'.

5. Filter rows where Name ends with 'e'.

6. Filter rows where City contains 'San'.


7. Filter rows where Age is in the top 3 highest ages.

8. Filter rows where Name is in ['Charlie', 'Eva', 'Grace'].

9. Filter rows where City is not in ['New York', 'Los Angeles'].

10. Filter rows where Age is in the range [30, 50].

11. Filter rows where Name is in ['Alice', 'Bob', 'Charlie', 'David'].

12. Filter rows where City is in ['San Diego', 'San Jose'].

13. Filter rows where Age is in [25, 35, 45, 55].

14. Filter rows where Name is in ['Eva', 'Frank', 'Grace'].

15. Filter rows where City is in ['Chicago', 'Houston', 'Phoenix'].

16. Filter rows where Age is in [40, 50, 60].

17. Filter rows where Name is in ['Alice', 'Charlie', 'Eva'].

18. Filter rows where City is in ['New York', 'Los Angeles',


'Chicago'].

19. Filter rows where Age is in [30, 40, 50, 60].

20. Filter rows where Name is in ['Bob', 'David', 'Frank'].

4. Filtering with query()

1. Filter rows where Age > 30.

2. Filter rows where City == "Chicago".

3. Filter rows where Name starts with 'A'.

4. Filter rows where Age is between 30 and 50.

5. Filter rows where City != "New York".

6. Filter rows where Name contains 'a'.

7. Filter rows where Age is even.

8. Filter rows where City is in ['Chicago', 'Houston', 'San Diego'].

9. Filter rows where Name length is greater than 4.

10. Filter rows where Age > mean age.

11. Filter rows where City starts with 'S'.

12. Filter rows where Name ends with 'e'.


13. Filter rows where City contains 'San'.

14. Filter rows where Age is in the top 3 highest ages.

15. Filter rows where Name is in ['Alice', 'Bob', 'Charlie'].

16. Filter rows where City is not in ['New York', 'Los Angeles'].

17. Filter rows where Age is odd.

18. Filter rows where Name is exactly 5 characters long.

19. Filter rows where City contains 'San' and Age > 40.

20. Filter rows where Name is in ['Alice', 'Bob',


'Charlie'] and Age < 50.

5. Filtering with loc and iloc

1. Filter rows where Age > 30 and select Name and City columns.

2. Filter rows where City is "Chicago" and select all columns.

3. Filter rows by index (rows 2 to 5).

4. Filter rows by index and select specific columns (columns 0 and 2).

5. Filter rows where Age > 30 and select columns Name to City.

6. Filter rows where Name starts with 'A' and select the first 3 columns.

7. Filter rows by index [1, 3, 5] and select all columns.

8. Filter rows where Age is even and select Name and Age columns.

9. Filter rows by index [0, 2, 4] and select columns [0, 1].

10. Filter rows where City is "San Diego" and select all columns.

11. Filter rows where Age < 40 and select Name and Age columns.

12. Filter rows by index [3, 6, 9] and select columns [1, 2].

13. Filter rows where Name contains 'a' and


select Name and City columns.

14. Filter rows by index [2, 4, 6] and select all columns.

15. Filter rows where Age > 50 and select Name and Age columns.

16. Filter rows by index [0, 1, 2] and select columns [0, 2].

17. Filter rows where City is "Houston" and


select Name and City columns.
18. Filter rows by index [5, 7, 9] and select columns [1, 2].

19. Filter rows where Age is odd and


select Name and Age columns.

20. Filter rows by index [4, 6, 8] and select all columns.

Answers

Let me know when you're ready for the answers, and I'll provide them! 😊
Sure! Below are the answers to the 20 questions for each filtering
technique.

1. Basic Filtering with Boolean Indexing

python

Copy

import pandas as pd

# Sample DataFrame

data = {

'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Eva', 'Frank', 'Grace', 'Hank',


'Ivy', 'Jack'],

'Age': [25, 30, 35, 40, 45, 50, 55, 60, 65, 70],

'City': ['New York', 'Los Angeles', 'Chicago', 'Houston', 'Phoenix',


'Philadelphia', 'San Antonio', 'San Diego', 'Dallas', 'San Jose']

df = pd.DataFrame(data)

# Answers

print(df[df['Age'] > 30]) # 1

print(df[df['City'] == 'Chicago']) # 2

print(df[df['Name'].str.startswith('A')]) # 3

print(df[(df['Age'] >= 30) & (df['Age'] <= 50)]) # 4

print(df[df['City'] != 'New York']) # 5

print(df[df['Name'].str.contains('a')]) # 6

print(df[df['Age'] % 2 == 0]) # 7

print(df[df['City'].isin(['Chicago', 'Houston', 'San Diego'])]) # 8

print(df[df['Name'].apply(lambda x: len(x) > 4)]) # 9

print(df[df['Age'] > df['Age'].mean()]) # 10

print(df[df['City'].str.startswith('S')]) # 11
print(df[df['Name'].str.endswith('e')]) # 12

print(df[(df['Age'] < 40) & (df['City'] == 'Los Angeles')]) # 13

print(df[df['Name'].str.contains('a') & (df['Age'] > 40)]) # 14

print(df[~df['City'].isin(['New York', 'Los Angeles'])]) # 15

print(df[df['Age'] % 2 != 0]) # 16

print(df[df['Name'].apply(lambda x: len(x) == 5)]) # 17

print(df[df['City'].str.contains('San')]) # 18

print(df[df['Age'].isin(df['Age'].nlargest(3))]) # 19

print(df[df['Name'].isin(['Alice', 'Bob', 'Charlie'])]) # 20

2. Multiple Conditions

python

Copy

# Answers

print(df[(df['Age'] > 30) & (df['City'] == 'Chicago')]) # 1

print(df[(df['Age'] < 40) | (df['City'] == 'Houston')]) # 2

print(df[(df['Age'] != 30) & ~(df['City'] == 'New York')]) # 3

print(df[(df['Age'] >= 30) & (df['Age'] <= 50) & (df['City'] != 'Phoenix')])
#4

print(df[df['Name'].str.startswith('A') | df['Name'].str.startswith('B')]) # 5

print(df[(df['Age'] % 2 == 0) & (df['City'].str.startswith('S'))]) # 6

print(df[df['Name'].str.contains('a') & (df['Age'] > 40)]) # 7

print(df[df['City'].isin(['Chicago', 'Houston']) & (df['Age'] > 30)]) # 8

print(df[(df['Name'].apply(lambda x: len(x) > 4)) & (df['Age'] % 2 != 0)])


#9

print(df[(df['Age'] > df['Age'].mean()) & (df['City'] != 'San Jose')]) # 10

print(df[(df['City'].isin(['New York', 'Los Angeles'])) & (df['Age'] < 50)]) #


11

print(df[(df['Name'].str.contains('a') | df['Name'].str.contains('e')) &


(df['Age'] > 30)]) # 12
print(df[(df['City'] != 'Chicago') & (df['Age'] % 2 == 0)]) # 13

print(df[(df['Name'].str.startswith('C') | df['Name'].str.startswith('D')) &


(df['Age'] > 35)]) # 14

print(df[(df['City'].isin(['San Diego', 'San Jose'])) & (df['Age'] < 60)]) # 15

print(df[(df['Name'].apply(lambda x: len(x) == 5)) &


(df['Age'].between(30, 50))]) # 16

print(df[(df['City'].str.contains('San')) & (df['Age'] > 40)]) # 17

print(df[(df['Name'].str.endswith('e')) & (df['Age'] % 2 != 0)]) # 18

print(df[~df['City'].isin(['New York', 'Los Angeles']) & (df['Age'] > 30)]) #


19

print(df[df['Name'].isin(['Alice', 'Bob', 'Charlie']) & (df['Age'] < 50)]) # 20

3. Filtering with isin()

python

Copy

# Answers

print(df[df['City'].isin(['Chicago', 'Houston'])]) # 1

print(df[df['Name'].isin(['Alice', 'Bob'])]) # 2

print(df[df['Age'].isin([30, 40, 50])]) # 3

print(df[df['City'].str.startswith('S')]) # 4

print(df[df['Name'].str.endswith('e')]) # 5

print(df[df['City'].str.contains('San')]) # 6

print(df[df['Age'].isin(df['Age'].nlargest(3))]) # 7

print(df[df['Name'].isin(['Charlie', 'Eva', 'Grace'])]) # 8

print(df[~df['City'].isin(['New York', 'Los Angeles'])]) # 9

print(df[df['Age'].isin(range(30, 51))]) # 10

print(df[df['Name'].isin(['Alice', 'Bob', 'Charlie', 'David'])]) # 11

print(df[df['City'].isin(['San Diego', 'San Jose'])]) # 12

print(df[df['Age'].isin([25, 35, 45, 55])]) # 13

print(df[df['Name'].isin(['Eva', 'Frank', 'Grace'])]) # 14


print(df[df['City'].isin(['Chicago', 'Houston', 'Phoenix'])]) # 15

print(df[df['Age'].isin([40, 50, 60])]) # 16

print(df[df['Name'].isin(['Alice', 'Charlie', 'Eva'])]) # 17

print(df[df['City'].isin(['New York', 'Los Angeles', 'Chicago'])]) # 18

print(df[df['Age'].isin([30, 40, 50, 60])]) # 19

print(df[df['Name'].isin(['Bob', 'David', 'Frank'])]) # 20

4. Filtering with query()

python

Copy

# Answers

print(df.query('Age > 30')) # 1

print(df.query('City == "Chicago"')) # 2

print(df.query('Name.str.startswith("A")', engine='python')) # 3

print(df.query('30 <= Age <= 50')) # 4

print(df.query('City != "New York"')) # 5

print(df.query('Name.str.contains("a")', engine='python')) # 6

print(df.query('Age % 2 == 0')) # 7

print(df.query('City in ["Chicago", "Houston", "San Diego"]')) # 8

print(df.query('Name.str.len() > 4', engine='python')) # 9

print(df.query('Age > Age.mean()')) # 10

print(df.query('City.str.startswith("S")', engine='python')) # 11

print(df.query('Name.str.endswith("e")', engine='python')) # 12

print(df.query('City.str.contains("San")', engine='python')) # 13

print(df.query('Age in @df.Age.nlargest(3)')) # 14

print(df.query('Name in ["Alice", "Bob", "Charlie"]')) # 15

print(df.query('City not in ["New York", "Los Angeles"]')) # 16

print(df.query('Age % 2 != 0')) # 17

print(df.query('Name.str.len() == 5', engine='python')) # 18


print(df.query('City.str.contains("San") and Age > 40', engine='python'))
# 19

print(df.query('Name in ["Alice", "Bob", "Charlie"] and Age < 50')) # 20

5. Filtering with loc and iloc

python

Copy

# Answers

print(df.loc[df['Age'] > 30, ['Name', 'City']]) # 1

print(df.loc[df['City'] == 'Chicago', :]) # 2

print(df.iloc[2:6]) # 3

print(df.iloc[2:6, [0, 2]]) # 4

print(df.loc[df['Age'] > 30, 'Name':'City']) # 5

print(df.loc[df['Name'].str.startswith('A'), :3]) # 6

print(df.iloc[[1, 3, 5], :]) # 7

print(df.loc[df['Age'] % 2 == 0, ['Name', 'Age']]) # 8

print(df.iloc[[0, 2, 4], [0, 1]]) # 9

print(df.loc[df['City'] == 'San Diego', :]) # 10

print(df.loc[df['Age'] < 40, ['Name', 'Age']]) # 11

print(df.iloc[[3, 6, 9], [1, 2]]) # 12

print(df.loc[df['Name'].str.contains('a'), ['Name', 'City']]) # 13

print(df.iloc[[2, 4, 6], :]) # 14

print(df.loc[df['Age'] > 50, ['Name', 'Age']]) # 15

print(df.iloc[[0, 1, 2], [0, 2]]) # 16

print(df.loc[df['City'] == 'Houston', ['Name', 'City']]) # 17

print(df.iloc[[5, 7, 9], [1, 2]]) # 18

print(df.loc[df['Age'] % 2 != 0, ['Name', 'Age']]) # 19

print(df.iloc[[4, 6, 8], :]) # 20


Let me know if you need further clarification! 😊

You might also like