简体   繁体   English

从.json 文件中获取行而不是列的问题

[英]Problem with getting values in a row instead of a column from .json file

I have several.json files that I would like to combine into one.csv file.我有几个.json 文件,我想将它们合并到一个.csv 文件中。 The.json files look like this: .json 文件如下所示:

{"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):用两个文件编译后,我在.csv 文件中得到以下结果(所有值都在一列中):

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.我想仅从每个.json 文件的一行中的列表'pose_keypoints_2d'中获取值,而不是从所有文件中获取一整列。 Something like that:像这样的东西:

338.938000 156.226000 0.322833 ...  0.000000  0.000000
346.096000 136.310000 0.822246 ...  0.000000  0.000000

You df is a column of 75 rows when it's added to data2.当您将 df 添加到 data2 时,它是一列 75 行。

Try replacing this:尝试替换这个:

data.append(df)

by this这样

data.append(df.T)

Which (df.T) is one row with 75 columns其中 (df.T) 是一行 75 列

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM