简体   繁体   English

Pandas 根据条件查找列平均值

[英]Pandas find column average based on condition

Dataframe has two columns item-1 and item-2, I want to find average of Buy and Sell columns for all the rows where item-1 value is in item-2 and item-2 value is in item-1 Dataframe 有两列 item-1 和 item-2,我想找到 item-1 值在 item-2 中且 item-2 值在 item-1 中的所有行的 Buy 和 Sell 列的平均值

在此处输入图像描述

Here the value of first row in item-1 is A, item-2 has value A at index 2,4 and 6, so the avg-item-1-buy should be average of buy value at those indexes这里 item-1 中第一行的值为 A,item-2 在索引 2,4 和 6 处的值为 A,因此 avg-item-1-buy 应该是这些索引处购买值的平均值

similarly the value of first row in item-2 is C, item-1 has value C at index 2 and 5, so the avg-item-2-buy should be average of buy value at those indexes同样,item-2 中第一行的值是 C,item-1 在索引 2 和 5 处具有值 C,因此 avg-item-2-buy 应该是这些索引处购买值的平均值

Expected Result:预期结果:

在此处输入图像描述

I don't think this is the best answer but this surely will do it (i have try it).我不认为这是最好的答案,但这肯定会做到(我已经尝试过了)。

First we calculate the average and store to the dictionary.首先我们计算平均值并存储到字典中。 I made 4 dictionary for each desired columns.我为每个所需的列制作了 4 个字典。

def count_avg(col_1, col_2, target_col):
    col_avg= {}
    for i in df[col_1].unique():
        col_avg[i]= df[df[col_2]==i][target_col].mean()

    return col_avg


item1_avg_buy_dict= count_avg('Item-1', 'Item-2', 'buy')
item1_avg_sell_dict= count_avg('Item-1', 'Item-2', 'sell')

item2_avg_buy_dict= count_avg('Item-2', 'Item-1', 'buy')
item2_avg_sell_dict= count_avg('Item-2', 'Item-1', 'sell')

And then we create a function to pair x with the dictionary we just created.然后我们创建一个 function 来将 x 与我们刚刚创建的字典配对。

def get_avg(x, dict_):
    try:
        res= dict_[x]
    except:
        res= None

    return res

And then we can apply each new columns for that function by .map()然后我们可以通过.map()为该 function 应用每个新列

df['avg-item1-buy']= df['Item-1'].map(lambda x: get_avg(x, item1_avg_buy_dict))
df['avg-item1-sell']= df['Item-1'].map(lambda x: get_avg(x, item1_avg_sell_dict))
df['avg-item2-buy']= df['Item-2'].map(lambda x: get_avg(x, item2_avg_buy_dict))
df['avg-item2-sell']= df['Item-2'].map(lambda x: get_avg(x, item2_avg_sell_dict))

输出df

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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