0

I have a pandas dataframe like following:

user_id, section, count
1, section_1, 1
1, section_2, 5
1, section_3, 1
2, section_3, 1
3, section_1, 4
3, section_3, 3

I want to make separate column for each section and want to get the count as the row value like this

user_id, section_1, section_2, section_3
1, 1, 5, 1
2, 0, 0, 1
3, 4, 0, 3

How can I do that in pandas?

Rafiul Sabbir
  • 626
  • 6
  • 21

1 Answers1

0

Use pivot_table()

df_ = df.pivot_table(index='user_id', columns='section', values='count').fillna(0)
print(df_)

section  section_1  section_2  section_3
user_id                                 
1              1.0        5.0        1.0
2              0.0        0.0        1.0
3              4.0        0.0        3.0
print(df_.rename_axis(None, axis=1).reset_index())

   user_id  section_1  section_2  section_3
0        1        1.0        5.0        1.0
1        2        0.0        0.0        1.0
2        3        4.0        0.0        3.0
Ynjxsjmh
  • 28,441
  • 6
  • 34
  • 52
  • Not understand why answering dupe, 1000times answered? – jezrael May 04 '21 at 10:09
  • @jezrael OP only puts part of his dataframe. If there is duplicate `user_id` and `section`, `pivot` will throw error. – Ynjxsjmh May 04 '21 at 10:16
  • @jezrael `pivot_table` will set column designated by `index` as column. OP's result want index as 0,1... BTW, this question just matches pivot, who know it is a dupe. – Ynjxsjmh May 04 '21 at 10:21
  • @jezrael I don't know if it is a dupe, it just matches `pivot` usage, so I answered it. – Ynjxsjmh May 04 '21 at 10:23
  • hmmm, idea - check dupe and maybe you can see it is dupe. – jezrael May 04 '21 at 10:25
  • @jezrael It's similar but not all the same. Like index part I mention before. You should know there is a question that is same/similar with this question, then you call it a dupe. This just matches the `pivot` usage, I just share the usage case with OP. – Ynjxsjmh May 04 '21 at 10:32