2016-05-09 18 views
7

Şu anda yapılandırılmıştır bazı veri kümesi vardır: benziyorPandalar - Birden çok değişken nasıl gruplanır ve açılır? aşağıdaki gibi

data = {'participant': [100, 101, 102, 103, 104, 105, 106, 107, 108, 109], 
     'step_name': ['first', 'first', 'second', 'third', 'second', 'first', 'first', 'first', 'second', 'third'], 
     'title': ['acceptable', 'acceptable', 'not acceptable', 'acceptable', 'not acceptable', 'acceptable', 'not acceptable', 'acceptable', 'acceptable', 'acceptable'], 
     'colour': ['blue', 'blue', 'blue', 'green', 'green', 'blue', 'green', 'blue', 'blue', 'green'], 
     'class': ['A', 'B', 'B', 'A', 'B', 'A', 'A', 'A', 'A', 'B']} 
df = pd.DataFrame(data, columns=['participant', 'step_name', 'title', 'colour', 'class']) 

:

+----+---------------+-------------+----------------+----------+---------+ 
| | participant | step_name | title   | colour | class | 
|----+---------------+-------------+----------------+----------+---------| 
| 0 |   100 | first  | acceptable  | blue  | A  | 
| 1 |   101 | first  | acceptable  | blue  | B  | 
| 2 |   102 | second  | not acceptable | blue  | B  | 
| 3 |   103 | third  | acceptable  | green | A  | 
| 4 |   104 | second  | not acceptable | green | B  | 
| 5 |   105 | first  | acceptable  | blue  | A  | 
| 6 |   106 | first  | not acceptable | green | A  | 
| 7 |   107 | first  | acceptable  | blue  | A  | 
| 8 |   108 | second  | acceptable  | blue  | A  | 
| 9 |   109 | third  | acceptable  | green | B  | 
+----+---------------+-------------+----------------+----------+---------+ 

Şimdi her satır tekrar değişkenler, her sayar, böylece veri kümesi toplamak istediğinizi hangi şöyle Şu anda iki değişken (step_name ve title) boyunca yapmak başardınız:

count_df = df[['participant', 'step_name', 'title']].groupby(['step_name', 'title']).count() 
count_df = count_df.unstack() 
count_df.fillna(0, inplace=True) 
count_df.columns = count_df.columns.get_level_values(1) 
count_df 

+--------+--------------+------------------+ 
|  | acceptable | not acceptable | 
|--------+--------------+------------------| 
| first |   4 |    1 | 
| second |   1 |    2 | 
| third |   2 |    0 | 
+--------+--------------+------------------+ 

Yine de, diğer değişkenler için değerleri içeren fazladan bir sütun kümesine sahip olmak istiyorum (colour ve class) - temel olarak, gruplamak ve sonra bu değişkenleri kaldırmak istiyorum, ancak nasıl yapacağımı bilmiyorum 2'den fazla değişkenle.

+------+------+--------+--------------+------------------+ 
|class |colour| step | acceptable | not acceptable | 
|----------------------+--------------+------------------| 
| A | blue | first |   3 |    0 | 
| B | blue | first |   1 |    0 | 
| A |green | first |   0 |    1 | 
| B |green | first |   0 |    0 | 
| A | blue | second |   1 |    0 | 
| B | blue | second |   0 |    1 | 
| A |green | second |   0 |    0 | 
| B |green | second |   0 |    1 | 
| A |blue | third |   0 |    0 | 
| B |blue | third |   0 |    0 | 
| A |green | third |   1 |    0 | 
| B |green | third |   1 |    0 | 
+------+------+--------+--------------+------------------+ 

benim son örnek gibi görünür, böylece benim verilerini yeniden şekillendirmek nasıl: benim son tablo bu gibi görünmek için Nihayetinde İsterdim? İstenmeyen posta ve grup işlevlerini hala kullanıyor muyum?

cevap

5

Sana (pandas0.18.0 yeni) aggfunc=len, reset_index ve rename_axis ile pivot_table gerektiğini düşünüyorum:

df = df.pivot_table(index=['class','colour','step_name'], 
        columns='title', 
        aggfunc=len, 
        values='participant', 
        fill_value=0).reset_index().rename_axis(None, axis=1) 
print df 
     class colour step_name acceptable not acceptable 
0   A blue  first   3    0 
1   A blue second   1    0 
2   A green  first   0    1 
3   A green  third   1    0 
4   B blue  first   1    0 
5   B blue second   0    1 
6   B green second   0    1 
7   B green  third   1    0 
+0

Teşekkür! 'Rename_axis' bit bana bir hata verir gibi görünüyor, ama - 'TypeError: yeniden adlandırmak için bir dizin geçirmeli' – orange1

+1

Bu pandalar 0.18.0' içinde yeni bir işlevdir, bunu atlayabilirsiniz. Ve kullan 'df.columns.name = Hiçbiri' – jezrael

6

Bunun için pivot_table() kullanabilirsiniz:

In [130]: df['count'] = 1 

In [134]: (df.pivot_table(index=['class','colour','step_name'], columns='title', 
    .....:     values='count', aggfunc='sum', fill_value=0) 
    .....: .reset_index() 
    .....:) 
Out[134]: 
title class colour step_name acceptable not acceptable 
0   A blue  first   3    0 
1   A blue second   1    0 
2   A green  first   0    1 
3   A green  third   1    0 
4   B blue  first   1    0 
5   B blue second   0    1 
6   B green second   0    1 
7   B green  third   1    0 
İlgili konular