Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas Merge returning NaN?

Tags:

python

pandas

I have 2 dataframes:

df1=

State           Code
Alabama         AL
Alaska          AK
American Samoa  AS
Arizona         AZ
Arkansas        AR

df2=

State            PREVAILING_WAGE
CALIFORNIA       102170.0
DELAWARE         97531.0
SOUTH CAROLINA   96824.0
WASHINGTON       89586.0
OREGON           88234.0

Now when I try to merge both if them using:

df1.merge(df2,left_on='State',right_on='State',how='left')

I get NaN values in the merged table like this:

State          PREVAILING_WAGE   Code
CALIFORNIA     102170.0          NaN
DELAWARE       97531.0           NaN
SOUTH CAROLINA 96824.0           NaN
WASHINGTON     89586.0           NaN
OREGON         88234.0           NaN

I checked the dtypes too, and both of them are objects or str. How do I solve this?

Thanks

EDIT: The dataframes contain all states names, so they have common stuff to match.

like image 744
user517696 Avatar asked Nov 16 '25 20:11

user517696


1 Answers

If you use a left join, if df2.State value does not exist in df1.State, it will return NaN in Code, meaning there is not a match.

I will check if there is any extra white space after each string in the State columns, and make sure all values are upper cases and converted to str.

like image 190
Ray Avatar answered Nov 18 '25 09:11

Ray



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!