2016-03-20 19 views
4

Kategorik değişkenlere ve bir değer aralığına göre gruplandırmak istediğim bir dataframe var. Benzer değerlerdeki satırlar gibi düşünebilirsiniz (kümeler?). Örneğin: Ben kullanırsanızPandalarda bir dizi değer nasıl gruplanır?

df = pd.DataFrame({'symbol' : ['IP', 'IP', 'IP', 'IP', 'IP', 'IP', 'IP'], 
        'serie' : ['A', 'B', 'A', 'B', 'A', 'B', 'B'], 
        'strike' : [10, 10, 12, 13, 12, 13, 14], 
        'last' : [1, 2, 2.5, 3, 4.5, 5, 6], 
        'price' : [11, 11, 11, 11, 11, 11, 11], 
        'type' : ['call', 'put', 'put', 'put', 'call', 'put', 'call']}) 

grouped = df.groupby(['symbol', 'serie', 'strike']) 

Ben çözüldü benim sorunun bir parçası var, ama böyle 10 ve 11, 12 ve 13 ve böylece yakın olan grev değerlerini birleştirmek istediğiniz ileri. Tercihen% bir aralık içinde.

+0

çoğaltmak görünüyor: http://stackoverflow.com/questions/21441259/pandas-groupby- aralık değerleri –

+2

Beklenen bir çıktı gösterebilir misiniz lütfen? –

+0

İlk olarak grev değerlerini kümelemek/gruplamak için iyi tanımlanmış bir ölçüme ihtiyacınız vardır. – Goyo

cevap

1

OP'nin, kategorik değişkenlere göre gruplamak istediğini ve ardından aralıklarla girilen sayısal bir değişkeni takip ettiğini tahmin ediyorum. Bu durumda np.digitize()'u kullanabilirsiniz.

smallest = np.min(df['strike']) 
largest = np.max(df['strike']) 
num_edges = 3 
# np.digitize(input_array, bin_edges) 
ind = np.digitize(df['strike'], np.linspace(smallest, largest, num_edges)) 

bin ile

[10, 10, 12, 13, 12, 13, 14] 

binning tekabül hangi

array([1, 1, 2, 2, 2, 2, 3], dtype=int64) 

sonra ind olmalı, istediğiniz tüm sütunlara göre, son olarak grup

array([ 10., 12., 14.]) # == np.linspace(smallest, largest, num_edges) 

kenarları ama ile Bu

group key 
('IP', 'A', 1) 
group content 
    last price serie strike symbol type binned_strike 
0 1.0  11  A  10  IP call    1 
============= 
group key 
('IP', 'A', 2) 
group content 
    last price serie strike symbol type binned_strike 
2 2.5  11  A  12  IP put    2 
4 4.5  11  A  12  IP call    2 
============= 
group key 
('IP', 'B', 1) 
group content 
    last price serie strike symbol type binned_strike 
1 2.0  11  B  10  IP put    1 
============= 
group key 
('IP', 'B', 2) 
group content 
    last price serie strike symbol type binned_strike 
3 3.0  11  B  13  IP put    2 
5 5.0  11  B  13  IP put    2 
============= 
group key 
('IP', 'B', 3) 
group content 
    last price serie strike symbol type binned_strike 
6 6.0  11  B  14  IP call    3 
============= 
2

yazdırmalısınız

df['binned_strike'] = ind 
for grp in df.groupby(['symbol', 'serie', 'binned_strike']): 
    print "group key" 
    print grp[0] 
    print "group content" 
    print grp[1] 
    print "=============" 

bu ek bin sütun bu bilgilere göre o grup pd.cut ile kutuları grev verilerinin strike

oluştur bidonları üzerine groupy() yapın ve:

# Create DataFrame 
df = pd.DataFrame({ 
    'symbol' : ['IP', 'IP', 'IP', 'IP', 'IP', 'IP', 'IP'], 
    'serie' : ['A', 'B', 'A', 'B', 'A', 'B', 'B'], 
    'strike' : [10, 10, 12, 13, 12, 13, 14], 
    'last' : [1, 2, 2.5, 3, 4.5, 5, 6], 
    'price' : [11, 11, 11, 11, 11, 11, 11], 
    'type' : ['call', 'put', 'put', 'put', 'call', 'put', 'call'] 
}) 
# Create Bins (example three bins across data) 
df['strikebins'] = pd.cut(df['strike'], bins=3) 

print 'Binned DataFrame:' 
print df 
print 

# Group these DataFrame 
grouped = df.groupby(['symbol', 'serie', 'strikebins']) 

# Do something with groups for example 
gp_sum = grouped.sum() 

print 'Grouped Sum (for example):' 
print gp_sum 
print 
Yapabilirsin drop() strike istediğini, veya aralık ortalama strikebins değiştirirseniz
Binned DataFrame: 
    last price serie strike symbol type  strikebins 
0 1.0  11  A  10  IP call (9.996, 11.333] 
1 2.0  11  B  10  IP put (9.996, 11.333] 
2 2.5  11  A  12  IP put (11.333, 12.667] 
3 3.0  11  B  13  IP put  (12.667, 14] 
4 4.5  11  A  12  IP call (11.333, 12.667] 
5 5.0  11  B  13  IP put  (12.667, 14] 
6 6.0  11  B  14  IP call  (12.667, 14] 

Grouped Sum (for example): 
           last price strike 
symbol serie strikebins       
IP  A  (9.996, 11.333]  1  11  10 
      (11.333, 12.667]  7  22  24 
      (12.667, 14]  NaN NaN  NaN 
     B  (9.996, 11.333]  2  11  10 
      (11.333, 12.667] NaN NaN  NaN 
      (12.667, 14]  14  33  40 

...