2015-12-12 112 views
1

混合我有這樣的數據幀樞軸大熊貓據幀與數字和文本字段

Athlete Race Distance Rank Time 
M.Smith A 400m.  1  48.57 
A.Moyet A 400m.  2  49.00 
C.Marconi B 800m  5  104.12 
M.Smith B 800m.  3  102.66 

,並希望把它變成

Athlete Race#1 Distance#1 Rank#1 Time#1 Race#2 Distance#2 Rank#2 Time#2 
M.Smith A  400m  1  48.57 B  800m  3  102.66 
A.Moyet A  400m  2  49.00 NaN  NaN   NaN NaN 
C.Marconi B  800m  5  104.12 NaN  NaN   NaN NaN 

謝謝您的回答!

+0

我試過df.pivot_table(),但它僅與數字領域的文章我相信。在這裏,我有一些與種族相關的文字和數字混合。我能夠使用MySQL自動增量功能將比賽計數作爲一個單獨的列轉移到熊貓身上,但它不能解決整個問題。無論如何也尋找只有熊貓的解決方案。 – AtakanTE

+0

請將您的代碼添加到您的答案 – blckbird

回答

1

訣竅是分配一個競賽號碼(例如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 
+0

美麗,謝謝! – AtakanTE

+0

解決這個非平凡的問題... + 1 –