0

I have two dataframe as listed below. It was generated using pandas.

df1

                    0

0       reallocations
1                four
2              payoff

df2

word             frequency

whether          1
House            1
Sniderman        1
payoff           6

My goal here is to read df1[0] and check if it exists in df2[word] if it exists then give me an output in the following format.

word             frequency

four             0    
whether          1
House            1
Sniderman        1
reallocations    0
four             0
payoff           6

Here is what I have tried: df1.intersection(df2). I believe I have to assign a column value for that.

I tried solution from Compare pandas dataframes by multiple columns

I am pretty sure there is a small thing that not allowing me to concatenate the desired result.

Any thoughts?

1 Answer 1

0

You need an outer join:

df1.rename(columns={'0': 'word'}).merge(df2, how='outer').fillna(0)
# or df1.rename(columns={0: 'word'}).merge(df2, how='outer').fillna(0) if column names in df1
# is a number

#   word            frequency
#0  reallocations   0.0
#1  four            0.0
#2  payoff          6.0
#3  whether         1.0
#4  House           1.0
#5  Sniderman       1.0
Sign up to request clarification or add additional context in comments.

1 Comment

Cool. Glad it helps.

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.