0

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?

6
  • foo_filter=df2['var1']=='Foo' to apply to df2 Commented Sep 26, 2019 at 14:30
  • I don't want to filter out the values of "Foo" in df2 Commented Sep 26, 2019 at 14:32
  • I want the rows in df2 with the same ID as those where var1 = 'Foo' in df1 Commented Sep 26, 2019 at 14:34
  • They have different indexes and different lengths - how would this work? :) Commented Sep 26, 2019 at 14:36
  • Well thats what I'm asking, I'm working on quite a cumbersome work around, but I wanted to know if I was missing something simple that would make it work Commented Sep 26, 2019 at 14:39

2 Answers 2

1

Only you need:

if ID is the index, putting ID as column:

df1.reset_index(inplace=True) #if ID is the index
df2.reset_index(inplace=True) #if ID is the index
filtered_df1=df1[df1['var1'].eq('Foo')]
print(filtered_df1)

    ID var1
0   1  Foo
1   2  Foo
2   3  Foo

df2.loc[df2['ID'].isin(filtered_df1['ID'])]

   ID var1
0   2  Foo
1   3  Bar
Sign up to request clarification or add additional context in comments.

Comments

0
df1 = pd.DataFrame({"id":[1,2,3,4], "var1":['Foo', 'Foo', 'Foo', 'Bar']})
df2 = pd.DataFrame({"id":[2,3,4,5, 6], "var1":['Foo','Bar','Bar','Foo', 'Bar']})

def filter(x):
    return x == 'Foo'

df1[df1['var1'].apply(filter)]
    id  var1
0   1   Foo
1   2   Foo
2   3   Foo



df2[df2['var1'].apply(filter)]
    id  var1
0   2   Foo
3   5   Foo

1 Comment

Thanks for taking the time but this does not produce what I want. I am looking for the the rows from df2 that have the same id as rows in df1 where var1='Foo' rather than the rows from df2 where var1='Foo'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.