2

I have several .json files that I would like to combine into one .csv file. The .json files look like this:

{"version":1.3,"people":[{"person_id":[-1],"pose_keypoints_2d":[346.096,136.31,0.822246,339.237,210.457,0.840786,264.747,203.769,0.807762,149.518,183.631,0.854263,88.6763,82.0749,0.906274,400.122,210.38,0.854152,508.225,183.228,0.898651,555.846,75.2072,0.958429,345.636,345.892,0.111406,291.626,346.153,0.194191,0,0,0,0,0,0,393.258,346.08,0.15249,0,0,0,0,0,0,339.241,136.084,0.804675,352.463,136.006,0.702916,305.539,156.42,0.639209,359.362,156.248,0.325405,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
"face_keypoints_2d":[],"hand_left_keypoints_2d":[],"hand_right_keypoints_2d":[],"pose_keypoints_3d":[],
"face_keypoints_3d":[],"hand_left_keypoints_3d":[],"hand_right_keypoints_3d":[]}]}

The code I use to load and combine files:

import json
import os
import pandas as pd

data = []

for file in os.listdir("C:/Users/ciach/PycharmProjects/CSV/"):
    if file.endswith(".json"):
        data2 = json.load(open(os.path.join("C:/Users/ciach/PycharmProjects/CSV/", file)))
        df = pd.json_normalize(data2,record_path=['people','pose_keypoints_2d'])
        data.append(df)

temp = pd.concat(data, ignore_index = True) 
temp.to_csv("keypoints.csv", index=False) 

After compilation with two files, I get the following results in .csv file (all values are in one column):

338.938
156.226
...
0.000000
0.000000

I would like to get the values only from the list 'pose_keypoints_2d' in one row for each .json file instead of one whole column from all files. Something like that:

338.938000 156.226000 0.322833 ...  0.000000  0.000000
346.096000 136.310000 0.822246 ...  0.000000  0.000000
2
  • What is the result of print(data)? Commented Dec 20, 2020 at 11:08
  • I think it is trivial to solve, but I cannot show you because shown input and output looks unrelated. Where is the sequence 338.938000 156.226000 0.322833 ... 0.000000 0.000000??? Commented Dec 20, 2020 at 11:12

1 Answer 1

2

You df is a column of 75 rows when it's added to data2.

Try replacing this:

data.append(df)

by this

data.append(df.T)

Which (df.T) is one row with 75 columns

Sign up to request clarification or add additional context in comments.

2 Comments

It wokrs! Thank you very much
Great, please accept answer so that it will leave the queue of 'unanswered' questions. Cheers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.