Home > OS >  How to filter the pandas dataframe, apply calculation to the column and return it back to the full d
How to filter the pandas dataframe, apply calculation to the column and return it back to the full d

Time:11-02

Suppose I have the following pandas dataframe.

df

Group      Class    Data           
a          x        5                                                   
b          y        4                                              
a          y        3   
a          x        2
b          y        1    
b          x        7
...

I want to filter the rows and apply calculations to the column:

  • divide Data by 2 for those in Group a and Class x
  • divide Data by 3 for those in Group a and Class y
  • divide Data by 4 for those in Group b and Class x
  • divide Data by 5 for those in Group b and Class y

Finally, I want to return the calculations to the original dataframe df

Is there a simple way to do the above?

CodePudding user response:

df["newcolumn"] =df.apply(functionname, axis=1)

def functionname (row) : if (row["Group"]==a and row["Class"]==x): return row["Data"] /2

add all conditions in this way

CodePudding user response:

  1. create a mapping series df[['Group', 'Class']].apply(tuple, axis=1).map(mapping)
  2. divide data with this series

In [119]: df
Out[119]:
  Group Class  Data
0     a     x     5
1     b     y     4
2     a     y     3
3     a     x     2
4     b     y     1
5     b     x     7

In [120]: mapping = {('a', 'x'): 2, ('a', 'y'): 3, ('b', 'x'): 4, ('b', 'y'): 5}
     ...: df['Data_calculated'] = df['Data'] / df[['Group', 'Class']].apply(tuple, axis=1).map(mapping)

In [121]: df
Out[121]:
  Group Class  Data  Data_calculated
0     a     x     5             2.50
1     b     y     4             0.80
2     a     y     3             1.00
3     a     x     2             1.00
4     b     y     1             0.20
5     b     x     7             1.75
  • Related