Search code examples
pythonpandaspivot-tablecrosstab

Pandas crosstab dataframe and setting the new columns as True/False/Null based on if they existed or not and based on another column


As the title states I want to pivot/crosstab my dataframe

Let's say I have a df that looks like this:

df = pd.DataFrame({'ID' : [0, 0, 1, 1, 1], 
                   'REV' : [0, 0, 1, 1, 1],
                   'GROUP' : [1, 2, 1, 2, 3],
                   'APPR' : [True, True, NULL, NULL, True})


+----+-----+-------+------+
| ID | REV | GROUP | APPR |
+----+-----+-------+------+
|  0 |   0 |     1 | True |
|  0 |   0 |     2 | True |
|  1 |   1 |     1 | NULL |
|  1 |   1 |     2 | NULL |
|  1 |   1 |     3 | True |
+----+-----+-------+------+

I want to do some kind of pivot so my result of the table looks like

+----+-----+------+------+-------+
| ID | REV |  1   |  2   |   3   |
+----+-----+------+------+-------+
|  0 |   0 | True | True | False |
|  1 |   1 | NULL | NULL | True  |
+----+-----+------+------+-------+

Now the values from the GROUP column becomes there own column. The value of each of those columns is T/F/NULL based on APPR only for the T/NULL part. I want it be False when the group didn't exist for the ID REV combo.

similar question I've asked before, but I wasn't sure how to make this answer work with my new scenario: Pandas pivot dataframe and setting the new columns as True/False based on if they existed or not

Hope that makes, sense!


Solution

  • Have you tried to pivot?

    pd.pivot(df, index=['ID','REV'], columns=['GROUP'], values='APPR').fillna(False).reset_index()