简体   繁体   中英

Pivoting count of column value using python pandas

I have student data with id's and some values and I need to pivot the table for count of ID.

Here's an example of data:

    id     name  maths science  
0   B001   john   50     60
1   B021   Kenny  89     77
2   B041   Jessi  100    89
3   B121   Annie  91     73
4   B456   Mark   45     33

pivot table:

count of ID
5

Lots of different ways to approach this, I would use either shape or nunique() as Sandeep suggested.

data = {'id' : ['0','1','2','3','4'], 'name' : ['john', 'kenny', 'jessi', 'Annie', 'Mark'], 'math' : [50,89,100,91,45], 'science' : [60,77,89,73,33]}

df = pd.DataFrame(data)

print(df ) id name math science 0 0 john 50 60 1 1 kenny 89 77 2 2 jessi 100 89 3 3 Annie 91 73 4 4 Mark 45 33

then pass either of the following:

df.shape() which gives you the length of a data frame.

or

in:df['id'].nunique()
out:5

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM