Assuming that Date will have NaN if the row needs to be merged, here's the code.
first create a dummy column merged. It will merge all the values of Description, Debit, and Credit. It will only merge if the value is alpha (excludes numeric values)
Then replace Description by using groupby transform (lambda) function.
Then dropna if rows have Date as NaN. Also drop the temp column merged.
df['merged'] = df[['Description','Debit','Credit']].apply(lambda x: ''.join([str(a) for a in x if pd.notnull(a) and not isinstance(a, float)]) ,axis=1)
df['Description'] = df.groupby("originalIdx")['merged'].transform(lambda x: "".join(x))
df.dropna(subset=['Date'],inplace=True)
df.drop(columns='merged',inplace=True)
print (df)
This will give you:
Date Description Debit Credit Balance originalIdx
0 01-03-19 AAAA NaN NaN 49Cr 0
1 01-03-19 ASSSXYZABCsaa NaN 6000.0 55Cr 1
3 01-03-19 ABZ 289.0 NaN 55Cr 3
Here's the full code with data and output.
Replace your df['merged'] with the below code:
df['merged'] = df[['Description','Debit','Credit']].apply(lambda x: ''.join([str(a) for a in x if pd.notnull(a) and not isinstance(a, float)]) ,axis=1)
Full Code is:
import pandas as pd
import numpy as np
pd.set_option('display.max_columns', 200)
pd.set_option('display.max_colwidth', 250)
c = ['Date','Description','Debit','Credit','Balance','originalIdx']
d = [['01-03-19','FORTAP-MUMBAI/',np.NaN, np.NaN, '49656.25Cr',0],
['01-03-19','FORTAP-MUMBAI/******',np.NaN,6000.00,'55656.25Cr',1],
[np.NaN,'UP/*ABC*/*DEF*','UPI/*PQR*/*XYZ*','paytm/NA',np.NaN,1],
['01-03-19','MBK/*ABCDEF*/*ZZZ*',289.00,np.NaN,'55357.25Cr',3]]
df = pd.DataFrame(d,columns=c)
print (df)
df['merged'] = df[['Description','Debit','Credit']].apply(lambda x: ''.join([str(a) for a in x if pd.notnull(a) and not isinstance(a, float)]) ,axis=1)
df['Description'] = df.groupby("originalIdx")['merged'].transform(lambda x: "".join(x))
df.dropna(subset=['Date'],inplace=True)
df.drop(columns='merged',inplace=True)
print (df)
Before and after output attached:
Before:
Date Description Debit Credit Balance originalIdx
0 01-03-19 FORTAP-MUMBAI/ NaN NaN 49656.25Cr 0
1 01-03-19 FORTAP-MUMBAI/****** NaN 6000.0 55656.25Cr 1
2 NaN UP/*ABC*/*DEF* UPI/*PQR*/*XYZ* paytm/NA NaN 1
3 01-03-19 MBK/*ABCDEF*/*ZZZ* 289.0 NaN 55357.25Cr 3
After:
Date Description Debit Credit Balance originalIdx
0 01-03-19 FORTAP-MUMBAI/ NaN NaN 49656.25Cr 0
1 01-03-19 FORTAP-MUMBAI/******UP/*ABC*/*DEF*UPI/*PQR*/*XYZ*paytm/NA NaN 6000.0 55656.25Cr 1
3 01-03-19 MBK/*ABCDEF*/*ZZZ* 289.0 NaN 55357.25Cr 3