更改 json 格式 pandas to_json(orient="records") 方法
Change json formatting for pandas to_json(orient="records") method
我正在尝试更改我的 json 文件的格式,如下所示 - 这可以通过 pandas 实现吗?我已经尝试了一些正则表达式操作,但是当我使用 to_json(orient='records').replace(regex=true) 方法时,我得到了一些非常奇怪的输出。 ([] 变成 '[\"\"]')。有没有其他选择?非常感谢你的帮助。我已经从大约一百万行中删除了个人信息。
一些背景信息:以下数据是从我的 algolia 数据库中抓取的,读入 pandas,并保存为 json 文件。
我的实际 json 文件(大约有一百万行)
[{"Unnamed: 0":37427,"email":null,"industry":"['']","category":"['help', 'motivation']","phone":null,"tags":"['U.S.']","twitter_bio":"I'm the freshest kid on the block."}]
我的实际输出
Unnamed: 0 category email industry phone tags twitter_bio
37427 ['help', 'motivation'] NaN [''] NaN ['U.S.'] I'm the freshest kid on the block.
需要 json 文件
[{"Unnamed: 0":37427,"email":null,"industry":[""],"category":["help", "motivation"],"phone":null,"tags":["U.S."],"twitter_bio":"I'm the freshest kid on the block."}]
期望的输出
Unnamed: 0 category email industry phone tags twitter_bio
37427 [help, motivation] NaN [] NaN [U.S.] I'm the freshest kid on the block.
我假设你正在尝试做的是转换你的列表(最初只是字符串),并希望它们成为实际列表。
您可以通过一些字符串操作来实现:
import json
import re
from pandas.io.json import json_normalize
json_file = 'C:/test.json'
jsonStr= open(json_file).read()
jsonStr = jsonStr.replace('"[','[')
jsonStr = jsonStr.replace(']"',']')
jsonStr = re.sub("\[[^]]*\]", lambda x:x.group(0).replace("'",'"'), jsonStr)
jsonObj = json.loads(jsonStr)
df = json_normalize(jsonObj[0])
输出:
print (df.to_string())
Unnamed: 0 category email industry phone tags twitter_bio
0 37427 [help, motivation] None [] None [U.S.] I'm the freshest kid on the block.
我正在尝试更改我的 json 文件的格式,如下所示 - 这可以通过 pandas 实现吗?我已经尝试了一些正则表达式操作,但是当我使用 to_json(orient='records').replace(regex=true) 方法时,我得到了一些非常奇怪的输出。 ([] 变成 '[\"\"]')。有没有其他选择?非常感谢你的帮助。我已经从大约一百万行中删除了个人信息。
一些背景信息:以下数据是从我的 algolia 数据库中抓取的,读入 pandas,并保存为 json 文件。
我的实际 json 文件(大约有一百万行)
[{"Unnamed: 0":37427,"email":null,"industry":"['']","category":"['help', 'motivation']","phone":null,"tags":"['U.S.']","twitter_bio":"I'm the freshest kid on the block."}]
我的实际输出
Unnamed: 0 category email industry phone tags twitter_bio
37427 ['help', 'motivation'] NaN [''] NaN ['U.S.'] I'm the freshest kid on the block.
需要 json 文件
[{"Unnamed: 0":37427,"email":null,"industry":[""],"category":["help", "motivation"],"phone":null,"tags":["U.S."],"twitter_bio":"I'm the freshest kid on the block."}]
期望的输出
Unnamed: 0 category email industry phone tags twitter_bio
37427 [help, motivation] NaN [] NaN [U.S.] I'm the freshest kid on the block.
我假设你正在尝试做的是转换你的列表(最初只是字符串),并希望它们成为实际列表。
您可以通过一些字符串操作来实现:
import json
import re
from pandas.io.json import json_normalize
json_file = 'C:/test.json'
jsonStr= open(json_file).read()
jsonStr = jsonStr.replace('"[','[')
jsonStr = jsonStr.replace(']"',']')
jsonStr = re.sub("\[[^]]*\]", lambda x:x.group(0).replace("'",'"'), jsonStr)
jsonObj = json.loads(jsonStr)
df = json_normalize(jsonObj[0])
输出:
print (df.to_string())
Unnamed: 0 category email industry phone tags twitter_bio
0 37427 [help, motivation] None [] None [U.S.] I'm the freshest kid on the block.