2016-12-05 50 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