Suppose I have this
d = {'State': ['California', 'California', 'New York', 'New York', 'New York'],
'County': ['Los Angeles County', 'Orange County', 'Mason County', 'Willams County', 'Stephan County'],
'FIPS': ['34027', '23421', '12345', '54321', '12456']}
df_test = pd.DataFrame(d)
I want to have a mapping of the state to the county and its associated unique code. Example,
California -> {Los Angeles county : 34027, Orange County : 23421}
I tried this
d = {}
for state, df_state in df_test.groupby('State'):
d[state] = df_state[['County', 'FIPS']].to_json()
but I get this for say California
d['California']
'{"County":{"0":"Los Angeles County","1":"Orange County"},"FIPS":{"0":"34027","1":"23421"}}'
This is almost right but I am not sure how to get it in the format I exactly want.