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

时间:2019-01-14 09:09:16

标签: pandas dataframe python-3.5

我无法根据需要转换输出:

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

1 个答案:

答案 0 :(得分: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