pandas filter list
时间: 2023-10-01 17:07:48 浏览: 64
十分钟搞定pandas
To filter a list in pandas, you can use the `isin()` method to select only the elements that match a certain criteria. Here is an example:
```
import pandas as pd
# create a list of numbers
numbers = [1, 2, 3, 4, 5]
# create a pandas series from the list
s = pd.Series(numbers)
# filter the series to only include even numbers
filtered = s[s.isin([2, 4])]
print(filtered)
```
Output:
```
1 2
3 4
dtype: int64
```
In this example, we create a list of numbers and convert it to a pandas series using the `pd.Series()` function. We then use the `isin()` method to select only the even numbers (2 and 4) and return a new series containing only those values. The resulting output is a series with the values 2 and 4.
阅读全文