使用pivot
或unstack
:
df = df.pivot(index = "ID", columns = "FIELD", values = "VALUE")
print (df)
FIELD E_REASON IN_SCOPE TEST
ID
12463634 010 Y 22.2
12463635 020 N 99.5
df = df.set_index(['ID', 'FIELD'])['VALUE'].unstack()
print (df)
FIELD E_REASON IN_SCOPE TEST
ID
12463634 010 Y 22.2
12463635 020 N 99.5
如果重複需要pivot_table
一些聚合函數 - sum
或','join
:
print (df)
ID FIELD VALUE
0 12463634 TEST 22.2
1 12463634 E_REASON 010
2 12463634 IN_SCOPE Y<-same ID and FIELED
3 12463634 IN_SCOPE Y1<-same ID and FIELED
4 12463635 TEST 99.5
5 12463635 E_REASON 020
6 12463635 IN_SCOPE N
df = df.pivot_table(index = "ID", columns = "FIELD", values = "VALUE", aggfunc='sum')
print (df)
FIELD E_REASON IN_SCOPE TEST
ID
12463634 010 YY1 22.2
12463635 020 N 99.5
或者:
df = df.pivot_table(index = "ID", columns = "FIELD", values = "VALUE", aggfunc=','.join)
print (df)
FIELD E_REASON IN_SCOPE TEST
ID
12463634 010 Y,Y1 22.2
12463635 020 N 99.5
我想可能你想要的是'pivot'而不是'pivot_table'。 – Ajean
只有他們做出非唯一索引。我只是試了一下,數據透視工作正常。 – Ajean