Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter Pandas dataframe with another series

I have Pandas Series we'll call approved_fields which I'd like to use to filter a df by:

approved_field(['Field1','Field2','Field3')]

df
    Field
0   Field1
1   Field4
2   Field2
3   Field5
4   Field2

After applying the approved_field filter, the resulting df should look like:

    Field
0   Field1
1   Field2
2   Field2

Thanks!

like image 835
ChrisArmstrong Avatar asked Jun 11 '13 14:06

ChrisArmstrong


People also ask

How do you combine two series in a data frame?

append() to Combine Two Series. You can use pandas. DataFrame(Series. append(Series,ignore_index=True)) to create a DataFrame by appending series to another series.

How do you filter a DataFrame based on a condition?

Filter Rows by Condition You can use df[df["Courses"] == 'Spark'] to filter rows by a condition in pandas DataFrame. Not that this expression returns a new DataFrame with selected rows. You can also write the above statement with a variable.

How do you filter a series?

filter() function to filter out some values in the given series object using a regular expressions. Output : Now we will use Series. filter() function to filter those values from the given series object whose index label name has a space in its name.


1 Answers

You can use isin and boolean indexing:

>>> import pandas as pd
>>> df = pd.DataFrame({"Field": "Field1 Field4 Field2 Field5 Field2".split()})
>>> approved_fields = "Field1", "Field2", "Field3"
>>> df['Field'].isin(approved_fields)
0     True
1    False
2     True
3    False
4     True
Name: Field, dtype: bool
>>> df[df['Field'].isin(approved_fields)]
    Field
0  Field1
2  Field2
4  Field2
like image 58
DSM Avatar answered Oct 19 '22 23:10

DSM