gpt4 book ai didi

python - 将 JSON 文件的嵌套数组中的数据提取到 Dataframe 中

转载 作者:太空宇宙 更新时间:2023-11-03 14:32:16 25 4
gpt4 key购买 nike

有一个 Json 文件,该文件是包含数组的数组我可以使用下面的代码获取所有“部分”,但无法弄清楚 json_normalize parms 的用法来提取嵌套数组中的不同级别?

即希望车辆数组中的“id”和模型数组中的“id”以及所有零件数组

car | camry | "value":"engine","price":10.82

谢谢

f = open('sample.json')
data = json.load(f)
f.close()
df1 = json_normalize(data['vehicle'], 'model')
df2 = df1[['parts']]
ddf = pd.DataFrame(columns=['value','charge'])

for (index,row) in df2.iterrows():
if pd.notnull(row[0]):
e = row[0]
ddf.loc[index] = [e[0]['value'], e[0]['charge']]


{
"vehicle":[
{
"id":"car",
"model":[
{
"id":"camry",
"parts": [
{
"value":"engine",
"charge":10.82
} ] }
,
{
"id":"avelon",
"parts": [
{
"value":"seats",
"charge":538.26
} ] }
,
{
"id":"prius",

"parts": [
{
"value":"seats",
"charge":10.91
} ] }
,
{
"id":"corolla",
"markup": {
"value":"61"
}
,
"accessories": [
{
"value":"vvvvv"
}]

} ] } ] }

最佳答案

我认为你需要:

#remove NaNs
s = df1['parts'].dropna()
#create new DataFrame, assuming only one list always
df2 = pd.DataFrame(s.str[0].values.tolist(), index=s.index)
print (df2)
charge value
0 10.82 engine
1 538.26 seats
2 10.91 seats

#join to original
df = df1[['id']].join(df2)
print (df)
id charge value
0 camry 10.82 engine
1 avelon 538.26 seats
2 prius 10.91 seats
3 corolla NaN NaN

关于python - 将 JSON 文件的嵌套数组中的数据提取到 Dataframe 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47203561/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com