data

Val Krem valkrem at yahoo.com
Thu Dec 29 10:46:13 EST 2016


Hi all,
I have a sample of data set and would  like to summarize in the following way.


ID,class,y
1,12,10
1,12,10
1,12,20
1,13,20
1,13,10
1,13,10
1,14,20
2,21,20
2,21,20
2,21,10
2,23,10
2,23,20
2,34,20
2,34,10
2,35,10

I want  get  the total count by ID, and the  the number of classes
by ID. The y variable is either 10 or 20 and count each by iD

The result should look like as follows.

ID,class,count,10's,20's
1,3,7,4,3
2,4,8,4,4

I can do this in  two or more steps. Is there an efficient way of doing it?


I used 

pd.crosstab(a['ID'],a['y'],margins=True)
and got

ID,10's,20's all
1,4,3,7
2,4,4,8

but I want get the class count as well like as follows

ID,class,10's,20's,all
1,3,4,3,7
2,4,4,4,8

how do I do it in python?
thank you in advance



More information about the Python-list mailing list