Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

process json file with pandas

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading