我是Pandas的新手,我正在尝试将Pandas DataFrame转换为自定义的嵌套JSON字符串(也许将其写入文件)。我尝试使用内置的Pandas to_json()
函数,但对我而言并不起作用。我正在发布我的Pandas DF的一部分,并希望最终结果看起来像什么。理想情况下,我希望使用DF索引的任意值填充“ id”键。我认为我的目标是不必担心列名是什么,而是有一种方法可以将DF转换为JSON字符串。我最初编写了一个for循环,该循环将遍历每一行并将内容写入文件中,但是经过一番思考,我相信这将很容易出错,因为大多数JSON序列化都是手动处理的。
任何帮助,将不胜感激。感谢您,对不起您的帖子很长。
BarcodeSequence LinkerPrimerSequence BodySite Year Month Day Subject ReportedAntibioticUsage DaysSinceExperimentStart Description
#SampleID
L1S8 AGCTGACTAGTC GTGCCAGCMGCCGCGGTAA gut 2008.0 10.0 28.0 subject-1 Yes 0.0 subject-1.gut.2008-10-28
L1S57 ACACACTATGGC GTGCCAGCMGCCGCGGTAA gut 2009.0 1.0 20.0 subject-1 No 84.0 subject-1.gut.2009-1-20
L1S76 ACTACGTGTGGT GTGCCAGCMGCCGCGGTAA gut 2009.0 2.0 17.0 subject-1 No 112.0 subject-1.gut.2009-2-17
L1S105 AGTGCGATGCGT GTGCCAGCMGCCGCGGTAA gut 2009.0 3.0 17.0 subject-1 No 140.0 subject-1.gut.2009-3-17
L2S155 ACGATGCGACCA GTGCCAGCMGCCGCGGTAA left palm 2009.0 1.0 20.0 subject-1 No 84.0 subject-1.left-palm.2009-1-20
L2S175 AGCTATCCACGA GTGCCAGCMGCCGCGGTAA left palm 2009.0 2.0 17.0 subject-1 No 112.0 subject-1.left-palm.2009-2-17
L2S204 ATGCAGCTCAGT GTGCCAGCMGCCGCGGTAA left palm 2009.0 3.0 17.0 subject-1 No 140.0 subject-1.left-palm.2009-3-17
L2S222 CACGTGACATGT GTGCCAGCMGCCGCGGTAA left palm 2009.0 4.0 14.0 subject-1 No 168.0 subject-1.left-palm.2009-4-14
L3S242 ACAGTTGCGCGA GTGCCAGCMGCCGCGGTAA right palm 2008.0 10.0 28.0 subject-1 Yes 0.0 subject-1.right-palm.2008-10-28
L3S294 CACGACAGGCTA GTGCCAGCMGCCGCGGTAA right palm 2009.0
[
{
"id": "L1S8",
"metadata": {
"BarcodeSequence": "AGCTGACTAGTC",
"LinkerPrimerSequence": "GTGCCAGCMGCCGCGGTAA",
"BodySite": "gut",
"Year": 2008.0,
"Month": 10.0,
"Day": 28.0,
"Subject": "subject-1",
"ReportedAntibioticUsage": "Yes",
"DaysSinceExperimentStart": 0.0,
"Description": "subject-1.gut.2008-10-28"
},
"sample_frequency": "7068.0"
},
{
"id": "L1S57",
"metadata": {
"BarcodeSequence": "ACACACTATGGC",
"LinkerPrimerSequence": "GTGCCAGCMGCCGCGGTAA",
"BodySite": "gut",
"Year": 2009.0,
"Month": 1.0,
"Day": 20.0,
"Subject": "subject-1",
"ReportedAntibioticUsage": "No",
"DaysSinceExperimentStart": 84.0,
"Description": "subject-1.gut.2009-1-20"
},
"sample_frequency": "8756.0"
},
{
"id": "L1S76",
"metadata": {
"BarcodeSequence": "ACTACGTGTGGT",
"LinkerPrimerSequence": "GTGCCAGCMGCCGCGGTAA",
"BodySite": "gut",
"Year": 2009.0,
"Month": 2.0,
"Day": 17.0,
"Subject": "subject-1",
"ReportedAntibioticUsage": "No",
"DaysSinceExperimentStart": 112.0,
"Description": "subject-1.gut.2009-2-17"
},
"sample_frequency": "7922.0"
},
{
"id": "L1S105",
"metadata": {
"BarcodeSequence": "AGTGCGATGCGT",
"LinkerPrimerSequence": "GTGCCAGCMGCCGCGGTAA",
"BodySite": "gut",
"Year": 2009.0,
"Month": 3.0,
"Day": 17.0,
"Subject": "subject-1",
"ReportedAntibioticUsage": "No",
"DaysSinceExperimentStart": 140.0,
"Description": "subject-1.gut.2009-3-17"
},
"sample_frequency": "7865.0"
}
]
答案 0 :(得分:1)
这是一种动态构建JSON(某种)的方法。不过,您仍然需要做一些假设,而且我不确定您的用例是否会接受这些假设:
cannot allocate memory
,value
将是示例数据框中的“值”列。sample_frequency
参数。这可能会或可能不会。也许您也需要提前识别此列,在这种情况下,您应该使用ìd
将其设置为数据框索引。话虽如此:
.set_index()
以下是数据:
import pandas as pd
import numpy as np
import json
data = pd.DataFrame(
{
'meta_1': np.random.choice(['A', 'B', 'C'], 10),
'meta_2': np.random.choice(['Blue', 'Green', 'Red'], 10),
'value': np.random.rand(10)
}
)
print(data)
现在将您要使用的列设置为“值”列。
meta_1 meta_2 value
0 A Red 0.095142
1 C Red 0.855082
2 C Blue 0.619704
3 B Green 0.371495
4 A Red 0.000771
5 B Green 0.027218
6 B Blue 0.655847
7 B Blue 0.657976
8 A Green 0.060862
9 C Red 0.702788
然后使用嵌套dict理解的列表理解:
val_col_name = 'value'
礼物:
json.dumps([{'id': i, 'metadata': {j: row[j] for j in data.columns if j != val_col_name}, val_col_name: row[val_col_name]} for i, row in data.iterrows()])