gpt4 book ai didi

python - 如何制作多列,每列具有不同的行

转载 作者:行者123 更新时间:2023-12-01 06:48:07 26 4
gpt4 key购买 nike

我目前被阻止,因为我想创建一个 CSV 文件,其中第一列仅填充键,第二列填充我的值和标题。像这样 : Screenshot 1

但实际上看起来像这样: Screenshot 2

我的 JSON 测试文件:

{
"name":"<customer>_<datetime>.zip",
"status":"OK",
"lib_status":"SUCCESS",
"ID":" ",
"ID_emitter":"<customer>",
"recipient":" ",
"validator":{
"sender_email_address":"example@example.com",
"sender_email_address_no_reply":"example@example.com"
},
"connexFileList":[
{
"connexFile":"<customer>_<datetime>",
"type":"json"
}
],
"PReS":{
"domain":" ",
"client":" ",
"inputFlowList":[
{
"inputFile":" ",
"type":" ",
"status":" "
}
]
}
}

我当前的代码:

import json
import pandas as pd

with open('test.json') as f:
json_dict = json.load(f)

name = json_dict['name']
status = json_dict['status']
lib_status = json_dict['lib_status']
ID_emitter = json_dict['ID_emitter']

for connexFileList in json_dict['connexFileList']:
connexFile = connexFileList['connexFile'],
type2 = connexFileList['type']

for PReS in json_dict['PReS']['inputFlowList']:
inputFile = PReS['inputFile']
type3 = PReS['type']
status2 = PReS['status']

raw_data = {
"firstitem": [name, status, lib_status, ID_emitter, '', '', '', '', ''],
"connexFileList": ['', '', '', '', connexFile, type2, '', '', ''],
"PReS": ['','','','','','', inputFile, type3, status2],
}

df = pd.DataFrame(raw_data,
index=pd.Index(['name :', 'status :', 'lib_status', 'ID_emitter',
'connexFile','type','inputFile', 'type', 'status']),
columns=pd.Index(['firstitem', 'connexFileList', 'PReS'])
)
df.to_csv('test.csv', sep=";")

但这不是我想要的 CSV,并且 [' ', ' ',...] 的 raw_data 并没有真正优化...我认为解决方案是 pd.MultiIndexpd.Series 但我不知道如何在我的代码中应用这两个解决方案。

最佳答案

试试这个:

raw_data = {
"firstitem": [name, status, lib_status, ID_emitter, '', '', '', '', ''],
" ": ['connexFile','type','', '', '', '', '', '', ''],
"connexFileList": [connexFile, type2, '', '', '','', '', '', ''],
"PReS": ['','','','','','', inputFile, type3, status2],
"": ['inputFile','type','status', '', '', '', '', '', '']
}

df = pd.DataFrame(raw_data,
index=pd.Index(['name', 'status', 'lib_status', 'ID_emitter',
'','','', '', '']),
columns=pd.Index(['firstitem', ' ','connexFileList','', 'PReS'])
)

关于python - 如何制作多列,每列具有不同的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59139492/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com