Извлечь значение из указанного ключа c из строки со вложенным словарем с python pandas - PullRequest
1 голос
/ 07 февраля 2020

Я вложил словари в столбец «metadata_y» в pandas dataframe. Мне нужно извлечь значение из указанного ключа c. Данные выглядят так:

id        metadata                                                score
123       {"dek": "<p>Don't forget to buy a card</p>", "...       345

Я извлек вложенный словарь из одной строки, и он выглядит следующим образом:

'{"dek": "<p>Don\'t forget to buy a card</p>", "links": {"edit": {"dev": "//patty-menshealth.feature.net/en/content/edit/76517422-96ad-4b5c-a24a-c080c58bce0c", "prod": "//patty-menshealth.prod.com/en/content/edit/76517422-96ad-4b5c-a24a-c080c58bce0c", "stage": "//patty-menshealth.stage.net/en/content/edit/76517422-96ad-4b5c-a24a-c080c58bce0c"}, "frontend": {"dev": "//menshealth.feature.net/trending-news/a19521193/fathers-day-weekend-plans/", "prod": "//www.menshealth.com/trending-news/a19521193/fathers-day-weekend-plans/", "stage": "//menshealth.stage.net/trending-news/a19521193/fathers-day-weekend-plans/"}}, "header": {"title_color": 1, "title_layout": 1}, "sponsor": {"program_type": 1, "tracking_urls": []}, "social_dek": "<p>Don\'t forget to buy a card</p>", "auto_social": 0, "index_title": "\u200bWeekend Guide: Treat Your Dad Right This Father\'s Day", "short_title": "Treat Your Dad Right This Father\'s Day", "social_title": "\u200bWeekend Guide: Treat Your Dad Right This Father\'s Day", "editors_notes": "<p>nid: 2801076<br>created_date: 2017-06-16 13:00:01<br>compass_feed_date: 2017-06-21 14:01:58<br>contract_id: 40</p>", "seo_meta_title": "Treat Your Dad Right This Father\'s Day\u200b | Men’s Health", "social_share_url": "/trending-news/a19521193/fathers-day-weekend-plans/", "seo_related_links": {}, "editor_attribution": "by", "hide_from_homepage": 1, "syndication_rights": 3, "seo_meta_description": "\u200bFrom gifts to food ideas, we\'ve got your Father\'s Day covered. Just don\'t forget to buy him a card."}'

, используя следующий код, я извлек ключи и значения из одной строки

def recursive_items(dictionary):
    for key, value in dictionary.items():
        if type(value) is dict:
            yield from recursive_items(value)
        else:
            yield (key, value)

for key, value in recursive_items(dict_val):
    print(key, value)

Вывод:

dek <p>Don't forget to buy a card</p>
dev //patty-menshealth.feature.net/en/content/edit/76517422-96ad-4b5c-a24a-c080c58bce0c
prod //patty-menshealth.prod.com/en/content/edit/76517422-96ad-4b5c-a24a-c080c58bce0c
stage //patty-menshealth.stage.net/en/content/edit/76517422-96ad-4b5c-a24a-c080c58bce0c
dev //menshealth.feature.net/trending-news/a19521193/fathers-day-weekend-plans/
prod //www.menshealth.com/trending-news/a19521193/fathers-day-weekend-plans/
stage //menshealth.stage.net/trending-news/a19521193/fathers-day-weekend-plans/
title_color 1
title_layout 1
program_type 1
tracking_urls []
social_dek <p>Don't forget to buy a card</p>
auto_social 0
index_title ​Weekend Guide: Treat Your Dad Right This Father's Day
short_title Treat Your Dad Right This Father's Day
social_title ​Weekend Guide: Treat Your Dad Right This Father's Day
editors_notes <p>nid: 2801076<br>created_date: 2017-06-16 13:00:01<br>compass_feed_date: 2017-06-21 14:01:58<br>contract_id: 40</p>
seo_meta_title Treat Your Dad Right This Father's Day​ | Men’s Health
social_share_url /trending-news/a19521193/fathers-day-weekend-plans/
editor_attribution by
hide_from_homepage 1
syndication_rights 3
seo_meta_description ​From gifts to food ideas, we've got your Father's Day covered. Just don't forget to buy him a card.

Мне нужны значения для каждой строки из второго ключа prod.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...