Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.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 - Fatal编程技术网

Python 了解大熊猫的群比

Python 了解大熊猫的群比,python,pandas,Python,Pandas,我希望在对数据帧进行分组后,得到数据帧中一些值的总和 一些样本数据: Race officeID CandidateId total_votes precinct Mayor 10 705 20 Bell Mayor 10 805 30 Bell Treasurer 12 505

我希望在对数据帧进行分组后,得到数据帧中一些值的总和

一些样本数据:

Race          officeID   CandidateId  total_votes   precinct
Mayor         10         705            20           Bell
Mayor         10         805            30           Bell
Treasurer     12         505            10           Bell
Treasurer     12         506            40           Bell
Treasurer     12         507            30           Bell
Mayor         10         705            50           Park
Mayor         10         805            10           Park
Treasurer     12         505            5            Park
Treasurer     12         506            13           Park
Treasurer     12         507            16           Park
要获得每位候选人的总票数,我可以:

cand_votes = df.groupby('CandidateId').sum().total_votes
print cand_votes

CandidateId
505    15
506    53
507    46
705    70
805    40
要获得每个办公室的总票数:

total_votes = df.groupby('officeID').sum().total_votes
print total_votes

officeID
10    110
12    114
但是如果我想得到每个候选人的选票百分比呢?我必须对每个数据对象应用某种函数吗?理想情况下,我希望最终的数据对象如下所示:

officeID    CandidateID    total_votes    vote_pct
10          705            70             .6363
10          805            40             .37

首先,创建一个包含候选人和办公室投票的框架

gb = df.groupby(['officeID','CandidateId'], as_index=False)['total_votes'].sum()
然后,您可以按office进行聚合,并使用转换(类似于索引数据返回)来计算office的百分比

gb['vote_pct'] = gb['total_votes'] / gb.groupby('officeID')['total_votes'].transform('sum')


In [146]: gb
Out[146]: 
   officeID  CandidateId  total_votes  vote_pct
0        10          705           70  0.636364
1        10          805           40  0.363636
2        12          505           15  0.131579
3        12          506           53  0.464912
4        12          507           46  0.403509