简体   繁体   中英

How can I repercentage a cell from several data points in Python Pandas?

I've been browsing different questions here on Stackexchange but haven't figured out how to do what I need in Pandas. I think it'll ultimately be pretty simple!

I'm doing a task where a dataset has a bunch of products, and each product has a row for each of the stores it's located in. So, Product A will have individual lines for food, drugstore, Target, Walmart, etc. Then, its availability and the importance of that outlet is multiplied and I need to repercentage that result to equal 100%.

Right now I'm doing it manually in Excel/Google Sheets, but that's annoying and tedious. I can tell how to get the sum total of column E per Product by using Groupby, but I can't figure out how to then make that number appear for each product so that each figure from column E can be divided into it.

Anyone have suggestions? Link to example of what the dataset looks like

To get the sum to show up for every product you want to .transform('sum')

In one line:

df['Repercentaged'] = df.groupby('Product').Multiplied.transform(lambda x: x/x.sum())

But if you want to keep the Sum Column...

import pandas as pd

df['Sum'] = df.groupby('Product').Multiplied.transform('sum')
#    Location  Multiplied Product   Sum
#0       Food        0.09       A  0.88
#1  Drugstore        0.21       A  0.88
#2    Walmart        0.35       A  0.88
#3     Target        0.23       A  0.88
#4       Food        0.13       B  0.73
#5  Drugstore        0.13       B  0.73
#6    Walmart        0.25       B  0.73
#7     Target        0.22       B  0.73

df['Repercentaged'] = df['Multiplied']/df['Sum']

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