Home > OS >  Python - Merge list of dictionary with same values
Python - Merge list of dictionary with same values

Time:10-10

  • I have a list of dictionary as show below
[{'Transaction_Date': '23/02/22', 'Particulars': 'UPI-RAJESHKUMAR KANOJIA-O112254823B@MAIR', 'Cheque Number': '205482165529', 'ValueDate': '23/02/22', 'Debit': '189', 'Credit': nan, 'Balance': 1939.24, 'IsTransactionStart': True, 'TransactionId': 1}, 
{'Transaction_Date': nan, 'Particulars': 'TEL-AIRP0000001-205482165529-PAYMENT MAD', 'Cheque Number': nan, 'ValueDate': nan, 'Debit': nan, 'Credit': nan, 'Balance': nan, 'IsTransactionStart': False, 'TransactionId': 1}, 
{'Transaction_Date': nan, 'Particulars': 'E TO ME', 'Cheque Number': nan, 'ValueDate': nan, 'Debit': nan, 'Credit': nan, 'Balance': nan, 'IsTransactionStart': False, 'TransactionId': 1}, 
{'Transaction_Date': '24/02/22', 'Particulars': 'UPI-ADD MONEY TO WALLET-ADD-MONEY@PAYTM-', 'Cheque Number': '205599473326', 'ValueDate': '24/02/22', 'Debit': '602', 'Credit': nan, 'Balance': 1337.24, 'IsTransactionStart': True, 'TransactionId': 2}, 
{'Transaction_Date': nan, 'Particulars': 'PYTM0123456-205599473326-NA', 'Cheque Number': nan, 'ValueDate': nan, 'Debit': nan, 'Credit': nan, 'Balance': nan, 'IsTransactionStart': False, 'TransactionId': 2}, 
{'Transaction_Date': '24/02/22', 'Particulars': '.ACH DEBIT RETURN CHARGES 020222 020222-', 'Cheque Number': 'MIR2205429451991', 'ValueDate': '24/02/22', 'Debit': '531', 'Credit': nan, 'Balance': 806.24, 'IsTransactionStart': True, 'TransactionId': 3}, 
{'Transaction_Date': nan, 'Particulars': 'MIR2205429451991', 'Cheque Number': nan, 'ValueDate': nan, 'Debit': nan, 'Credit': nan, 'Balance': nan, 'IsTransactionStart': False, 'TransactionId': 3}]
  • Is there a way to combine list of dictionary with same TransactionId and make a new list as in the expected output.
  • Expected Output
[{'Transaction_Date': '23/02/22', 'Particulars': 'UPI-RAJESHKUMAR KANOJIA-O112254823B@MAIR TEL-AIRP0000001-205482165529-PAYMENT MAD E TO ME', 'Cheque Number': '205482165529', 'ValueDate': '23/02/22', 'Debit': '189', 'Credit': nan, 'Balance': 1939.24, 'IsTransactionStart': True, 'TransactionId': 1}, 
{'Transaction_Date': '24/02/22', 'Particulars': 'UPI-ADD MONEY TO WALLET-ADD-MONEY@PAYTM-PYTM0123456-205599473326-NA', 'Cheque Number': '205599473326', 'ValueDate': '24/02/22', 'Debit': '602', 'Credit': nan, 'Balance': 1337.24, 'IsTransactionStart': True, 'TransactionId': 2}, 
{'Transaction_Date': '24/02/22', 'Particulars': '.ACH DEBIT RETURN CHARGES 020222 020222-MIR2205429451991', 'Cheque Number': 'MIR2205429451991', 'ValueDate': '24/02/22', 'Debit': '531', 'Credit': nan, 'Balance': 806.24, 'IsTransactionStart': True, 'TransactionId': 3}, 

CodePudding user response:

You can use pandas for this:

import pandas as pd
import numpy as np

data = [{'Transaction_Date': '23/02/22', 'Particulars': 'UPI-RAJESHKUMAR KANOJIA-O112254823B@MAIR', 'Cheque Number': '205482165529', 'ValueDate': '23/02/22', 'Debit': '189', 'Credit': np.nan, 'Balance': 1939.24, 'IsTransactionStart': True, 'TransactionId': 1}, 
{'Transaction_Date': np.nan, 'Particulars': 'TEL-AIRP0000001-205482165529-PAYMENT MAD', 'Cheque Number': np.nan, 'ValueDate': np.nan, 'Debit': np.nan, 'Credit': np.nan, 'Balance': np.nan, 'IsTransactionStart': False, 'TransactionId': 1}, 
{'Transaction_Date': np.nan, 'Particulars': 'E TO ME', 'Cheque Number': np.nan, 'ValueDate': np.nan, 'Debit': np.nan, 'Credit': np.nan, 'Balance': np.nan, 'IsTransactionStart': False, 'TransactionId': 1}, 
{'Transaction_Date': '24/02/22', 'Particulars': 'UPI-ADD MONEY TO WALLET-ADD-MONEY@PAYTM-', 'Cheque Number': '205599473326', 'ValueDate': '24/02/22', 'Debit': '602', 'Credit': np.nan, 'Balance': 1337.24, 'IsTransactionStart': True, 'TransactionId': 2}, 
{'Transaction_Date': np.nan, 'Particulars': 'PYTM0123456-205599473326-NA', 'Cheque Number': np.nan, 'ValueDate': np.nan, 'Debit': np.nan, 'Credit': np.nan, 'Balance': np.nan, 'IsTransactionStart': False, 'TransactionId': 2}, 
{'Transaction_Date': '24/02/22', 'Particulars': '.ACH DEBIT RETURN CHARGES 020222 020222-', 'Cheque Number': 'MIR2205429451991', 'ValueDate': '24/02/22', 'Debit': '531', 'Credit': np.nan, 'Balance': 806.24, 'IsTransactionStart': True, 'TransactionId': 3}, 
{'Transaction_Date': np.nan, 'Particulars': 'MIR2205429451991', 'Cheque Number': np.nan, 'ValueDate': np.nan, 'Debit': np.nan, 'Credit': np.nan, 'Balance': np.nan, 'IsTransactionStart': False, 'TransactionId': 3}]

df = pd.DataFrame(data)
# groupby TransactionID, keep all first column items, except for Particulars - join that one
df = df.groupby('TransactionId', as_index=False).agg({'Transaction_Date': 'first', 'Particulars': '-'.join, 'Cheque Number':'first', 'ValueDate':'first', 'Debit':'first', 'Credit':'first', 'Balance':'first', 'IsTransactionStart':'first', 'TransactionId':'first'})

Ouput:

Transaction_Date Particulars Cheque Number ValueDate Debit Credit Balance IsTransactionStart TransactionId
0 23/02/22 UPI-RAJESHKUMAR KANOJIA-O112254823B@MAIR-TEL-AIRP0000001-205482165529-PAYMENT MAD-E TO ME 205482165529 23/02/22 189 nan 1939.24 True 1
1 24/02/22 UPI-ADD MONEY TO WALLET-ADD-MONEY@PAYTM--PYTM0123456-205599473326-NA 205599473326 24/02/22 602 nan 1337.24 True 2
2 24/02/22 .ACH DEBIT RETURN CHARGES 020222 020222--MIR2205429451991 MIR2205429451991 24/02/22 531 nan 806.24 True 3

Convert back to dict:

df.to_dict(orient='records')
  • Related