I have a pandas DataFrame
A
foo one 3
two 2
three 4
bar one 1
two 5
three 2
I want to add a column with relative values for each row, based on the sum of all rows from the first index level. The sum for all values in foo
would be 3+2+4=9, so the relative value for the first row would be 3/9 = 0.33.
The resulting DataFrame
would look like that:
A rel
foo one 3 0.33
two 2 0.22
three 4 0.44
bar one 1 0.125
two 5 0.625
three 2 0.25
I already tried to use groupby on the DataFrame
, but i can only figure out how to apply a method to one groupby dimension.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.