Group by and apply custom function in pandas data frame

RGRGRG

I have a df as below:

enter image description here

I would like to group by id and flag and create a new column in the df which is the result of: [sum(value1)/sum(value2)] * 12. Therefore I will need the result to be:

enter image description here

I have created a function:

 `def calculation (value1, value2):

       result = (value1/value2) * 12

       return(result)`

Could you advise which is the best way to apply this function along with the grouping, in order to get the desired output? Many thanks

Ranjith kumar

The following code should work.

import pandas as pd
df = pd.DataFrame({"id" : [1,1,2,2],"flag":["A","B","B","A"],"value1":[520,200,400,410],"value2":[12,5,11,2]})
def calculation(value1, value2):
    result = (value1/value2) * 12
    return(result)
df.groupby(['id','flag']).apply(lambda x: calculation(x['value1'],x['value2'])).astype(int)

You just have to use the following for groupby and apply.

df.groupby(['id','flag']).apply(lambda x: calculation(x['value1'],x['value2'])).astype(int)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to apply custom function to pandas data frame for each row

From Dev

How to apply scipy function on Pandas data frame

From Dev

Run a custom function on a data frame in R, by group

From Dev

Apply function in data frame

From Dev

Apply autocorrelation function acf() to elements of set of vectors by group in a data frame

From Dev

Apply custom function to two columns for every row in data frame in R

From Dev

Apply custom function to each subset of a data frame and result a dataframe

From Dev

Apply custom function to each subset of a data frame and result a dataframe

From Dev

How to properly apply a lambda function into a pandas data frame column

From Dev

Apply function row wise on pandas data frame on columns with numerical values

From Dev

function won't apply to pandas data frame, getting syntax error

From Dev

ValueError when apply function to data frame in python-pandas

From Dev

Apply multivariate custom function by group

From Dev

Apply function to subset of data frame

From Dev

Apply a function data frame column

From Dev

How to create a data frame for each group in the pandas.groupby function?

From Dev

Can I apply a function that uses 'shift' on a grouped data frame, and return a simple data frame from pandas?

From Dev

Can I apply a function that uses 'shift' on a grouped data frame, and return a simple data frame from pandas?

From Dev

Pandas group by custom function

From Dev

MultiIndex Group By in Pandas Data Frame

From Dev

GROUP BY SUM ON pandas data frame

From Dev

Apply functions on each group in Data Frame

From Dev

R: Apply function on data frame A dependent on values of data frame B

From Dev

R Apply function on data frame columns

From Dev

apply a function in data frame with if/else statement in R

From Dev

apply a function in data frame with if/else statement in R

From Dev

Apply function to heterogeneous rows of data.frame

From Dev

Apply function to column of data frame (column is a list)

From Dev

how to apply duplicated function to the columns of a data frame

Related Related

  1. 1

    How to apply custom function to pandas data frame for each row

  2. 2

    How to apply scipy function on Pandas data frame

  3. 3

    Run a custom function on a data frame in R, by group

  4. 4

    Apply function in data frame

  5. 5

    Apply autocorrelation function acf() to elements of set of vectors by group in a data frame

  6. 6

    Apply custom function to two columns for every row in data frame in R

  7. 7

    Apply custom function to each subset of a data frame and result a dataframe

  8. 8

    Apply custom function to each subset of a data frame and result a dataframe

  9. 9

    How to properly apply a lambda function into a pandas data frame column

  10. 10

    Apply function row wise on pandas data frame on columns with numerical values

  11. 11

    function won't apply to pandas data frame, getting syntax error

  12. 12

    ValueError when apply function to data frame in python-pandas

  13. 13

    Apply multivariate custom function by group

  14. 14

    Apply function to subset of data frame

  15. 15

    Apply a function data frame column

  16. 16

    How to create a data frame for each group in the pandas.groupby function?

  17. 17

    Can I apply a function that uses 'shift' on a grouped data frame, and return a simple data frame from pandas?

  18. 18

    Can I apply a function that uses 'shift' on a grouped data frame, and return a simple data frame from pandas?

  19. 19

    Pandas group by custom function

  20. 20

    MultiIndex Group By in Pandas Data Frame

  21. 21

    GROUP BY SUM ON pandas data frame

  22. 22

    Apply functions on each group in Data Frame

  23. 23

    R: Apply function on data frame A dependent on values of data frame B

  24. 24

    R Apply function on data frame columns

  25. 25

    apply a function in data frame with if/else statement in R

  26. 26

    apply a function in data frame with if/else statement in R

  27. 27

    Apply function to heterogeneous rows of data.frame

  28. 28

    Apply function to column of data frame (column is a list)

  29. 29

    how to apply duplicated function to the columns of a data frame

HotTag

Archive