'pandas dividing rows by its total
I have this df:
Name num1 num2 num3
A 1 2 3
B 4 5 6
C 7 8 9
My goal is to divide each row by the total. This is what I came up with:
df.loc[:,"num1":"num3"] = df.loc[:,"num1":"num3"].div(df["total"], axis=0)
It works well. However, if there are more "numx" columns added in after num3, I would have to manually update the code to "num1":"numx". Is there a way to work around this?
Solution 1:[1]
first select matching columns:
In [21]: cols = df.columns[df.columns.str.contains('^num\d+')]
then divide elements in those rows by their sum (calculated for each row):
In [22]: df[cols] = df[cols].div(df[cols].sum(axis=1), axis=0)
In [23]: df
Out[23]:
Name num1 num2 num3
0 A 0.166667 0.333333 0.500
1 B 0.266667 0.333333 0.400
2 C 0.291667 0.333333 0.375
Solution 2:[2]
You need select_dtypes
df.loc[:,df.select_dtypes(exclude='object').columns]=df.select_dtypes(exclude='object').div(df.select_dtypes(exclude='object').sum(1),0)
df
Out[66]:
Name num1 num2 num3
0 A 0.166667 0.333333 0.500
1 B 0.266667 0.333333 0.400
2 C 0.291667 0.333333 0.375
Solution 3:[3]
You can use apply function and apply the division on each row. It'll give same results.
df = df.apply(lambda x = x.div(x.sum()), axis=1)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | MaxU - stop genocide of UA |
Solution 2 | BENY |
Solution 3 | Aditya Sharma |