at the moment I try to transform some data within a dataframe and I am struggling somehow. I start with a minimal example of my initial situation:
import pandas as pd
data = {'UUID': ['UUID1','UUID2','UUID3','UUID3'],
'grouping': ['a','a','a','b'],
'code': ['1','2','3','4']
}
df = pd.DataFrame(data)
print (df)
> UUID grouping code
> 0 UUID1 a 1
> 1 UUID2 a 2
> 2 UUID3 a 3
> 3 UUID3 b 4
I have a UUID and want to transform the table in a way that I have only unique UUIDs in a column. The grouping column should be used to swap the code values to the new cols. Here is an output of a dataframe I would like to receive:
> UUID code a code b
> 0 UUID1 1 NaN
> 1 UUID2 2 NaN
> 2 UUID3 3 4
I tried to work with pd.crosstab()
and pd.getdummies()
but both did not do the job.
I am happy for any help I can get :-).
Best P