How to convert values into percentage of a "total" column into pandas?

Asked

Viewed 2,111 times

0

Hello! I’m learning about Python and Numpy, but I’m having trouble working with percentages. Considering that the total column corresponds to 100%, as I do to obtain the respective percentages of each column with its lines?

inserir a descrição da imagem aqui

  • Good morning, please enter the code you have tried and/or a sample of the data (.csv maybe) to make life easier for those who try to help you. Never put images because it makes it difficult to copy/Paste obviously.

1 answer

0


Try this:

import pandas as pd

df = pd.DataFrame({
    'a': ['Data Analisis', 'Machine Learning', 'Data Visualisation', 'Big Data', 'Deep Learning', 'Data Journalism'],
    'b': [1688, 1629, 1340, 1332, 1263, 429],
    'c': [444, 477, 734, 729, 770, 1081],
    'd': [60, 74, 102, 127, 136, 610],
})

df['Total'] = df.sum(axis=1) # somar valores da linha, caso nao tenha isto logo a partida

# calcular percentagens
df['b %'] = df['b']/df['Total']*100
df['c %'] = df['c']/df['Total']*100
df['d %'] = df['d']/df['Total']*100
print(df)

OUTPUT:

                    a     b     c    d  Total        b %        c %        d %
0       Data Analisis  1688   444   60   2192  77.007299  20.255474   2.737226
1    Machine Learning  1629   477   74   2180  74.724771  21.880734   3.394495
2  Data Visualisation  1340   734  102   2176  61.580882  33.731618   4.687500
3            Big Data  1332   729  127   2188  60.877514  33.318099   5.804388
4       Deep Learning  1263   770  136   2169  58.229599  35.500231   6.270171
5     Data Journalism   429  1081  610   2120  20.235849  50.990566  28.773585

DEMONSTRATION

  • Thank you very much! It’s this kind of operation I’d like to do, and I wasn’t getting it, Miguel!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.