訣竅是分配一個競賽號碼(例如1或2),這取決於它是否應與比賽#1或種族#2相關聯的每一行:
df['race'] = df.groupby('Athlete').cumcount()+1
# Athlete Distance Race Rank Time race
# 0 M.Smith 400m A 1 48.57 1
# 1 A.Moyet 400m A 2 49.00 1
# 2 C.Marconi 800m B 5 104.12 1
# 3 M.Smith 800m B 3 102.66 2
然後期望的數據幀可以被表示爲set_index/unstack
操作的結果:
result = df.set_index(['Athlete', 'race']).unstack('race')
# Distance Race Rank Time
# race 1 2 1 2 1 2 1 2
# Athlete
# A.Moyet 400m NaN A NaN 2 NaN 49.00 NaN
# C.Marconi 800m NaN B NaN 5 NaN 104.12 NaN
# M.Smith 400m 800m A B 1 3 48.57 102.66
Athlete
的和race
列移動到索引。 unstack
操作將race
索引級別移動到列級別。
,伴隨着一點點感動起來以獲得所需格式的列:
import pandas as pd
df = pd.DataFrame({'Athlete': ['M.Smith', 'A.Moyet', 'C.Marconi', 'M.Smith'],
'Distance': ['400m', '400m', '800m', '800m'],
'Race': ['A', 'A', 'B', 'B'],
'Rank': [1, 2, 5, 3],
'Time': [48.57, 49.0, 104.12, 102.66]})
df['race'] = df.groupby('Athlete').cumcount()+1
result = df.set_index(['Athlete', 'race']).unstack('race')
result = result.sortlevel('race', axis='columns')
result.columns = ['{}#{}'.format(col, n) for col, n in result.columns]
print(result)
產生
Distance#1 Race#1 Rank#1 Time#1 Distance#2 Race#2 Rank#2 Time#2
Athlete
A.Moyet 400m A 2 49.00 NaN NaN NaN NaN
C.Marconi 800m B 5 104.12 NaN NaN NaN NaN
M.Smith 400m A 1 48.57 800m B 3 102.66
我試過df.pivot_table(),但它僅與數字領域的文章我相信。在這裏,我有一些與種族相關的文字和數字混合。我能夠使用MySQL自動增量功能將比賽計數作爲一個單獨的列轉移到熊貓身上,但它不能解決整個問題。無論如何也尋找只有熊貓的解決方案。 – AtakanTE
請將您的代碼添加到您的答案 – blckbird