0

(这不是真实数据)

我从 Rest API 请求了类似的数据。然后,我能够将一些数据转换为 .ndJSON 格式(lines = True);但是,地址列仍然以类似于 Python 字典的 ndjson 格式结构显示。我的目标是拥有以下列: 第 1 列 | 街道地址 | 城市 | 状态 | 邮政编码 | 邮政编码 | 生日 |

这是第一行:

& address &birthDate & deceasedBoolean & \ 0 & {[}{'city': 'MURFREESBORO', 'line': {[}'9999 Candy Cane Island'{]}, 'postalCode': '39999', 'state' : '56'}{]} & 11/10/2081 & 0 & \

import pandas as pd
import json
from io import StringIO

data = response.text

newdf = pd.read_json(StringIO(data),lines = True)

newdf.tail(10)

newdf.to_csv('file.csv')

在此处输入图像描述

4

1 回答 1

1
import pandas as pd

# dummy df
df = pd.DataFrame({'address': [{'city': 'MURFREESBORO', 'line': ['9999 Candy Cane Island'], 'postalCode': '39999', 'state': '56'}], 
                               'birthdate': ['11/20/1977']})
# remove the [] from our address colum
df['address'] = df['address'].apply(str).str.replace('[', '').str.replace(']', '')
# turn string dict to dict
df['address']= df['address'].map(eval)
# explode the dict into multiple cols
df2 = pd.DataFrame(df['address'].values.tolist(), index=df.index)
# join other col(s) 
df3 = df2.join(df['birthdate'])

输出df

    city            line                   postalCode   state   birthdate
0   MURFREESBORO    9999 Candy Cane Island  39999        56    11/20/1977
于 2022-02-17T01:07:25.720 回答