Home > Net >  process json file with pandas
process json file with pandas

Time:11-27

I have a json file with objects like this: `

{"_id":"62b2eb94955fe1001d22576a","datasetName":"training-set","x":[1.062747597694397,0.010748463682830334,0.5052880048751831,0.7953124046325684,0.4599417448043823,0.5107740, 0.005278450902551413,0,0.372520387172699,0.9956972002983093],"y":"Contemporary", "team":"A"}

`

can you help me extract the "x" arrays as a list in panda based on the team and the y value? I mean extracting an array of "x" as a panda list specically for "team":"A" and "y":"contemporary" for example.

I don't know how to write a nested two for loops to extract "x" arrays as a list for each team per each "y" value.

CodePudding user response:

I'm guessing here but am assuming you want?

df = pd.DataFrame(data).groupby(["team", "y"])["x"].apply(list).reset_index()["x"].squeeze()
print(df)

Output:

[1.062747597694397, 0.010748463682830334, 0.5052880048751831, 0.7953124046325684, 0.4599417448043823, 0.510774, 0.005278450902551413, 0.0, 0.372520387172699, 0.9956972002983093]
  • Related