简体   繁体   中英

How can I calculate the percentage of each value in every row?

Here is my dataframe: 在此处输入图像描述

How can I generate a new dataframe that has all the values in percentage? For example, the Retro of Cab Monitor shows the percentage of the sum(Retro + Other + Overtime + Injured + Detail + Quinn). I know there are some ways to calculate the percentage of sum of the column, but I want to get the percentage of sum of the row. Thank you very much!

You can sum along rows and then use the sum to compute percentage easily on every column

df['Total']= df.sum(axis=0)
for col in df.columns:
    df[col] = df[col] / df['Total']

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM