我怎样才能正常化 json
How can I normalize json
我无法正常化 json 字符串,看起来像这样:
{
"Mercy": [
[
"Chicago",
"New Orleans"
]
],
"Zarya": [
[
"Rio de Janeiro",
"Capetown"
],
[
"Rome",
"Seattle"
]
],
"Torbjörn": [
[
"Buenos Aires",
"New York"
],
[
"Capetown",
"Juneau"
],
[
"Istanbul",
"Cairo"
]
]
}
我想得到这样的数据框:
名称 city1 city2 city3 city4 city5 city6 ..... cityN
0
1个
.
.
.
可能吗?
我相信你需要 2 列 DataFrame:
df = pd.DataFrame([(k, y) for k, v in d.items() for x in v for y in x],
columns=['name','city'])
print (df)
name city
0 Mercy Chicago
1 Mercy New Orleans
2 Zarya Rio de Janeiro
3 Zarya Capetown
4 Zarya Rome
5 Zarya Seattle
6 Torbjörn Buenos Aires
7 Torbjörn New York
8 Torbjörn Capetown
9 Torbjörn Juneau
10 Torbjörn Istanbul
11 Torbjörn Cairo
编辑:如果需要在新列中列出,请使用:
df = pd.DataFrame([(k,*x) for k, v in d.items() for x in v])
df.columns=['name'] + [f'city{i}' for i in df.columns[1:]]
print (df)
name city1 city2
0 Mercy Chicago New Orleans
1 Zarya Rio de Janeiro Capetown
2 Zarya Rome Seattle
3 Torbjörn Buenos Aires New York
4 Torbjörn Capetown Juneau
5 Torbjörn Istanbul Cairo
pandas
中有一个函数 json_normalize()
(不确定版本是否正确)
json_normalize(json_data)
它returns 一个扁平化的数据框
这是 API reference 这个
它的样子:
也许这对你有用? (您的数据在 data.json
文件中)
with open('data.json', 'r') as fil:
DATA = pd.read_json(fil, orient='index')
print(DATA.T)
输出:
Mercy Zarya Torbjörn
0 [Chicago, New Orleans] [Rio de Janeiro, Capetown] [Buenos Aires, New York]
1 None [Rome, Seattle] [Capetown, Juneau]
2 None None [Istanbul, Cairo]
我无法正常化 json 字符串,看起来像这样:
{
"Mercy": [
[
"Chicago",
"New Orleans"
]
],
"Zarya": [
[
"Rio de Janeiro",
"Capetown"
],
[
"Rome",
"Seattle"
]
],
"Torbjörn": [
[
"Buenos Aires",
"New York"
],
[
"Capetown",
"Juneau"
],
[
"Istanbul",
"Cairo"
]
]
}
我想得到这样的数据框: 名称 city1 city2 city3 city4 city5 city6 ..... cityN 0 1个 . . . 可能吗?
我相信你需要 2 列 DataFrame:
df = pd.DataFrame([(k, y) for k, v in d.items() for x in v for y in x],
columns=['name','city'])
print (df)
name city
0 Mercy Chicago
1 Mercy New Orleans
2 Zarya Rio de Janeiro
3 Zarya Capetown
4 Zarya Rome
5 Zarya Seattle
6 Torbjörn Buenos Aires
7 Torbjörn New York
8 Torbjörn Capetown
9 Torbjörn Juneau
10 Torbjörn Istanbul
11 Torbjörn Cairo
编辑:如果需要在新列中列出,请使用:
df = pd.DataFrame([(k,*x) for k, v in d.items() for x in v])
df.columns=['name'] + [f'city{i}' for i in df.columns[1:]]
print (df)
name city1 city2
0 Mercy Chicago New Orleans
1 Zarya Rio de Janeiro Capetown
2 Zarya Rome Seattle
3 Torbjörn Buenos Aires New York
4 Torbjörn Capetown Juneau
5 Torbjörn Istanbul Cairo
pandas
中有一个函数 json_normalize()
(不确定版本是否正确)
json_normalize(json_data)
它returns 一个扁平化的数据框
这是 API reference 这个
它的样子:
也许这对你有用? (您的数据在 data.json
文件中)
with open('data.json', 'r') as fil:
DATA = pd.read_json(fil, orient='index')
print(DATA.T)
输出:
Mercy Zarya Torbjörn
0 [Chicago, New Orleans] [Rio de Janeiro, Capetown] [Buenos Aires, New York]
1 None [Rome, Seattle] [Capetown, Juneau]
2 None None [Istanbul, Cairo]