简体   繁体   中英

How do I create corresponding values based on the metadata dictionary in pandas data frame?

I have a pandas data frame which looks like this:

    Column1 Column2 Column3
1   apple    fruit  [{"tag_id":123123,'name':"juicy","weight":'1'},{"tag_id":55657672,'name':'Spain',"weight":"53"},{"tag_id":24356,'name':'the UK',"weight":"67"]

2   cat      animal [{"tag_id":1235,'name':"funny","weight":"10"},{"tag_id":4514,'name':'expensive',"weight":"56"
}]

3  English   language [{"tag_id":10010,'name':"culture","weight":"34"},{"tag_id":44123,"name"="COVID-19","weight":"5"}]

What I wanted to get is like this below

       Column1 Column2 tag_id     name        weight
    1   apple    fruit  123123    juicy        1
    2   apple    fruit  55657     Spain        53
    3   apple    fruit  24356     the UK       67
    4   apple    fruit  24356     the UK       67
    5   cat      animal  1235     funny        10
    6   cat      animal  4514     expensive    56
    7   English  language  10010  culture      34
    8   English  language  44123  COVID-19      5

Yep, I just don't know how to transform the dictionary data and assign with the key value.

Thanks

We can use a combination of explode and json_normalize

explode : To transform each element of a list-like to a row.

json_normalize To convert dict to columns

import pandas as pd
df = pd.DataFrame([['apple','fruit',
    [{"tag_id":123123,'name':"juicy","weight":'1'}, {"tag_id":55657672,'name':'Spain',"weight":"53"},{"tag_id":24356,'name':'the UK',"weight":"67"}]],
    ['cat','animal',[{"tag_id":1235,'name':"funny","weight":"10"},{"tag_id":4514,'name':'expensive',"weight":"56"}]]],columns=['col1','col2','col3'])

df = df.explode('col3').reset_index(drop=True)
tempdf = pd.json_normalize(df['col3'])
df = pd.concat([df,tempdf],axis=1)
df.drop('col3',axis=1,inplace=True)
print(df)

+----+--------+--------+----------+-----------+----------+
|    | col1   | col2   |   tag_id | name      |   weight |
+====+========+========+==========+===========+==========+
|  0 | apple  | fruit  |   123123 | juicy     |        1 |
|  1 | apple  | fruit  | 55657672 | Spain     |       53 |
|  2 | apple  | fruit  |    24356 | the UK    |       67 |
|  3 | cat    | animal |     1235 | funny     |       10 |
|  4 | cat    | animal |     4514 | expensive |       56 |
+----+--------+--------+----------+-----------+----------+

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM