简体   繁体   English

如何根据 Pandas 数据框中的元数据字典创建相应的值?

[英]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我们可以结合使用explodejson_normalize

explode : To transform each element of a list-like to a row. explode :将类似列表的每个元素转换为一行。

json_normalize To convert dict to columns json_normalize将 dict 转换为列

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 |
+----+--------+--------+----------+-----------+----------+

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据字典值为熊猫数据框的单个单元格着色 - How to color individual cells of a pandas data frame based on dictionary values 如何在添加数字时从 pandas 数据框创建嵌套字典 - How do I create nested dictionary from pandas data frame while adding numbers Python/Pandas:如何根据个人 ID 替换 Pandas 数据框的特定值? - Python/Pandas: How do I replace specific values of a Pandas Data Frame based on individual id? 如何用字典替换熊猫数据框中的值? - How to replace values in pandas data frame by dictionary? 如何从pandas数据框中创建字典的字典 - How to create a dictionary of a dictionary of a dictionary from a pandas data frame 如何使用1行代码基于熊猫数据框中的值删除多行? - How do I drop multiple rows based on values in a pandas data frame with 1 line of code? 如何根据字符串值列表对熊猫数据框进行子集设置? - How do I subset a pandas data frame based on a list of string values? 在根据条件比较 2 个数据框列后,如何创建新的 pandas dataframe? - How do I create a new pandas dataframe after comparing 2 data frame columns based on a condition? 如何根据条件从 pandas 列创建字典 - How do I create dictionary from pandas columns based on condition 根据单独字典中分配的值对 pandas 数据框进行排序 - Sort pandas data frame based on values assigned in separate dictionary
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM