将一列json字符串转换为数据列(API结果/输出)

时间:2019-10-15 15:47:45

标签: python json pandas dataframe dictionary

我的要求与以下完全相同:

Convert a column of json strings into columns of data

按照建议的解决方案进行操作,并出现以下错误:我做错了什么?我正在学习使用python并发出API请求。

***df['json'] = df['json'].map(address',': dict(eval(address)))***
                                     ^
`**SyntaxError: invalid syntax**

下面是我的代码

import requests
import json
import pandas as pd
import dictionary as dict


    Base_url = 'MY_URL'
    TOKEN_EndPoint = Base_url + 'token'
    Account_EndPoint = Base_url + 'MY_URL'

    data = {
        'username': 'MY_USERNAME',
        'password': 'MY_PASSWORD',
        'grant_type': 'MY_GRANT_TYPE'
    }

    def main():

        results = requests.post(url=TOKEN_EndPoint, data=data)

        MyToken = results.json()['access_token']

        print(MyToken)

        MyInputs = GetSourceAddress()

        callData = {
            'ClientKey': 'MY_CLIENTKEY',
            'StreetName': MyInputs['StreetName'],
            'CityName' : MyInputs['CityName'],
           # 'StateCode' : MyInputs['StateCode'],
            'PostalCode' : MyInputs['PostalCode'],
            'ManagerVersion': '2'
        }

        PostFields = json.dumps(callData)

        MyHeader = {'Authorization': 'Bearer ' + MyToken,
                    'content-type': 'application/json'}

        results = requests.post(url = Account_EndPoint, data = PostFields,
                               headers = MyHeader)

        address = results.json()

       # results.json() = pd.DataFrame(address)

        df = pd.DataFrame(['address'], columns=['json'])
        **df['json'] = df['json'].map(address',': dict(eval(address)))**
        address = df['json'].apply(pd.Series)

        for address in address:
              print(address)


    def GetSourceAddress():
        MyInputs = {
          'StreetName': 'MY_STREETNAME',
          'CityName': 'MY_CITYNAME',
         # 'StateCode': 'MY_STATE',
           'PostalCode': 'MY_ZIPCODE',
          # 'Unit': 'UNIT #'
        }

        return MyInputs

        def GetAddressFrom****(result):
            inputs = {
                'StreetName': result['ShippingStreet'],
                'CityName': result['BillingCity'],
                'StateCode': result['BillingState'],
                'PostalCode': result['BillingPostalCode'],
                # 'Unit': ''
             }
           return inputs

        main()

谢谢

0 个答案:

没有答案