I have two dataframes as below.
df1:
data1 = {'Acc': [1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4],'indi_val': ['Val1', 'val2', 'Val_E', 'Val1_E', 'Val1', 'Val3', 'val2', 'val2_E', 'val22_E', 'val2_A', 'val2_V', 'Val_E', 'Val_A', 'Val', 'Val2', 'val7'],'Amt': [10, 20, 5, 5, 22, 38, 15, 25, 22, 23, 24, 56, 67, 45, 87, 88]}df1 = pd.DataFrame(data1)
df2:
data2 = {'Acc': [1, 1, 2, 2, 3, 4],'Indi': ['With E', 'Without E', 'With E', 'Without E', 'Normal', 'Normal']}df2 = pd.DataFrame(data2)
Based on these two dataframes I need to create final output as below:
AccNo Indi Amt 1 With E 7 1 Without E 90 2 With E 47 2 Without E 62 3 Normal 225 4 Normal 88
The logic:
with E
: where last 2 characters fromdf1['indi_val]
equal "_E", getsum(Amt)
.Without E
: where last 2 characters fromdf1['indi_val']
do not equal "_E", getsum(Amt)
.Normal
: without any filter ondf1['indi_val']
, getsum(Amt)
.
I tried writing something as below:
def get_indi(row): listval = [] if row['Indi'] == "With E": #print('A') df1.apply(lambda df1row: listval.append(df1row['amt'] if df1row['Acc']==row['Acc'] and df1row['indi_val'][-2:]=="_E" else 0)) if row['Indi'] == "Without E": df1.apply(lambda df1row: listval.append(df1row['amt'] if df1row['Acc']==row['Acc'] and df1row['indi_val'][-2:]!="_E" else 0)) if row['Indi'] == "Normal": df1.apply(lambda df1row: listval.append(df1row['amt'])) return sum(listval)# Apply the function to create the 'Indi' column in df1df2['Amt'] = df2.apply(get_indi)
With above code I am getting the following error:
get_loc raise KeyError(key)KeyError: 'Indi'