逐行将多列与特定列进行逐元素乘法

时间:2019-06-04 17:39:06

标签: pandas dataframe

  • 左侧的表是原始数据框。右侧的表是所需的数据框。

  • 列[0,1,2]中的值是百分比,需要为 乘以该特定行的“总费用”列。

  • 这样做,您会在右侧获得所需的答案表。

  • 似乎是df.row [0,1,2] *的逐元素乘法* df.row [总费用]

  • 但不确定如何使用熊猫

  • 下面提供的简化数据框的字典版本

enter image description here     

{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}}

2 个答案:

答案 0 :(得分:1)

IIUC,您需要df.mul()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

注意:所提供数据的总和为0.9981205987006647,这就是每行总和与最后一行不匹配的原因。否则,这种逻辑应该起作用。

答案 1 :(得分:0)

另一种就地更新数据帧的替代方法是直接对基础的numpy ndarray进行操作。

*imports*
urlpatterns = [
    path('',PersonListView.as_view(),name='persons),
    path('new/',PersonCreateView.as_view(),name='person-create),
]

如果您要创建新的数据框而不是就地更新,则可以

df.values[:, :-1] *= df.values[:, [-1]]