I have a Dataframe that looks like this:
| Col 1 | Col 2 |
0| A | 2 |
1| A | 3 |
2| B | 1 |
3| B | 2 |
and I need to transform it into a Dataframe that shows for each combination, of the values in Col 1 and Col 2 if that combination is contained in the original DataFrame:
| 1 | 2 | 3 |
A |False|True |True |
B |True |True |False|
Is there a native way in pandas to get this transformation? I was creating the transformed Dataframe manually, but this is way to slow.
Thank you in advance!
Here's a pivot solution:
You could use:
Use
get_dummies
withmax
:Or if possible not missing values in column
Col2
then useDataFrame.pivot
withDataFrame.notna
, for remove index and columns name useDataFrame.rename_axis
:Alternative is possible duplicates and
pivot
failed:Or solution from comments: