Home > OS >  Convert pandas dataframe to a specific json type
Convert pandas dataframe to a specific json type

Time:03-06

I have a pandas dataframe as shown below

    id    txn_id         txn_type  dr_cust_id  dr_cust_acct dr_cust_name dr_cust_type  cr_cust_id  ...           dept   bank_user  source  stped      status difference department_sla predicted_sla
0     1  FT123456    Fund Transfer        1234    1234567890        Vimal       Retail        4321  ...   FRONT OFFICE    FO MAKER  Mobile   NSTP   INITIATED         32            MET           MET
1     2  FT123456    Fund Transfer        1234    1234567890        Vimal       Retail        4321  ...   FRONT OFFICE  FO CHECKER  Mobile   NSTP   INITIATED         32            MET           MET
2     3  FT123456    Fund Transfer        1234    1234567890        Vimal       Retail        4321  ...  MIDDLE OFFICE    MO MAKER  Mobile   NSTP  INPROGRESS         32            MET           MET
3     4  FT123456    Fund Transfer        1234    1234567890        Vimal       Retail        4321  ...  MIDDLE OFFICE  MO CHECKER  Mobile   NSTP  INPROGRESS         32            MET           MET
4     5  FT123456    Fund Transfer        1234    1234567890        Vimal       Retail        4321  ...    BACK OFFICE    BO MAKER  Mobile   NSTP   COMPLETED         32            MET           MET

Now I want this data in the following json format

{'id': 2111, 'txn_id': 'MC123456', 'txn_type': 'MANAGERS CHEQUE', 'dr_cust_id': 1234, 'dr_cust_acct': 1234567897, 'dr_cust_name': 'Vimal', 'dr_cust_type': 'HNI-VIP', 'cr_cust_id': 4321, 'cr_cust_acct': 987654321, 'cr_cust_name': 'Vivek', 'cr_cust_type': 'HNI-VIP', 'amount': 1046, 'currency': 'USD', 'txn_code': 'MC00001', 'remark': 'adkaDKA', 'txn_start_date_time': '2009-10-04 17:00:01', 'txn_end_date_time': '2009-10-04 19:45:01', 'dept': 'BACK OFFICE', 'bank_user': 'BO MAKER', 'source': 'IB', 'stped': 'NSTP', 'status': 'COMPLETED', 'difference': 165, 'department_sla': 'MET'}
{'id': 2112, 'txn_id': 'MC123456', 'txn_type': 'MANAGERS CHEQUE', 'dr_cust_id': 1234, 'dr_cust_acct': 1234567897, 'dr_cust_name': 'Vimal', 'dr_cust_type': 'HNI-VIP', 'cr_cust_id': 4321, 'cr_cust_acct': 987654321, 'cr_cust_name': 'Vivek', 'cr_cust_type': 'HNI-VIP', 'amount': 1047, 'currency': 'USD', 'txn_code': 'MC00001', 'remark': 'adkaDKA', 'txn_start_date_time': '2009-10-04 17:00:01', 'txn_end_date_time': '2009-10-04 19:45:01', 'dept': 'BACK OFFICE', 'bank_user': 'BO CHECKER', 'source': 'IB', 'stped': 'NSTP', 'status': 'COMPLETED', 'difference': 165, 'department_sla': 'MET'}

I tried different types of orient in order to get the desired format but none of them gave the required output. What should be done here?

CodePudding user response:

Python offers a tool to dump a dict into a json, as long you can transform your DataFrame into a dict, this can be done easily.

Supose df is your Pandas DataFrame:

import json
jsonparse=json.dumps(df.to_dict('records'))

CodePudding user response:

To Convert Pandas DataFrame To JSON Using orient = 'records' Supose df is your Pandas DataFrame you can create df2 which holds the structure required:

df2 = df.to_json(orient = 'records')
print(df2)
  • Related