I have a pandas dataframe that has values like this:
**id** **type** **attr** **value**
1 type1 key1 val1
1 type1 key2 val2
2 type1 key1 val3
2 type1 key2 val4
And I want the output as
**id** **type** **key1** **key2**
1 type1 val1 val2
2. type1 val3 val4
I am trying to filter rows with same ids here like pick rows with same id and then create new columns as shown above.
I tried the following but getting DataError: No numeric types to aggregate
pd.pivot_table(df, values='value', index=['id', 'type'], columns='attr').reset_index()
UPDATE:
Solved the data error by the following modification:
df = pd.pivot_table(df, values='value', index=['id', 'type'], columns='attr', aggfunc='first')
But I am not able to understand the output that I am getting below. Why attr
, 'key1' and key2
columns hover above than id
and type
?
attr key1 key2
id type
1 type1 val1 val2
2 type1 val3 val4