Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having issue summarising python dataframe to one line per record

I've got a dataframe in the form:

df = pd.DataFrame({'id':['a', 'a', 'a', 'b','b'],'var':[1,2,3,5,9]})

and I'm trying to reshape it so that there is one line per 'id' and the values 'var' are displayed across in one line, so 'a' would have 1,2,3 ... 'b' would have '5,9'

I've tried with:

test = pd.crosstab(df.id, df.var)  # but it does not work?

If someone could help me it would be much appreciated

EDIT, I enclose the desired results as a picture hereenter image description here

like image 952
tezzaaa Avatar asked Apr 10 '26 21:04

tezzaaa


1 Answers

You must supply the correct arguments, like:

pd.crosstab(index=df['id'], columns=df['var'])

var  1  2  3  5  9
id                
a    1  1  1  0  0
b    0  0  0  1  1
like image 124
Nickil Maveli Avatar answered Apr 12 '26 11:04

Nickil Maveli