I have a Dataframe that looks like this:
| Col 1 | Col 2 |
0| A | 2 |
1| A | 3 |
2| B | 1 |
3| B | 2 |
an
Here's a pivot solution:
(df.pivot('Col 1', 'Col 2', 'Col 1').fillna(0) != 0).rename_axis(index=None, columns=None)
1 2 3
A False True True
B True True False
Use get_dummies with max
:
df = pd.get_dummies(df.set_index('Col 1')['Col 2'], dtype=bool).rename_axis(None).max(level=0)
print (df)
1 2 3
A False True True
B True True False
Or if possible not missing values in column Col2
then use DataFrame.pivot with DataFrame.notna, for remove index and columns name use DataFrame.rename_axis:
df = df.pivot('Col 1', 'Col 2', 'Col 1').notna().rename_axis(index=None, columns=None)
print (df)
1 2 3
A False True True
B True True False
Alternative is possible duplicates and pivot
failed:
df = (df.pivot_table(index='Col 1', columns='Col 2', values='Col 1', aggfunc='size')
.notna()
.rename_axis(index=None, columns=None))
print (df)
1 2 3
A False True True
B True True False
Or solution from comments:
df = (pd.crosstab(df['Col 1'], df['Col 2'])
.gt(0)
.rename_axis(index=None, columns=None))
You could use:
df.groupby(['Col 1','Col 2']).size().unstack(fill_value=0).astype(bool)
Col2 1 2 3
Col1
A False True True
B True True False