4

I am trying to merge three DataFrames using Pandas. Why do I get this error message?

ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

This is the code:

df=pd.merge(energy,GDP,ScimEn,on="Country")

Here energy,GDP,ScimEn are the three DataFrames I was trying to merge.

1
  • Welcome to SO. You can take a look at the documentation of merge. You can merge exactly two data frames using pd.merge Commented Feb 28, 2019 at 16:44

1 Answer 1

5

The call signature of pd.merge accepts only 2 DataFrames to merge;

pd.merge(left, right, how='inner', on=None, left_on=None, right_on=None, left_index=False, right_index=False, sort=False, suffixes=('_x', '_y'), copy=True, indicator=False, validate=None)

So you'd need to do it in 2 steps:

df = pd.merge(energy, GDP, on="Country")
df = pd.merge(df, ScimEn, on="Country")

Or chain the 2 steps together with DataFrame.merge which allows for the immediate use of the new DataFrame:

df = energy.merge(GDP, on="Country").merge(ScimEn, on="Country")

Also it's a good idea to set the how keyword explicitly. If there are countries that are in one of the DataFrames but not in the others, then merging using the default how=inner would result in lost data, and should be changed to outer if you want to keep all data from all DataFrames.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.