IIU C использование GroupBy.cumcount
для вспомогательной колонки с pivot
:
df1 = (df.assign(a=df.groupby('Person_ID').cumcount().add(1))
.pivot('Person_ID','a','Person_value')
.add_prefix('Person_value'))
print (df1)
a Person_value1 Person_value2
Person_ID
A usr:value1 val:value2
B usr:value1 val:value2
C usr:value1 val:value2
D usr:value1 val:value2
или совокупность join
:
df1 = (df.groupby('Person_ID')
.agg(' // '.join)
.reset_index())
print (df1)
Person_ID Person_value
0 A usr:value1 // val:value2
1 B usr:value1 // val:value2
2 C usr:value1 // val:value2
3 D usr:value1 // val:value2
Если необходимо, разделите строки на Person_value
до :
до pivot
:
df1 = (df.assign(a=df['Person_value'].str.split(':').str[0])
.pivot('Person_ID','a','Person_value'))
print (df1)
a usr val
Person_name
A usr:value1 val:value2
B usr:value1 val:value2
C usr:value1 val:value2
D usr:value1 val:value2