Skip to main content
 首页 » 编程设计

python之如何从 JSON 字符串中提取 python 数据框

2025年04月02日34freeliver54

我在 txt 文件中有以下 JSON 字符串,我正在尝试从“visualLogs”变量中提取数据框。我可以读取 JSON 字符串,也可以访问 visualLogs 列表,但我一整天都未能将其转换为 float 的 9 列数据框

{ 
  "visualScore" : 0, 
  "selfReportingResults" : 5, 
  "voiceScore" : "No Data", 
  "selfReportScore" : 0, 
  "subject" : "Baseline for patient: 108", 
  "email" : "steven.vannoy@gmail.com", 
  "visualLogs" : [ 
    "time,anger,contempt,disgust,engagement,joy,sadness,surprise,valence\r22.61086,0.00633,0.19347,0.56258,0.18005,0.00223,0.0165,0.31969,0.0\r22.81096,0.00478,0.19439,0.45847,0.09747,0.00188,0.02188,0.22043,0.0\r" 
  ], 
  "askedQuestions" : [ 
    "What is your name?", 
    "How old are you?", 
    "What tim is it?" 
  ], 
  "voiceCompleteResults" : { 
    "status" : "fail" 
  } 
} 
 
with open(f4lJasonFileName) as data_file: 
    feelDat = json.load(data_file) 
 
x = feelDat['visualLogs'][0] # Ultimately there will be more than one of these 

我所有将 x 转换为数据框的尝试都失败了。我已经获得了文本值的 1 列数据框,但这不是我需要的。

我用逗号替换了那些 '\r' 字符,这最终得到了一列文本数据框,但我想要 9 列带有标签,然后是浮点行。

请您参考如下方法:

一旦你加载了 json,你需要在\r 然后在逗号上拆分:

import  pandas as pd 
 
spl = d["visualLogs"][0].split("\r") 
 
 
df = pd.DataFrame([v for v in map(lambda x: x.split(","), spl[1:]) if v[0]], columns=spl[0].split(",")) 

分解成几个部分可能更容易理解:

import pandas as pd 
 
# split into lines creating an iterator so we don't have to slice. 
spl = iter(d["visualLogs"][0].rstrip().split("\r")) 
 
# split first line to get the  column names. 
columns = next(spl).split(",") 
 
# split remaining lines into individual rows, removing empty row. 
rows = [v for v in (sub_str.split(",") for sub_str in spl) if len(v) > 1] 
 
df = pd.DataFrame(data=rows, columns=columns) 

我们也可以只 spl = iter(d["visualLogs"][0].split()) 因为没有其他空格。

或者使用 read_csv 使用 StringIO 对象:

import pandas as pd 
spl = d["visualLogs"][0] 
 
from io import StringIO 
df = pd.read_csv(StringIO(spl)) 

这给了你:

      time    anger  contempt  disgust  engagement      joy  sadness  \ 
0  22.61086  0.00633   0.19347  0.56258     0.18005  0.00223  0.01650    
1  22.81096  0.00478   0.19439  0.45847     0.09747  0.00188  0.02188    
 
   surprise  valence   
0   0.31969        0   
1   0.22043        0