簡體   English   中英

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

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

我正在從 pandas dataframe 中提取值,並希望包含一個索引,以便我知道值屬於 dataframe 中的哪一行。 例如,我正在獲取 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)

預期 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
.
.
.

這是我正在使用的數據示例(對長格式表示歉意):

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