如何将嵌套的dict格式的csv的'_source'列扁平化为数据框

时间:2019-09-18 19:42:23

标签: python json pandas dataframe

我有一个包含500多个行的csv,其中一列“ _source”存储为JSON。我想将其提取到pandas数据框中。我需要每个键成为其自己的列。 #我有一个1 mb的在线社交媒体数据Json文件,我需要将字典和键值转换为它们自己的单独列。社交媒体数据来自Facebook,Twitter /网络爬行等。大约528行独立的帖子/推文/文本行,每行在词典中都有许多词典。我在下面的Jupyter笔记本电脑上附加了几个步骤,以提供更完整的理解。需要将字典中所有字典的所有键值对都转换为数据框内的列 非常感谢您,这将是巨大的帮助!!! 我已经尝试通过执行此操作将其更改为数据框

source = pd.DataFrame.from_dict(source, orient='columns')

它返回了类似的内容...我认为它可能会解开字典的包装,但没有。

#source.head()

#_source
#0   {'sub_organization_id': 'default', 'uid': 'aba...
#1   {'sub_organization_id': 'default', 'uid': 'ab0...
#2   {'sub_organization_id': 'default', 'uid': 'ac0...

下面是形状

#source.shape (528, 1)
下方的

是实际的“ _source”行看起来像张开的样子。有许多字典和键:值对,其中每个键都必须是其自己的列。谢谢!出于隐私原因,实际链接已更改/混乱。

{'sub_organization_id': 'default',
 'uid': 'ac0fafe9ba98327f2d0c72ddc365ffb76336czsa13280b',
 'project_veid': 'default',
 'campaign_id': 'default',
 'organization_id': 'default',
 'meta': {'rule_matcher': [{'atribs': {'website': 'github.com/res',
     'source': 'Explicit',
     'version': '1.1',
     'type': 'crawl'},
    'results': [{'rule_type': 'hashtag',
      'rule_tag': 'Far',
      'description': None,
      'project_veid': 'A7180EA-7078-0C7F-ED5D-86AD7',
      'campaign_id': '2A6DA0C-365BB-67DD-B05830920',
      'value': '#Far',
      'organization_id': None,
      'sub_organization_id': None,
      'appid': 'ray',
      'project_id': 'CDE2F42-5B87-C594-C900E578C',
      'rule_id': '1838',
      'node_id': None,
      'metadata': {'campaign_title': 'AF',
       'project_title': 'AF '}}]}],
  'render': [{'attribs': {'website': 'github.com/res',
     'version': '1.0',
     'type': 'Page Render'},
    'results': [{'render_status': 'success',
      'path': 'https://east.amanaws.com/rays-ime-store/renders/b/b/70f7dffb8b276f2977f8a13415f82c.jpeg',
      'image_hash': 'bb7674b8ea3fc05bfd027a19815f82c',
      'url': 'https://discooprdapp.com/',
      'load_time': 32}]}]},
 'norm_attribs': {'website': 'github.com/res',
  'version': '1.1',
  'type': 'crawl'},
 'project_id': 'default',
 'system_timestamp': '2019-02-22T19:04:53.569623',
 'doc': {'appid': 'subtter',
  'links': [],
  'response_url': 'https://discooprdapp.com',
  'url': 'https://discooprdapp.com/',
  'status_code': 200,
  'status_msg': 'OK',
  'encoding': 'utf-8',
  'attrs': {'uid': '2ab8f2651cb32261b911c990a8b'},
  'timestamp': '2019-02-22T19:04:53.963',
  'crawlid': '7fd95-785-4dd259-fcc-8752f'},
 'type': 'crawl',
 'norm': {'body': '\n',
  'domain': 'discordapp.com',
  'author': 'crawl',
  'url': 'https://discooprdapp.com',
  'timestamp': '2019-02-22T19:04:53.961283+00:00',
  'id': '7fc5-685-4dd9-cc-8762f'}}

在发布之前,请确保实际代码适用于所附加的数据。谢谢!

我尝试了下面的代码,但是它不起作用,出现语法错误,我无法弄清。

pd.io.json.json_normalize(source_data。[_ source] .apply(json.loads))

pd.io.json.json_normalize(source_data.[_source].apply(json.loads))
                                      ^
SyntaxError: invalid syntax

任何能帮助我的人都是圣人!

1 个答案:

答案 0 :(得分:0)

前段时间我不得不做类似的事情。基本上,我使用了一个将json完全弄平的函数,以标识将被转换为列的键,然后遍历json来重建一行并将每行追加到“结果”数据帧中。因此,使用您提供的数据,它创建了52列行,并对其进行浏览,看起来它已将所有键包含在其自己的列中。任何嵌套的内容,例如:'meta': {'rule_matcher':[{'atribs': {'website': ...]}应该具有列名meta.rule_matcher.atribs.website,其中'.'表示那些嵌套键

data_source = {'sub_organization_id': 'default',
 'uid': 'ac0fafe9ba98327f2d0c72ddc365ffb76336czsa13280b',
 'project_veid': 'default',
 'campaign_id': 'default',
 'organization_id': 'default',
 'meta': {'rule_matcher': [{'atribs': {'website': 'github.com/res',
     'source': 'Explicit',
     'version': '1.1',
     'type': 'crawl'},
    'results': [{'rule_type': 'hashtag',
      'rule_tag': 'Far',
      'description': None,
      'project_veid': 'A7180EA-7078-0C7F-ED5D-86AD7',
      'campaign_id': '2A6DA0C-365BB-67DD-B05830920',
      'value': '#Far',
      'organization_id': None,
      'sub_organization_id': None,
      'appid': 'ray',
      'project_id': 'CDE2F42-5B87-C594-C900E578C',
      'rule_id': '1838',
      'node_id': None,
      'metadata': {'campaign_title': 'AF',
       'project_title': 'AF '}}]}],
  'render': [{'attribs': {'website': 'github.com/res',
     'version': '1.0',
     'type': 'Page Render'},
    'results': [{'render_status': 'success',
      'path': 'https://east.amanaws.com/rays-ime-store/renders/b/b/70f7dffb8b276f2977f8a13415f82c.jpeg',
      'image_hash': 'bb7674b8ea3fc05bfd027a19815f82c',
      'url': 'https://discooprdapp.com/',
      'load_time': 32}]}]},
 'norm_attribs': {'website': 'github.com/res',
  'version': '1.1',
  'type': 'crawl'},
 'project_id': 'default',
 'system_timestamp': '2019-02-22T19:04:53.569623',
 'doc': {'appid': 'subtter',
  'links': [],
  'response_url': 'https://discooprdapp.com',
  'url': 'https://discooprdapp.com/',
  'status_code': 200,
  'status_msg': 'OK',
  'encoding': 'utf-8',
  'attrs': {'uid': '2ab8f2651cb32261b911c990a8b'},
  'timestamp': '2019-02-22T19:04:53.963',
  'crawlid': '7fd95-785-4dd259-fcc-8752f'},
 'type': 'crawl',
 'norm': {'body': '\n',
  'domain': 'discordapp.com',
  'author': 'crawl',
  'url': 'https://discooprdapp.com',
  'timestamp': '2019-02-22T19:04:53.961283+00:00',
  'id': '7fc5-685-4dd9-cc-8762f'}}

代码:

def flatten_json(y):
    out = {}
    def flatten(x, name=''):
        if type(x) is dict:
            for a in x:
                flatten(x[a], name + a + '_')
        elif type(x) is list:
            i = 0
            for a in x:
                flatten(a, name + str(i) + '_')
                i += 1
        else:
            out[name[:-1]] = x
    flatten(y)
    return out


flat = flatten_json(data_source)


import pandas as pd
import re

results = pd.DataFrame()
special_cols = []

columns_list = list(flat.keys())
for item in columns_list:
    try:
        row_idx = re.findall(r'\_(\d+)\_', item )[0]
    except:
        special_cols.append(item)
        continue
    column = re.findall(r'\_\d+\_(.*)', item )[0]
    column = re.sub(r'\_\d+\_', '.', column)

    row_idx = int(row_idx)
    value = flat[item]

    results.loc[row_idx, column] = value

for item in special_cols:
    results[item] = flat[item]

输出:

print (results.to_string())
   atribs_website atribs_source atribs_version atribs_type results.rule_type results.rule_tag  results.description          results.project_veid           results.campaign_id results.value  results.organization_id  results.sub_organization_id results.appid           results.project_id results.rule_id  results.node_id results.metadata_campaign_title results.metadata_project_title attribs_website attribs_version attribs_type results.render_status                                       results.path               results.image_hash                results.url  results.load_time sub_organization_id                                             uid project_veid campaign_id organization_id norm_attribs_website norm_attribs_version norm_attribs_type project_id            system_timestamp doc_appid          doc_response_url                    doc_url  doc_status_code doc_status_msg doc_encoding                doc_attrs_uid            doc_timestamp                 doc_crawlid   type norm_body     norm_domain norm_author                  norm_url                    norm_timestamp                 norm_id
0  github.com/res      Explicit            1.1       crawl           hashtag              Far                  NaN  A7180EA-7078-0C7F-ED5D-86AD7  2A6DA0C-365BB-67DD-B05830920          #Far                      NaN                          NaN           ray  CDE2F42-5B87-C594-C900E578C            1838              NaN                              AF                            AF   github.com/res             1.0  Page Render               success  https://east.amanaws.com/rays-ime-store/render...  bb7674b8ea3fc05bfd027a19815f82c  https://discooprdapp.com/               32.0             default  ac0fafe9ba98327f2d0c72ddc365ffb76336czsa13280b      default     default         default       github.com/res                  1.1             crawl    default  2019-02-22T19:04:53.569623   subtter  https://discooprdapp.com  https://discooprdapp.com/              200             OK        utf-8  2ab8f2651cb32261b911c990a8b  2019-02-22T19:04:53.963  7fd95-785-4dd259-fcc-8752f  crawl        \n  discordapp.com       crawl  https://discooprdapp.com  2019-02-22T19:04:53.961283+00:00  7fc5-685-4dd9-cc-8762f