Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ValueError while doing merge with DataFrames using Pandas

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.

like image 441
Rashida Avatar asked Oct 24 '25 12:10

Rashida


1 Answers

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.

like image 90
Toby Petty Avatar answered Oct 26 '25 03:10

Toby Petty