我在txt文件中有以下JSON字符串,我正在尝试从'visualLogs'变量中提取数据帧。我可以读取JSON字符串,我可以访问visualLogs列表,但我整天都没有将它转换为浮点数的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列带有标签,然后是浮点行。
答案 0 :(得分:1)
一旦你加载了json,你需要在\ r \ n然后在逗号上分割:
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())
,因为没有其他空格。
或使用 StringIO 对象使用 read_csv :
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