Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 用于计算满足特定条件的加权百分比_Python_Pandas_Group By - Fatal编程技术网

Python 用于计算满足特定条件的加权百分比

Python 用于计算满足特定条件的加权百分比,python,pandas,group-by,Python,Pandas,Group By,我有一个数据框,上面有这样的调查数据,每一行都是不同的受访者 weight race Question_1 Question_2 Question_3 0.9 white 1 5 4 1.1 asian 5 4 3 0.95 white 2

我有一个数据框,上面有这样的调查数据,每一行都是不同的受访者

weight    race      Question_1      Question_2      Question_3
   0.9   white               1               5               4
   1.1   asian               5               4               3
  0.95   white               2               1               5
  1.25   black               5               4               3
  0.80   other               4               5               2
每个问题的评分范围为1到5(实际数据中还有几个问题)。对于每一个问题,我试图计算回答为5分的受访者的百分比,按种族分组并按权重列加权

df.groupby('race').apply(lambda x: ((x == 5).sum()) / x.count())
我相信下面的代码可以计算每个问题回答5分的百分比,按种族分组。但我不知道如何通过重量栏来称量它

df.groupby('race').apply(lambda x: ((x == 5).sum()) / x.count())
我对熊猫不熟悉。有人能解释一下怎么做吗?谢谢你的帮助

编辑:上述数据帧的所需输出如下所示。显然,真实的数据有更多的受访者(行)和更多的问题

        Question_1      Question_2      Question_3
white         0.00            0.49            0.51
black         1.00            0.00            0.00
asian         1.00            0.00            0.00
other         0.00            1.00            0.00   
# Define a dummy indicating a '5 response'
df['Q1'] = np.where(df['Question_1']==5 ,1, 0)

# Create a weighted version of the above dummy
df['Q1_w'] = df['Q1'] * df['weight']

# Compute the sum by race
ds = df.groupby(['race'])[['Q1_w', 'weight']].sum()

# Compute the weighted average
ds['avg'] = ds['Q1_w'] / ds['weight']

谢谢。

以下是问题1的解决方法。你可以很容易地把它推广到其他问题上

        Question_1      Question_2      Question_3
white         0.00            0.49            0.51
black         1.00            0.00            0.00
asian         1.00            0.00            0.00
other         0.00            1.00            0.00   
# Define a dummy indicating a '5 response'
df['Q1'] = np.where(df['Question_1']==5 ,1, 0)

# Create a weighted version of the above dummy
df['Q1_w'] = df['Q1'] * df['weight']

# Compute the sum by race
ds = df.groupby(['race'])[['Q1_w', 'weight']].sum()

# Compute the weighted average
ds['avg'] = ds['Q1_w'] / ds['weight']
基本上,首先按种族计算权重和加权
5假人的权重之和,然后除以权重之和。

这将为您提供加权平均值。

这里有一个解决方案,通过定义一个自定义函数并将该函数应用于每列。然后,您可以将每个列连接到一个数据帧中:

def wavg(x, col):
    return (x['weight']*(x[col]==5)).sum()/x['weight'].sum()

grouped = df.groupby('race')
pd.concat([grouped.apply(wavg,col) for col in df.columns if col.startswith('Question')],axis=1)\
    .rename(columns = {num:f'Question_{num+1}' for num in range(3)})
输出:

        Question_1  Question_2  Question_3
race            
asian   1.0         0.000000    0.000000
black   1.0         0.000000    0.000000
other   0.0         1.000000    0.000000
white   0.0         0.486486    0.513514

我想我不明白。我把这些百分比加起来是为了说明我所寻找的输出类型。我已经编辑了上面数据帧的期望输出。当然,真正的数据有更多的行和更多的问题。谢谢。您说希望使用“权重”列,但所需的输出没有反映这一点。您想在计算中计算重量吗?是的,对不起,我已编辑以考虑重量。我在计算中一定要用到权重。谢谢,非常感谢,乔。真的很有帮助。非常感谢。