如何将 pandas 数据框存储到 json 文件中,同时将第一列名称作为访问 json 的键

how to store pandas data frame into json file, while having first column name as key to access json

我正在尝试借助 for 循环将 pandas 数据帧转换为 json。我想要 json 中的数据框的自定义格式。 我在 csv 中有数据,想将其转换为 json 文件,如下所示。

我尝试转换数据框 to_json,然后尝试用帐号替换索引号,但没有替换密钥,我的 for 循环将代码中存在的所有“零”替换为帐号。

{"12312312313": {
    account_no: 12312312313,  # To save acc number
    account_type: 'saving'  # account type
    branch: BARB,  #branch
    branch_address:  #address
    ifsc: BARB000000,  #ifsc
    uidai: 123412341234,   #aadhar_id
    mobile_no: 8888888888,  #communication medium
    email: 'rajaram@gmail.com',  #email
    fname: "Raj",
    mname: "Ramesh",
    sname: "Rampal",
    acc_balance: 132156465413513,
    dob: "20/12/1999",
    pan: "QWER123QER",
    occupation: "Student",
    address: "Mumbai-412321",
    ac_open_date: 12/12/2012,
    blood_group: "a-",
    history:{
        dd_mm_yyyy_hh_mm_ss:{
            transition_id: 132132323213563513,
            added: 13213,
            withdrawn: 1231,
        }
    },
    cards_allocated: {
        credit:{
            credit_card_type: "platinum",
            credit_card_no: 1323213,
            name_on_card: "raj ramesh rampal",
            card_limit: 20000,
            amount_used: 10000,
            amount_to_pay: 0000.0,
            card_issue_date: 12/12/2012,
            card_exp_date: 11/12/2025,
            cvv: 123123,
            credit_card_password: 1234,
            monthly_emi: 4225,
            history:{
                dd_mm_yyyy_hh_mm_ss:{
                    transition_id: 132132323213563513,
                    added: 13213,
                    withdrawn: 1231,
                }
            }
        },
        debit:{
            debit_card_type: "visa",
            debit_card_no: 1323213,
            name_on_card: "raj ramesh rampal",
            daily_debit_limit: 20000,
            card_issue_date: 12/12/2012,
            card_exp_date: 11/12/2025,
            cvv: 123123,
            debit_card_password: 1234,
            history:{
                dd_mm_yyyy_hh_mm_ss:{
                    transition_id: 132132323213563513,
                    added: 13213,
                    withdrawn: 1231,
                }
            },
        }
    }
Also i tried to add Transition history column but its not working.

**I am using this data to create ATM Machine Simulator using TKinter or PyQT5.**

这是一个例子,试试'Split'选项。

sample2=pd.DataFrame({'Client':['Bob','Sally', 'Bob', 'Doug', 'Sally'],
                'Result':['Expired', 'Expired','Expired', 'Not Expired', 'Not Expired'],
                'Account_Type':['Savings', 'Savings', 'Savings', 'Savings', 'Checking'],
                'Occupation':['Student', 'Engineer', 'Doctor', 'Student', 'Engineer']})


df_json = sample2.set_index('Client').to_json(orient='split')

打印(df_json)

{"columns":["Result","Account_Type","Occupation"],"index":["Bob","Sally","Bob","Doug","Sally"],"data":[["Expired","Savings","Student"],["Expired","Savings","Engineer"],["Expired","Savings","Doctor"],["Not Expired","Savings","Student"],["Not Expired","Checking","Engineer"]]}