Reputation: 271
I have a dataframe df
I want to calculate the percentage based on the column total
.
Suppose I have:
df = pd.DataFrame({
'ID': range(1, 4),
'col1': [10, 5, 10],
'col2': [15, 10, 15],
'col3': [10, 15, 15],
'total': [35, 30, 40]
})
print(df)
ID col1 col2 col3 total
0 1 10 15 10 35
1 2 5 10 15 30
2 3 10 15 15 40
I want to get:
ID col1 col2 col3 total
0 1 28.57 % 42.85 % 28.57 % 100 %
1 2 16.66 % 33.33 % 50 % 100 %
2 3 25 % 37.5% 37.5 % 100 %
Upvotes: 6
Views: 10344
Reputation: 342
df = pd.DataFrame({
'ID': range(1, 4),
'col1': [10, 5, 10],
'col2': [15, 10, 15],
'col3': [10, 15, 15],
'total': [35, 30, 40]
})
cols = ['col1', 'col2', 'col3', 'total']
for col in cols:
df[col] = (df[col]/df['total']*100).round(2).astype(str) + ' %'
Output
ID col1 col2 col3 total
0 1 28.57 % 42.86 % 28.57 % 100.0 %
1 2 16.67 % 33.33 % 50.0 % 100.0 %
2 3 25.0 % 37.5 % 37.5 % 100.0 %
Upvotes: 1
Reputation: 18406
You can use div
or divide
, and perform division on bulk column and assign them back, and in case if you don't know the index of the column, you can just use the column labels.
cols = ['col1', 'col2', 'col3', 'total']
df[cols] = df[cols].divide(df['total'], axis=0)*100
OUTPUT:
ID col1 col2 col3 total
0 1 28.571429 42.857143 28.571429 100.0
1 2 16.666667 33.333333 50.000000 100.0
2 3 25.000000 37.500000 37.500000 100.0
If you want the values to round and to be represented as string with %
sign, you can just use round
and convert it to string and add the %
sign
df[cols] = (df[cols].divide(df['total'], axis=0)*100).round(2).astype(str) + ' %'
OUTPUT:
ID col1 col2 col3 total
0 1 28.57 % 42.86 % 28.57 % 100.0 %
1 2 16.67 % 33.33 % 50.0 % 100.0 %
2 3 25.0 % 37.5 % 37.5 % 100.0 %
Upvotes: 2
Reputation: 78650
Use
>>> df.iloc[:, 1:] = df.iloc[:, 1:].div(df['total'], axis=0).mul(100).round(2).astype(str).add(' %')
>>> df
ID col1 col2 col3 total
0 1 28.57 % 42.86 % 28.57 % 100.0 %
1 2 16.67 % 33.33 % 50.0 % 100.0 %
2 3 25.0 % 37.5 % 37.5 % 100.0 %
Upvotes: 8
Reputation: 1644
You can use:
import pandas as pd
df = pd.DataFrame({
'ID': range(1, 4),
'col1': [10, 5, 10],
'col2': [15, 10, 15],
'col3': [10, 15, 15],
'total': [35, 30, 40]
})
idx = ['col1', 'col2', 'col3', 'total']
df[idx] = df[idx].apply(lambda x: x / x['total'], axis=1)
df
which gives you:
| | ID | col1 | col2 | col3 | total |
|---:|-----:|---------:|---------:|---------:|--------:|
| 0 | 1 | 0.285714 | 0.428571 | 0.285714 | 1 |
| 1 | 2 | 0.166667 | 0.333333 | 0.5 | 1 |
| 2 | 3 | 0.25 | 0.375 | 0.375 | 1 |
Upvotes: 3
Reputation: 1260
import pandas as pd
df = pd.DataFrame({
'ID': range(1, 4),
'col1': [10, 5, 10],
'col2': [15, 10, 15],
'col3': [10, 15, 15],
'total': [35, 30, 40]
})
df['col1'] = (df['col1']/df['total']).mul(100).round(2).astype(str).add('%')
df['col2'] = (df['col2']/df['total']).mul(100).round(2).astype(str).add('%')
df['col3'] = (df['col3']/df['total']).mul(100).round(2).astype(str).add('%')
df['total'] = (df['total']/df['total']).mul(100).round(2).astype(str).add('%')
print(df)
prints
ID col1 col2 col3 total
0 1 28.57% 42.86% 28.57% 100.0%
1 2 16.67% 33.33% 50.0% 100.0%
2 3 25.0% 37.5% 37.5% 100.0%
Upvotes: 0