我有一个数据框,其中 1 列中有一个 json 子字符串。我想提取变量并为它们创建列
I have a dataframe with a json substring in 1 of the columns. i want to extract variables and make columns for them
imports json
df = pd.read_json("C:/xampp/htdocs/PHP code/APItest.json", orient='records')
print(df)
我想额外创建三列:['name','logo','ico_score']
而不是 'main'
列
我试过:
df2 = df.join(pd.DataFrame(list(json.loads(d).values())[0] for d in df.pop('main')) )
但得到这个 TypeError:
the JSON object must be str, bytes or bytearray, not dict
希望有人能帮我想办法,这样我就可以得到一个数据table来统计。
向学生致以最诚挚的问候。
我的 json 数据是这样的:
[
{
"id": "126",
"main": {"name": "SONM", "logo": "link", "ico_score": "6.7"},
"links": {"url": "link"},
"finance": {"raised": "42000000"},
},
{
"id": "132",
"main": {"name": "openANX", "logo": "link", "ico_score": "5.7"},
"links": {"url": "link"},
"finance": {"raised": "18756937"},
},
{
"id": "166",
"main": {"name": "Boul\u00e9", "logo": "link", "ico_score": "5.6"},
"links": {"url": "link"},
"finance": {"raised": ""},
},
]
IIUC 你可以这样做:
with open('your_json_file.json') as f:
data = json.load(f)
df = pd.json_normalize(data)
df.columns = ['id', 'name', 'logo', 'ico_score', 'url', 'raised']
print(df)
id name logo ico_score url raised
0 126 SONM link 6.7 link 42000000
1 132 openANX link 5.7 link 18756937
2 166 Boul\u00e9 link 5.6 link
imports json
df = pd.read_json("C:/xampp/htdocs/PHP code/APItest.json", orient='records')
print(df)
我想额外创建三列:['name','logo','ico_score']
而不是 'main'
列
我试过:
df2 = df.join(pd.DataFrame(list(json.loads(d).values())[0] for d in df.pop('main')) )
但得到这个 TypeError:
the JSON object must be str, bytes or bytearray, not dict
希望有人能帮我想办法,这样我就可以得到一个数据table来统计。 向学生致以最诚挚的问候。
我的 json 数据是这样的:
[
{
"id": "126",
"main": {"name": "SONM", "logo": "link", "ico_score": "6.7"},
"links": {"url": "link"},
"finance": {"raised": "42000000"},
},
{
"id": "132",
"main": {"name": "openANX", "logo": "link", "ico_score": "5.7"},
"links": {"url": "link"},
"finance": {"raised": "18756937"},
},
{
"id": "166",
"main": {"name": "Boul\u00e9", "logo": "link", "ico_score": "5.6"},
"links": {"url": "link"},
"finance": {"raised": ""},
},
]
IIUC 你可以这样做:
with open('your_json_file.json') as f:
data = json.load(f)
df = pd.json_normalize(data)
df.columns = ['id', 'name', 'logo', 'ico_score', 'url', 'raised']
print(df)
id name logo ico_score url raised
0 126 SONM link 6.7 link 42000000
1 132 openANX link 5.7 link 18756937
2 166 Boul\u00e9 link 5.6 link