Filtering DataFrame rows by column values in Pandas using NumPy array
Extract rows from a Pandas DataFrame where a specific column's values are in a given NumPy array.
Sample Solution:
Python Code:
Output:
Name Age Salary 0 Teodosija 25 50000 3 David 35 70000
Explanation:
In the exerciser above -
- First create a sample DataFrame (df) with columns 'Name', 'Age', and 'Salary'.
- We define a NumPy array selected_age_values containing the values we want to filter by in the 'Age' column.
- The df['Age'].isin(selected_age_values) condition creates a boolean Series, and boolean indexing is used to extract rows where the condition is True.
- The resulting DataFrame (selected_rows) contains only rows where the 'Age' column values are in the specified NumPy array.
Flowchart:

Python Code Editor:
Previous: Merging DataFrames based on a common column in Pandas.
Next: Performing element-wise addition in Pandas DataFrame with NumPy array.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.