I'm wondering if there's a concise, pythonic way to do this
phone 0 {"brand":{"type":"android"},"names":[{"id":"1", "name":"a-1"},{"id":"2", "name":"a-2"}]} 1 {"brand":{"type":"iphone"},"names":[{"id":"3", "name":"i-1"},{"id":"4", "name":"i-2"}]}
I want to expand the json field to be data fields, to get this:
type id name 0 android 1 a-1 1 android 2 a-2 2 iphone 3 i-1 3 iphone 4 i-2 I have found a good solution:def parser_expand_json(data): keys = [] values = [] for key in data: keys.append(key) values.append(data.get(key)) return pd.Series(values, index=keys) # that is it def test(): data = [{'brand': {'type': 'android'}, 'names': [{'id': '1', 'name': 'a-1'}, {'id': '2', 'name': 'a-2'}]}, {'brand': {'type': 'iphone'}, 'names': [{'id': '3', 'name': 'i-1'}, {'id': '4', 'name': 'i-2'}]}] df = pd.DataFrame(data) # expand json list to N rows df = df.merge(df['names'].apply(pd.Series), right_index=True, left_index=True).drop('names', axis=1).melt( id_vars=['brand'], value_name='names').drop('variable', axis=1) """ brand names 0 {u'type': u'android'} {u'id': u'1', u'name': u'a-1'} 1 {u'type': u'iphone'} {u'id': u'3', u'name': u'i-1'} 2 {u'type': u'android'} {u'id': u'2', u'name': u'a-2'} 3 {u'type': u'iphone'} {u'id': u'4', u'name': u'i-2'} """ print df # expand json key to columns name df = pd.concat([df, df['brand'].apply(parser_expand_json), df['names'].apply(parser_expand_json)], axis=1).drop( ['brand', 'names'], axis=1) """ type id name 0 android 1 a-1 1 iphone 3 i-1 2 android 2 a-2 3 iphone 4 i-2 """ print df