Given this data:
pd.DataFrame({'id':['aaa','aaa','abb','abb','abb','acd','acd','acd'],
'loc':['US','UK','FR','US','IN','US','CN','CN']})
id loc
0 aaa US
1 aaa UK
2 abb FR
3 abb US
4 abb IN
5 acd US
6 acd CN
7 acd CN
How do I pivot it to this:
id loc1 loc2 loc3
aaa US UK None
abb FR US IN
acd US CN CN
I am looking for the most idiomatic method.
I think you can create new column
cols
withgroupby
,cumcount
and convert tostring
byastype
, last usepivot
:If you want remove index and columns names use rename_axis:
All together, thank you Colin:
I try
rank
, but I get error in version0.18.0
: