apply a function to a pandas Dataframe whose returned value is based on other rows

前端 未结 1 1413
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-19 04:01

I have a Dataframe looking like this:

>>> import pandas
>>> df = pandas.DataFrame({\'region\' : [\'east\', \'west\', \'south\', \'west\',
.         


        
1条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-19 04:34

    Ok, I think this does what you want:

    Make a dictionary of your regional weights:

    In [1]: weights = {'east':1,'west':2,'south':3}
    

    The following function maps values from a Series to the value found in the weights dictionary. x is the row value of region and w is the region series after it has been mapped to the weights dict.

    In [2]: def f(x):
       ...:     w = x.map(weights)
       ...:     return w / w.sum().astype(float)
    

    Here, we groupby ['item','price'] and apply the function above. The output is a series of relative weights for the unique combinations of item and price.

    In [3]: df.groupby(['item','price']).region.apply(f)
    Out[3]:
    0    0.333333
    1    0.666667
    2    1.000000
    3    1.000000
    4    0.333333
    5    0.666667
    6    1.000000
    7    1.000000
    

    Finally, you can multiply df.quantity by the above series to calculate your weight-adjusted quantities.

    In [4]: df['wt_quant'] = df.groupby(['item','price']).region.apply(f) * df.quantity
    
    In [5]: df
    Out[5]:
        item  price  quantity region  wt_quant
    0    one     50         3   east  1.000000
    1    one     50         3   west  2.000000
    2    two     12         4  south  4.000000
    3  three     35         5   west  5.000000
    4    two     10        12   east  4.000000
    5    two     10        14   west  9.333333
    6    one     12         3   east  3.000000
    7  three     12         8   west  8.000000
    

    0 讨论(0)
提交回复
热议问题