如何将嵌套的json结构转换为数据框 [英] How to convert nested json structure to dataframe
问题描述
我将JSON转换为DataFrame
,并以"Structure_value"列结尾,该列具有以下值作为字典/词典列表:
I converted a JSON into DataFrame
and ended up with a column 'Structure_value' having below values as list of dictionary/dictionaries:
Structure_value
[{'Room': 6, 'Length': 7}, {'Room': 6, 'Length': 7}]
[{'Room': 6, 'Length': 22}]
[{'Room': 6, 'Length': 8}, {'Room': 6, 'Length': 9}]
由于它是一个对象,所以我猜它最终以这种格式出现.
Since it is an object so I guess it ended up in this format.
我需要将其分为以下四列:
I need to split it into below four columns:
Structure_value_room_1
Structure_value_length_1
Structure_value_room_2
Structure_value_length_2
Structure_value_room_1
Structure_value_length_1
Structure_value_room_2
Structure_value_length_2
StackOverflow上的所有其他解决方案仅处理将Simple JSON转换为DataFrame而不是嵌套结构的问题.
All other solutions on StackOverflow only deal with converting Simple JSON into DataFrame and not the nested structure.
附言::我知道我可以通过显式命名字段来做些事情,但是我需要一个通用的解决方案,以便将来可以处理这种格式的任何JSON
P.S.: I know I can do something by explicitly naming fields but I need a generic solution so that in future any JSON of this format can be handled
:输出应如下所示:
Structure_value_room_1 Structure_value_length_1 Structure_value_room_2 \
0 6 7 6.0
1 6 22 NaN
2 6 8 6.0
Structure_value_length_2
0 7.0
1 NaN
2 9.0
推荐答案
将列表理解与嵌套字典理解结合使用,并将枚举用于重复数据删除的字典键,最后将字典列表传递给DataFrame
构造函数:
Use list comprehension with nested dictionary comprehension with enumerate for deduplicate keys of dicts, last pass list of dictionaries to DataFrame
constructor:
L = [ {f"{k}_{i}": v for i, y in enumerate(x, 1)
for k, v in y.items()}
for x in df["Structure_value"] ]
df = pd.DataFrame(L)
print(df)
Room_1 Length_1 Room_2 Length_2
0 6 7 6.0 7.0
1 6 22 NaN NaN
2 6 8 6.0 9.0
对于问题中的列名,请使用:
For columns names from question use:
def json_to_df(df, column):
L = [ {f"{column}_{k.lower()}_{i}": v for i, y in enumerate(x, 1)
for k, v in y.items()}
for x in df[column] ]
return pd.DataFrame(L)
df1 = json_to_df(df, 'Structure_value')
print(df1)
Structure_value_room_1 Structure_value_length_1 Structure_value_room_2 \
0 6 7 6.0
1 6 22 NaN
2 6 8 6.0
Structure_value_length_2
0 7.0
1 NaN
2 9.0
这篇关于如何将嵌套的json结构转换为数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!