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

标签 python json

我在 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  

关于python - 如何从 JSON 字符串中提取 python 数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36510390/

相关文章:

python - 为什么用 Python 拍摄的网络摄像头图像这么暗?

javascript - 在 D3 Javascript 中显示 .json map

java - 使用 GSON 解析 JSON 文件

java - 是否可以在 jsp 中根据条件设置页面内容类型或为单个 jsp 设置不同的内容类型

javascript - 在Javascript中更新JSON数据的引用问题

json - 使用 Swift 获取 Json 元素

python - 如何将掩码应用于 Python 中的数字或字符串?

python - 以整数元组为键的 KeyError

java - TCP 客户端出现异常错误

python - 我怎样才能更快地阅读一行?