Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 16420

Row based filter and aggregation in pandas python

$
0
0

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 from df1['indi_val] equal "_E", get sum(Amt).
  • Without E: where last 2 characters from df1['indi_val'] do not equal "_E", get sum(Amt).
  • Normal: without any filter on df1['indi_val'], get sum(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'

Viewing all articles
Browse latest Browse all 16420

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>