Skip to content
Advertisement

How do i write this in python and preferably in pandas?(Assume that i am dealing with a dataframe)

This is the code that i am trying to convert to Pandas:

Assume the following column for input data frame: geo | region | sub region | txn_date | revenue | profit.

Columns in output dataframe : geo | region | ytd_rev | py_ytd_rev| total_profit

Advertisement

Answer

I believe you need GroupBy.agg with named aggregation and new columns created in DataFrame.assign:

Creating sample dataframe:

The sample dataframe:

The solution to the problem:

The final output:

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement