如何从JSON字符串中提取python数据帧
我在一个txt文件中得到了以下JSON字符串,我正试图从“VisualLS”变量中提取一个数据帧。我可以在中读取JSON字符串,也可以访问VisualLS列表,但我整天都未能将其转换为9列浮点数字数据帧如何从JSON字符串中提取python数据帧,python,json,Python,Json,我在一个txt文件中得到了以下JSON字符串,我正试图从“VisualLS”变量中提取一个数据帧。我可以在中读取JSON字符串,也可以访问VisualLS列表,但我整天都未能将其转换为9列浮点数字数据帧 { "visualScore" : 0, "selfReportingResults" : 5, "voiceScore" : "No Data", "selfReportScore" : 0, "subject" : "Baseline for patient: 108",
{
"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后,需要在逗号上拆分:
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[“VisualLS”][0].split())
,因为没有其他空格
或者使用StringIO对象使用read_csv:
这给了你:
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
你想要一个真正的
pandas
dataframe,还是一个二维列表?@Paul,我想我想要一个数据帧。R是我的母语,我习惯于在数据帧中处理数据。我目前的理解是熊猫数据帧和R数据帧的结构非常相似。我将对这些数据进行建模。我之所以使用python,是因为它对web开发的强大支持,因为这些数据是通过APIGah输入的,在我写我的答案时编辑了你的答案pandas
似乎很擅长将\r
理解为行分隔符,因此无需拆分和重新加入。@Paul,我尝试过,但刚刚意识到我已经在上拆分了,doh!另外,您可能想提到StringIO
在2.x和3.x之间移动。如果使用io导入StringIO中的,则两者都兼容from six.moves import StringIO
可能会处理其他情况(不确定io
何时被后端口)。@Paul,没错,我通常会使用它,更少键入:)谢谢,我接近第二种方法,但没有完全正确。我对第一种方法不太了解,但它确实有效,我会随着python的进步找出它是如何起作用的。我真希望我今天早些时候问过这个问题!但是,我学到了很多:)
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