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 here
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With