Home > Mobile >  Python pivot-table with array value
Python pivot-table with array value

Time:05-27

For a project with table features, I try to create a new table with pivot_table. Small problem however, one of my columns contains an array. Here an ex

| House  | Job | 
| ----- | --- |
| Gryffindor  | ["Head of Auror Office", " Minister for Magic"]| 
| Gryffindor  | ["Auror"]|
| Slytherin | ["Auror","Student"] |

Ideally, I would like with a pivot table to create a table that looks like this

| House | Head of Auror Office | Minister for Magic | Auror | Student |
|:----- |:--------------------:|:------------------:|:-----:|:-------:|
| Gryffindor  | 1 | 1| 1| 0|
| Slytherin  | 0 | 0| 1| 1|

Of course I can have a value like 2,3 or 4 in the array so something that is not fixed. Anyone have a solution? Maybe the pivot_table is not the best solution :/

Sorry for the arrays, It's not working :(

CodePudding user response:

suppose your table is df with two columns:

(df.explode('Job')
   .groupby(['House', 'Job']).size().reset_index()
   .pivot(index = 'House', columns = 'Job').fillna(0))

the code first expand the list into rows, then do the count, and finally do the pivot table

  • Related