Home > Mobile >  suming columns basis the condition in pandas dataframe
suming columns basis the condition in pandas dataframe

Time:01-19

Existing Dataframe :

Id        amt_1          amt_2          final_1          
01        23             23               29               
02        30             0                30               
03         0             0                10               
04        10             0                10               

Expected Dataframe :

Id        amt_1          amt_2          final_1          final_2
01        23             23               29               29
02        30             0                30               60
03         0             0                10               10
04        10             0                10               20

final_2 = final_1 amt_1(only if amt_2 == 0)

Approach used :

df['final_2'] = df[['amt_1','final_1']].sum(axis=1).where(df['amt_2'] == 0, 0)

not getting the expected output with the same. any leads.?

CodePudding user response:

Use where for the condition, and add with a fill_value=0 to handle the NaNs:

df['final_2'] = df['final_1'].add(df['amt_1'].where(df['amt_2'].eq(0)), fill_value=0)

Output:

   Id  amt_1  amt_2  final_1  final_2
0   1     23     23       29     29.0
1   2     30      0       30     60.0
2   3      0      0       10     10.0
3   4     10      0       10     20.0

CodePudding user response:

Replace amt_1 to 0 by condition in Series.where:

df['final_2'] = df['final_1'].add(df['amt_1'].where(df['amt_2'] == 0, 0))

print (df)
   Id  amt_1  amt_2  final_1  final_2
0   1     23     23       29       29
1   2     30      0       30       60
2   3      0      0       10       10
3   4     10      0       10       20

Or:

df['final_2'] = df['final_1']   df['amt_1'].where(df['amt_2'] == 0, 0)

print (df)
   Id  amt_1  amt_2  final_1  final_2
0   1     23     23       29       29
1   2     30      0       30       60
2   3      0      0       10       10
3   4     10      0       10       20
  • Related