using series to filter dataframe row
时间: 2024-05-15 10:16:38 浏览: 84
Series和DataFrame使用简单入门
To filter a dataframe row using a series, you can use the boolean indexing technique. Here's an example:
``` python
import pandas as pd
# create a sample dataframe
df = pd.DataFrame({'Name': ['John', 'Jane', 'Mike', 'Kate'],
'Age': [25, 30, 22, 28],
'Gender': ['M', 'F', 'M', 'F']})
# create a series to filter the rows
filter_series = pd.Series([True, False, True, False])
# filter the dataframe using the series
filtered_df = df[filter_series]
# print the filtered dataframe
print(filtered_df)
```
Output:
```
Name Age Gender
0 John 25 M
2 Mike 22 M
```
In this example, we created a sample dataframe with three columns - Name, Age, and Gender. We also created a series with boolean values to filter the rows. We then used boolean indexing to filter the rows of the dataframe based on the values in the series. The resulting filtered dataframe contains only the rows where the corresponding value in the series is True.
阅读全文