Reputation: 551
I try to transpose a dataframe with a specific format :
Here is my current dataframe called df :
and the result of transpose shoud be :
Thanks in advance.
Upvotes: 1
Views: 5219
Reputation: 323226
Assuming you have following dataframe , using unstack
df=pd.DataFrame({'pid':[1,1,1],'TreeFeature':['a','b','c'],'Import':[1,2,3],'acc':[1,1,1]})
df.set_index(['pid','acc','TreeFeature']).Import.unstack().reset_index()
Out[298]:
TreeFeature pid acc a b c
0 1 1 1 2 3
Upvotes: 2
Reputation: 164673
You can try using pd.pivot_table
:
res = df.pivot_table(index=['pid', 'Accuracy'], columns=['TreeFeatures'],
values='Importance 1', aggfunc='first', fill_value=0)
If you need to elevate index to columns, reset index via res.reset_index()
.
Upvotes: 2