2016-12-05 149 views
-2

我想将聚合函数(sum())应用于我首先通过维度“Customer”进行聚合的变量(“PurchAmount”)。同时我想选择“数量”一栏。同时在Pandas DataFrame中分组/聚合和选择

在R这是可能的:

myData[, list(Quantity, AggPurch=sum(PurchAmount)), by=Customer] 

有没有在Python的熊猫数据帧类似的解决方案?

+0

@Rich Scriven我唯一的编辑是去除虚假的R ta克这个问题。如果不通过编辑,应该如何实现? – G5W

回答

0

可以使用 '.groupby' 拆分大熊猫成组:

http://pandas.pydata.org/pandas-docs/stable/groupby.html#splitting-an-object-into-groups

import pandas as pd 

raw_data = {'regiment': ['Nighthawks', 'Nighthawks', 'Nighthawks', 'Nighthawks', 'Dragoons', 'Dragoons', 'Dragoons', 'Dragoons', 'Scouts', 'Scouts', 'Scouts', 'Scouts'], 
    'company': ['1st', '1st', '2nd', '2nd', '1st', '1st', '2nd', '2nd','1st', '1st', '2nd', '2nd'], 
    'name': ['Miller', 'Jacobson', 'Ali', 'Milner', 'Cooze', 'Jacon', 'Ryaner', 'Sone', 'Sloan', 'Piger', 'Riani', 'Ali'], 
    'preTestScore': [4, 24, 31, 2, 3, 4, 24, 31, 2, 3, 2, 3], 
    'postTestScore': [25, 94, 57, 62, 70, 25, 94, 57, 62, 70, 62, 70]} 

df = pd.DataFrame(raw_data, columns = ['regiment', 'company', 'name', 'preTestScore', 'postTestScore']) 

df['preTestScore'].groupby([df['regiment'], df['company']]).mean() 

为DF的输出将是:

regiment company 
Dragoons 1st   3.5 
      2nd  27.5 
Nighthawks 1st  14.0 
      2nd  16.5 
Scouts  1st   2.5 
      2nd   2.5 
dtype: float64 

实施例从: http://chrisalbon.com/python/pandas_apply_operations_to_groups.html