Reputation: 441
I'm trying to reshape my dataframe, in which I want the Person
to the be index, however how do I make it so the index is unique? I don't know want duplicates in my Index.
df = pd.DataFrame({'Person':['Paul','Paul','Paul','John','John','Mia'],'Score':[24,23,54,64,89,56],'Type':['A','C','F','A','G','X'],'Number':[1,2,3,1,2,1]})
df.set_index('Person',inplace = True)
Desired output:
Upvotes: 0
Views: 382
Reputation: 10545
You could create a MultiIndex
, keeping the previous numerical index as the second level:
df.set_index(['Person', df.index], inplace=True)
df
Score Type Number
Person
Paul 0 24 A 1
1 23 C 2
2 54 F 3
John 3 64 A 1
4 89 G 2
Mia 5 56 X 1
I think this is the closest you can get to your desired output, because by definition there has to be an index value for each row of the dataframe.
Upvotes: 1
Reputation: 14949
If you just wanna remove the duplicate values from the index use:
df = df.set_index('Person')
df.index = np.where(df.index.duplicated(), '', df.index)
Upvotes: 1