I am trying to get my table in the following form. For some reason, i could not get my pivot code working.
df = pd.DataFrame([('a','f1'), ('a','f2'),('a','f3') ,('b','f4'),('c','f2'), ('c','f4')], columns = ['user', 'val'])
df
---
user val
a f1
a f2
a f3
b f4
c f2
c f4
>> output
user f1 f2 f3 f4
a 1 1 1 0
b 0 0 0 1
c 1 0 1 0
Option 1get_dummies
with groupby
+ sum
df.set_index('user').val.str.get_dummies().sum(level=0)
f1 f2 f3 f4
user
a 1 1 1 0
b 0 0 0 1
c 0 1 0 1
Option 2groupby
+ value_counts
+ unstack
df.groupby('user').val.value_counts().unstack(fill_value=0)
val f1 f2 f3 f4
user
a 1 1 1 0
b 0 0 0 1
c 0 1 0 1
Option 3pivot_table
with size
as the aggfunc
.
df.pivot_table(index='user', columns='val', aggfunc='size', fill_value=0)
val f1 f2 f3 f4
user
a 1 1 1 0
b 0 0 0 1
c 0 1 0 1
Seems like pd.crosstab(df['user'], df['val'])
work too.
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