Given data as follows:
x | y |
---|---|
a | d1 |
a | d2 |
b | d1 |
b | d3 |
would like to create:
thing | d1 | d2 | d3 |
---|---|---|---|
a | True | True | False |
b | True | False | True |
example data:
data_from_csv = 'x,y\na,d1\na,d2\nb,d1\nb,d3\n'
data_to_csv = 'thing,d1,d2,d3\na,True,True,False\nb,True,False,True\n'
----------
I thought there was some sort of unstacking which could be done here but I can't seem to get it working.
CodePudding user response:
Use crosstab
with test not equal 0
:
df = pd.crosstab(df['x'], df['y']).ne(0)
print (df)
y d1 d2 d3
x
a True True False
b True False True
df=pd.crosstab(df['x'],df['y']).ne(0).rename_axis(index='thing',columns=None).reset_index()
print (df)
thing d1 d2 d3
0 a True True False
1 b True False True