Reputation: 621
The table on the left is the original dataframe. The Table on the right is the desired dataframe.
The values in column [0, 1, 2] are percentages and need to be multiplied by the column "Total Cost" for that particular row.
By doing so, you get the desired answer table on the right.
It seems like an element wise multiplication of df.row[0, 1, 2] * df.row[Total Cost]
But not sure how to do this with pandas
dict version of simplified dataframe provided below
{0: {"Nov '18": 0.1666471015536077, "Dec '18": 0.5403863967743445, "Jan '19": 0.5362702245675458, "Feb '19": 0.3538342118892141, "Mar '19": 0.6068213241958712, "Apr '19": 0.6959594096743349, "May '19": 0.682575498865738}, 1: {"Nov '18": 0.2993902407933448, "Dec '18": 0.44429158402908286, "Jan '19": 0.3729695419273137, "Feb '19": 0.3980823560973494, "Mar '19": 0.3200835471705221, "Apr '19": 0.29763667231002056, "May '19": 0.2840070502525354}, 2: {"Nov '18": 0.5337308848310992, "Dec '18": 0.013817091931355035, "Jan '19": 0.07008689274226004, "Feb '19": 0.10680130054564026, "Mar '19": 0.06818860955654642, "Apr '19": 0.004524516700862339, "May '19": 0.004338865464848797}, 'xTrader (838)': {"Nov '18": 75319.0, "Dec '18": 42484.39, "Jan '19": 40484.71, "Feb '19": 40470.29, "Mar '19": 66609.0, "Apr '19": 71057.87999999999, "May '19": 89627.88}}
{0: {'Owner': 'system_voy', 'App': 'Voyager', 'LOB': 'Risk Management: Capital Markets', 'Transit': '83534', "Nov '18": 0.1666471015536077, "Dec '18": 0.5403863967743445, "Jan '19": 0.5362702245675458, "Feb '19": 0.3538342118892141, "Mar '19": 0.6068213241958712, "Apr '19": 0.6959594096743349, "May '19": 0.682575498865738, "Jun '19": 0.7032990347937492}, 1: {'Owner': 'eu\\xtradereod', 'App': 'xTrader', 'LOB': 'Capital Markets: Global Markets', 'Transit': '75088', "Nov '18": 0.2993902407933448, "Dec '18": 0.44429158402908286, "Jan '19": 0.3729695419273137, "Feb '19": 0.3980823560973494, "Mar '19": 0.3200835471705221, "Apr '19": 0.29763667231002056, "May '19": 0.2840070502525354, "Jun '19": 0.2929727958768866}, 2: {'Owner': 'eu\\system_xtrader2', 'App': 'xTrader', 'LOB': 'Capital Markets: Global Markets', 'Transit': '75088', "Nov '18": 0.5337308848310992, "Dec '18": 0.013817091931355035, "Jan '19": 0.07008689274226004, "Feb '19": 0.10680130054564026, "Mar '19": 0.06818860955654642, "Apr '19": 0.004524516700862339, "May '19": 0.004338865464848797, "Jun '19": 0.0027272448226331497}, 3: {'Owner': 'mr-tech', 'App': 'FRTB', 'LOB': 'Risk Management: Capital Markets', 'Transit': '83534', "Nov '18": 4.021308836676355e-06, "Dec '18": 7.853538029670704e-05, "Jan '19": 0.015370002324550705, "Feb '19": 0.11787934038028858, "Mar '19": 1.5161864573662851e-07, "Apr '19": 1.0092819280702894e-06, "May '19": 9.714219073341933e-06, "Jun '19": 1.1635748117981739e-07}, 4: {'Owner': 'eu\\system_xtsup_prd', 'App': 'xTrader', 'LOB': 'Capital Markets: Global Markets', 'Transit': '75088', "Nov '18": 0, "Dec '18": 0, "Jan '19": 0, "Feb '19": 0.021433060967667138, "Mar '19": 0, "Apr '19": 0, "May '19": 0.016256659135696943, "Jun '19": 0}, 5: {'Owner': 'xt-tech', 'App': 'xTrader', 'LOB': 'Capital Markets: Global Markets', 'Transit': '75088', "Nov '18": 0.00022774976090734464, "Dec '18": 2.212229303038311e-06, "Jan '19": 0.004022482749891066, "Feb '19": 0.00011334322251753845, "Mar '19": 0.0036268312234368394, "Apr '19": 4.7611888584087586e-05, "May '19": 0.0103897652257289, "Jun '19": 0.0010008081492497863}, 6: {'Owner': 'ad\\watb', 'App': 'CVATrader', 'LOB': 'Capital Markets: RMG', 'Transit': '91707', "Nov '18": 0, "Dec '18": 0, "Jan '19": 0.0012585476083139418, "Feb '19": 0.0017582009987088963, "Mar '19": 0.001275486891583217, "Apr '19": 0.0015783820251811566, "May '19": 0.0006181777165474082, "Jun '19": 0}, 7: {'Owner': 'ad\\xustev', 'App': 'xTrader', 'LOB': 'Capital Markets: Global Markets', 'Transit': '75088', "Nov '18": 0, "Dec '18": 0.0014241796556178747, "Jan '19": 2.2308080124760536e-05, "Feb '19": 9.818589861410218e-05, "Mar '19": 4.049343394433275e-06, "Apr '19": 0.00025239811908896236, "May '19": 0.0006735771849304808, "Jun '19": 0}, 8: {'Owner': 'ad\\cvatrader', 'App': 'CVATrader', 'LOB': 'Capital Markets: RMG', 'Transit': '91707', "Nov '18": 0, "Dec '18": 0, "Jan '19": 0, "Feb '19": 0, "Mar '19": 0, "Apr '19": 0, "May '19": 0.0011116369831956367, "Jun '19": 0}, 9: {'Owner': 'ad\\mccloske', 'App': 'xTrader', 'LOB': 'Capital Markets: Global Markets', 'Transit': '75088', "Nov '18": 0, "Dec '18": 0, "Jan '19": 0, "Feb '19": 0, "Mar '19": 0, "Apr '19": 0, "May '19": 1.905495170508051e-05, "Jun '19": 0}, 10: {'Owner': 'anonymous', 'App': 'xTrader', 'LOB': 'Capital Markets: Global Markets', 'Transit': '75088', "Nov '18": 1.752204286133488e-09, "Dec '18": 0, "Jan '19": 0.0, "Feb '19": 0, "Mar '19": 0, "Apr '19": 0, "May '19": 0, "Jun '19": 0}, 'xTrader (838)': {'Owner': 0.0, 'App': 0.0, 'LOB': 0.0, 'Transit': 0.0, "Nov '18": 75319.0, "Dec '18": 42484.39, "Jan '19": 40484.71, "Feb '19": 40470.29, "Mar '19": 66609.0, "Apr '19": 71057.87999999999, "May '19": 89627.88, "Jun '19": 0.0}}
Upvotes: 1
Views: 83
Reputation: 4273
Another alternative to update the dataframe in-place is to operate on the underlying numpy ndarray directly.
df.values[:, :-1] *= df.values[:, [-1]]
If you want to create a new dataframe rather than updating in-place, you could do
df_new = df.iloc[:, :-1].mul(df.iloc[:, -1], axis=0).join(df.iloc[:, -1])
Upvotes: 0
Reputation: 75080
IIUC, you need df.mul()
with df.iloc[]
:
data.iloc[:,:-1]=data.iloc[:,:-1].mul(data.iloc[:,-1],axis=0)
print(data)
0 1 2 xTrader (838)
Apr '19 49453.400218 21149.430945 321.502565 71057.88
Dec '18 22957.986431 18875.456930 587.010722 42484.39
Feb '19 14319.773167 16110.508395 4322.279605 40470.29
Jan '19 21710.744523 15099.563744 2837.447527 40484.71
Mar '19 40419.761583 21320.444993 4541.975094 66609.00
May '19 61177.794903 25454.949819 388.883313 89627.88
Nov '18 12551.693042 22549.773546 40200.076515 75319.00
Note: the sum of the provided data is 0.9981205987006647
, this is the reason why the sum of each row doesn't match the last. Else, this logic should work.
Upvotes: 1