Last active
September 15, 2018 18:09
-
-
Save zinyosrim/2de57eee1daa39f4baac68f46664bb87 to your computer and use it in GitHub Desktop.
Make dict keys in a DF column a DF column
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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