简体   繁体   English

在 for 循环中向字典添加索引

[英]Add an index to dictionary in for-loop

I'm extracting values from a pandas dataframe and want to include an index so that I know from which row in the dataframe the values belong to.我正在从 pandas dataframe 中提取值,并希望包含一个索引,以便我知道值属于 dataframe 中的哪一行。 For example, I'm getting the lists within a dataframe and extracting the individual values with the code below:例如,我正在获取 dataframe 中的列表,并使用以下代码提取各个值:

list_test = defaultdict(list)
for i in range(0, len(first_test)):
    for con, val in zip(first_test.content[i].values(), first_test.value[i].values()):
        for un_con, un_val in zip(con, val):
            list_test["data_content"].append(un_con)
            list_test["data_values"].append(un_val)

Expected output:预期 output:

       Index      data_content         data_values
0      0          Condition:            Used: An item that has been previously used. S...
1      0          Card Size:            Standard
2      0          Set:                  Base Set
3      0          Autograph Format:     Does not apply
4      0          Certification Number: Does not apply
5      0          HP:                   Does not apply
6      0          Cost:                 Unknown
7      0          Graded:               No
.
.
.

Here's a sample of the data I'm working with (Apologies for the long format):这是我正在使用的数据示例(对长格式表示歉意):

first_test = pd.DataFrame({
    'index': {
        0: 0,
        1: 1
    },
    'content': {
        0: {
            'contents': ['Condition:',
                'Card Size:',
                'Set:',
                'Autograph Format:',
                'Certification Number:',
                'HP:',
                'Cost:',
                'Graded:',
                'Language:',
                'Autograph Authentication:',
                'Convention/Event:',
                'Autographed:',
                'Signed By:',
                'Creature/Monster Type:',
                'Custom Bundle:',
                'Year Manufactured:',
                'Vintage:',
                'Autograph Authentication Number:',
                'Card Name:',
                'Manufacturer:'
            ]
        },
        1: {
            'contents': ['Condition:',
                'Card Size:',
                'Set:',
                'HP:',
                'Vintage:',
                'Language:',
                'Manufacturer:',
                'Features:',
                'Finish:',
                'Character:',
                'Attribute/MTG:Color:',
                'Autographed:',
                'Creature/Monster Type:',
                'Year Manufactured:',
                'Graded:',
                'Card Name:',
                'Stage:',
                'Card Type:',
                'Speciality:',
                'Card Condition:'
            ]
        }
    },
    'value': {
        0: {
            'values': ['Used: An item that has been previously used. See the seller’s listing for full details and ... ',
                'Standard',
                'Base Set',
                'Does not apply',
                'Does not apply',
                'Does not apply',
                'Unknown',
                'No',
                'French',
                'Does not apply',
                'Does not apply',
                'No',
                'Does not apply',
                'Unknown',
                'No',
                '2008',
                'Yes',
                'Does not apply',
                'Magby',
                'Pokémon / Nintendo'
            ]
        },
        1: {
            'values': ['Used: An item that has been used previously. See the seller’s listing for full details and ... ',
                'Standard',
                'Vivid Voltage',
                '140',
                'No',
                'English',
                'creatures gamefreak 2020 nintendo',
                'Altered Art, Box Topper, Full Art, Shadowless, Unlimited',
                'Regular',
                'Drednaw',
                'Blue',
                'No',
                'Turtle',
                '2020',
                'No',
                'Drednaw',
                'Stage 1',
                'Pokémon',
                'V',
                'Near Mint'
            ]
        }
    }
})
list_test = []
for i, row in first_test.iterrows():
    for con, val in zip(row['content'].values(), row['value'].values()):
        for un_con, un_val in zip(con, val):
            list_test += [
                dict(index=i,
                     data_content=un_con,
                     data_values=un_val,
            )]

pd.DataFrame(list_test)

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

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