I have two dataframes:
df1:
ID var1
1 Foo
2 Foo
3 Foo
4 Bar
df2:
ID var1
2 Foo
3 Bar
4 Bar
5 Foo
6 Bar
I have created a filter for df1 where var1 = 'Foo':
foo_filter=df1['var1']=='Foo'
Which when applied to df1 correctly returns:
df1_filtered=df1[foo_filter]
print(df1_filtered)
ID var1
1 Foo
2 Foo
3 Foo
I would like to then apply the same filter to the df2 with an output of this:
print(df2_filtered)
ID var1
2 Foo
3 Bar
However I am presented with this error:
df2_filtered=df2[foo_filter]
IndexingError: Unalignable boolean Series provided as indexer (index of the boolean Series and of the indexed object do not match
Is there any way to do this with filters or do I need a different method?
foo_filter=df2['var1']=='Foo'to apply to df2