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!
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With