Python Forum
Add a row per group and depending of values - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Add a row per group and depending of values (/thread-39637.html)



Add a row per group and depending of values - Menthix - Mar-20-2023

Hello,
I have this dataframe (see capture 1).

And I 'd like to add one row per group of the Var_1 such as I have a new value Var_2=3. And the value in var_4 would be equal to (Var_5 value of the group - sum of the values of the Var_4).
So i would end up with the df in capture 2.

I'm not sure if this is clear but the capture 2 will give you a good idea of what I m looking for. And I'd like to automate this for each Var_1 group (so i'd like not to enter the raw values "A", "B" and "C" in the code).

I thank you very much by advance for your help !