Skip to content

Instantly share code, notes, and snippets.

@zinyosrim
Last active September 15, 2018 18:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zinyosrim/2de57eee1daa39f4baac68f46664bb87 to your computer and use it in GitHub Desktop.
Save zinyosrim/2de57eee1daa39f4baac68f46664bb87 to your computer and use it in GitHub Desktop.
Make dict keys in a DF column a DF column
df = pd.DataFrame({'order_id': ['A', 'B'],
'address': [{'city': "NY", 'latitude': 2.12, 'longitude' : 3.12,'country_code' : "US"},
{'city': "KL", 'latitude': 12.12, 'longitude' : 23.12,'country_code' : "MY"}]},
columns= ['order_id', 'address'])
new_cols = ['city', 'country_code']
for col in new_cols:
df['address_{}'.format(col)] = df['address'].map(lambda x: np.nan if pd.isnull(x) else x[col])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment