如何将熊猫列作为键和值添加到json对象中

时间:2018-07-26 09:37:07

标签: python json pandas

我有数以百万计的json对象,看起来像:

{b'content:c1': b'        [{          "@context": "https://schema.org", "@type": "NewsArticle",          "url": "https://www.example.com/news/trends/current-affairs-trends/us-russia-india-driving-chinas-nuclear-modernisationpentagon-1000000.html", "description":"In a report to Congress detailing China\'s nuclear power, Pentagon yesterday said the country was deploying new command, control and communications capabilities to its nuclear forces to improve control of multiple units in the field.",    "datePublished": "12:12:05 PM IST May 14, 2016",          "dateModified": "12:28:57 PM IST May 14, 2016",   "publisher":{                "@context": "https://schema.org",                "@type": "Organization",                "name": "xyz",                "logo": {                        "@context": "https://schema.org",                        "@type": "ImageObject",                        "width": "247px",                        "height": "94px",                        "url": "https://img-d02.example.co.in/images/common/header/logo.png"                    },                "url":"https://www.example.com"              }        }]    ', b'post_id:c1': b'mc_1000000', b'update_datetime:c1': b'1529742855'}
{b'content:c1': b'        [{          "@context": "https://schema.org", "@type": "NewsArticle",          "url": "https://www.example.com/news/trends/current-affairs-trends/india-is-ready-for-nsg-membership-us-1000001.html",          "description":"Defending its move to block India\'s entry into the NSG, China today claimed that several members of the 48-nation bloc shared its view that signing of the NPT was an "important" standard for the NSG\'s expansion.",   "datePublished": "12:12:08 PM IST May 14, 2016",          "dateModified": "12:28:57 PM IST May 14, 2016",   "publisher":{                "@context": "https://schema.org",                "@type": "Organization",                "name": "xyz",                "logo": {                        "@context": "https://schema.org",                        "@type": "ImageObject",                        "width": "247px",                        "height": "94px",                        "url": "https://img-d02.example.co.in/images/common/header/logo.png"                    },                "url":"https://www.example.com"              }        }]    ', b'post_id:c1': b'mc_1000001', b'update_datetime:c1': b'1529742855'}

我已经将此json键转换为pandas列。像这样:-

@context  @type  url   description  datePublished   dateModified   publisher  keywords

现在,列keywords是我基于文本挖掘创建的新列,并将其转换为结构如下的json:

0     {'index': 0, 'keywords': ['nuclear', 'mercedez']
1     {'index': 1, 'keywords': ['dubai', 'burz khalifa']

现在,我想将keywords列作为键名称:keywords及其值作为['nuclear','mercedez'](对于第一个对象)添加到现有json对象中,所有对象的ID相同。

输出看起来像是

例如:-对于第一个json对象

{b'content:c1': b'        [{          "@context": "https://schema.org", "@type": "NewsArticle",          "url": "https://www.example.com/news/trends/current-affairs-trends/us-russia-india-driving-chinas-nuclear-modernisationpentagon-1000000.html", "description":"In a report to Congress detailing China\'s nuclear power, Pentagon yesterday said the country was deploying new command, control and communications capabilities to its nuclear forces to improve control of multiple units in the field.",    "datePublished": "12:12:05 PM IST May 14, 2016",          "dateModified": "12:28:57 PM IST May 14, 2016",   "publisher":{                "@context": "https://schema.org",                "@type": "Organization",                "name": "xyz",                "logo": {                        "@context": "https://schema.org",                        "@type": "ImageObject",                        "width": "247px",                        "height": "94px",                        "url": "https://img-d02.example.co.in/images/common/header/logo.png"                    },                "url":"https://www.example.com"              }   ,"keywords":['nuclear','mercedez'     }]    ', b'post_id:c1': b'mc_1000000', b'update_datetime:c1': b'1529742855'}

0 个答案:

没有答案