如何使用pandas.dataframe()函数转换python的嵌套dict

问题描述 投票:0回答:1

我无法按照我的意愿转换输出:

x = {
     "urlCrawlErrorSample": [ 
         {
           "urlDetails": {
                           "containingSitemaps": ['asff','123'],
                           "linkedFromUrls": ['xcvxcvad','89767']
                          },
           "first_detected": "A String", 
           "pageUrl": "A String", 
           "responseCode": 42, 
           "last_crawled": "A String"
         }  
      ]
     }

我申请的内容:

df = 
pandas.DataFrame.from_dict(json_normalize(x['urlCrawlErrorSample']), orient='columns')

我得到了什么,输出:

df
  first_detected last_crawled   pageUrl  responseCode urlDetails.containingSitemaps urlDetails.linkedFromUrls
0       A String     A String  A String            42                   [asff, 123]         [xcvxcvad, 89767]

期望的输出:

 df
  first_detected last_crawled   pageUrl  responseCode urlDetails.containingSitemaps urlDetails.linkedFromUrls
0       A String     A String  A String            42                          asff                  xcvxcvad
1       A String     A String  A String            42                          123                      89767
pandas dataframe python-3.5
1个回答
0
投票

解决这个问题的一种方法

x = {
 "urlCrawlErrorSample": [ 
     {
       "urlDetails": {
                       "containingSitemaps": ['asff','123'],
                       "linkedFromUrls": ['xcvxcvad','89767']
                      },
       "first_detected": "A String", 
       "pageUrl": "A String", 
       "responseCode": 42,       
 "last_crawled": "A String"
     }  
  ]
 }

a = pd.DataFrame(x['urlCrawlErrorSample']*2)
b = pd.DataFrame(x['urlCrawlErrorSample'][0]['urlDetails'])
print( pd.concat([a,b], axis=1).drop('urlDetails', axis=1) )

产量

  first_detected last_crawled   pageUrl  responseCode containingSitemaps  \
0       A String     A String  A String            42               asff   
1       A String     A String  A String            42                123   

  linkedFromUrls  
0       xcvxcvad  
1          89767  
© www.soinside.com 2019 - 2024. All rights reserved.