0

I'm trying to convert U.S. geolocation codes for states, counties and cities. The problem is, the county and city codes are duplicated -- meaning, multiple states have counties and cities with the same code. And I'm dealing with separate dataframes.

The main df has rows that look like this:

State   CountyCode  CityCode   Data
Alabama 61          2720       3

And I've put the downloaded codes into a separate df2 that looks like this:

State    CountyCode  County  CityCode  City
Alabama  61          Henry   2720      Albertville

What I want is a df that looks like this:

State    CountyCode  County   CityCode    City          Data
Alabama  61          Henry    2720        Albertville   3

My question is how to do this. I've put the rows in df2 into a series, dictionary, list. I've tried map() and apply() and iterating through df, but nothing has quite worked. For one thing, to convert the county and city codes, I need the state as well as the local name. I assume there is a better way to do this, conceptually, and that I'm just not aware of it. Many thanks for any help.

  • What exactly is the issue? question? Your example does not reproduce the duplicates and you don't show your expected output. – mozway May 14 '22 at 00:45
  • You can try merging on multiple columns. See https://stackoverflow.com/questions/41815079/pandas-merge-join-two-data-frames-on-multiple-columns – Pinyi Wang May 14 '22 at 00:46
  • Thanks, Pinyi Wang. Can't believe I didn't think of that before. I was too stuck on transforming the existing data. – dunkenstein May 15 '22 at 20:54

0 Answers0