pandas wide format of occurrences

14 Views Asked by At

I am trying to understand if there is a smarter or clearer way to solve this transformation. I have a dataframe (long format) with the columns users and the column permissions. I would like to have a binary matrix with these two columns. I know it should be easy but all the examples i find about the pivot function show multiple columns data.

Let's say

data = {'user': ['A', 'A', 'B', 'B'],'ermission': [p1, p2, p1, p4]}
df = pd.DataFrame(data).set_index('user')

 Permission
user    
A     p1
A     p2
B     p1
B     p3
B     p4

And I would like:

 Permission
user  p1 p2 p3 p4
A     1  1  0  0
B     1  0  1  1

So this is the solution i found:

df_tran = df.pivot(index='user', columns='permission', values=df.columns[1]).fillna(0)
df_tran [df_tran != 0] = 1

Is there a correct way to do this?

0

There are 0 best solutions below