Home > database >  Pandas map returns column with NaN values
Pandas map returns column with NaN values

Time:11-19

I have two dataframes. I am trying to map state postal codes from state_abbv_dict to the state column.

county_2015.head()

    Year    Month   State   County  Rate    min_wage
0   2015    February    Mississippi Newton County   6.1 7.91
1   2015    February    Mississippi Panola County   9.4 7.91
2   2015    February    Mississippi Monroe County   7.9 7.91
3   2015    February    Mississippi Hinds County    6.1 7.91
4   2015    February    Mississippi Kemper County   10.6    7.91
state_abbv_dict



{'Postal Code': {'Alabama': 'AL',
  'Alaska': 'AK',
  'Arizona': 'AZ',
  'Arkansas': 'AR',
  'California': 'CA',
  'Colorado': 'CO',
  'Connecticut': 'CT',
  'Delaware': 'DE',
  'District of Columbia': 'DC',
  'Florida': 'FL',
  'Georgia': 'GA',
  'Hawaii': 'HI',
  'Idaho': 'ID',
  'Illinois': 'IL',
  'Indiana': 'IN',
  'Iowa': 'IA',
  'Kansas': 'KS',
  'Kentucky': 'KY',
  'Louisiana': 'LA',
  'Maine': 'ME',
  'Maryland': 'MD',
  'Massachusetts': 'MA',
  'Michigan': 'MI',
  'Minnesota': 'MN',
  'Mississippi': 'MS',
...
  'Virginia': 'VA',
  'Washington': 'WA',
  'West Virginia': 'WV',
  'Wisconsin': 'WI',
  'Wyoming': 'WY'}}

county_2015['State'] = county_2015['State'].map(state_abbv_dict)

county_2015.tail()


Year    Month   State   County  Rate    min_wage
2797    2015    February    NaN Somerset County 8.4 8.18
2798    2015    February    NaN Oxford County   6.8 8.18
2799    2015    February    NaN Knox County 6.1 8.18
2800    2015    February    NaN Piscataquis County  7.0 8.18
2801    2015    February    NaN Aroostook County    7.2 8.18


CodePudding user response:

It looks like it's because the states are a secondary level, I think you just need to change it to this:

county_2015['State'] = county_2015['State'].map(state_abbv_dict['Postal Code'])
  • Related