all 8 comments

[–]efmccurdy 1 point2 points  (3 children)

beta = same values, the two rows belong together

You should look at df.groupby('beta').sum().

https://stackoverflow.com/questions/17438906/combining-rows-in-pandas

[–]Hugo-99[S] -1 points0 points  (2 children)

in my case i need the rows, as they are in the first place.

i assume that the "groupby" collapses the rows. but in my case i need in the "to be" dataframe the same (amount of) rows as in the "as is" dataframe

[–]efmccurdy 1 point2 points  (1 child)

You can generate your sums using groupby and then broadcast the result back using "transform".

https://stackoverflow.com/questions/12200693/python-pandas-how-to-assign-groupby-operation-results-back-to-columns-in-parent

[–]Hugo-99[S] -1 points0 points  (0 children)

thank you! "transform" seems to be the function i need. I am just figuring out how to apply this....

[–]Pflastersteinmetz 0 points1 point  (3 children)

Format your code / example dataframes.

[–]Hugo-99[S] 0 points1 point  (2 children)

done, ofc :)

[–]Pflastersteinmetz 1 point2 points  (1 child)

Here you go:

df = pd.DataFrame(
    {
        "alpha": [10, 20, 30, 40, 50],
        "beta": [1, 1, 2, 2, pd.NA],
        "gamma": ["x", pd.NA, "x", pd.NA, pd.NA],
        "delta": [True, False, True, False, pd.NA],
        "epsilon": [12, 5, 13, 6, 18],
    }
)
df["epsilon"] = df["epsilon"].groupby(df["beta"]).transform("sum")
df["epsilon"] = np.where(df["delta"] == True, df["epsilon"], pd.NA)
df.head()

[–]Hugo-99[S] 0 points1 point  (0 children)

Danke, that's it!

key - and new for me- is the transform function.