2014-03-04 48 views
8

我有以下代碼,它取得熊貓數據框的一列中的值,並使它們成爲新數據框的列。數據幀第一列中的值將成爲新數據幀的索引。將列值更改爲大熊貓中的列標題

從某種意義上說,我想把一個鄰接表變成一個鄰接矩陣。這裏是到目前爲止的代碼:

import pandas as pa 
print "Original Data Frame" 
# Create a dataframe 
oldcols = {'col1':['a','a','b','b'], 'col2':['c','d','c','d'], 'col3':[1,2,3,4]} 
a = pa.DataFrame(oldcols) 
print a 

# The columns of the new data frame will be the values in col2 of the original 
newcols = list(set(oldcols['col2'])) 
rows = list(set(oldcols['col1'])) 

# Create the new data matrix 
data = np.zeros((len(rows), len(newcols))) 

# Iterate over each row and fill in the new matrix 
for row in zip(a['col1'], a['col2'], a['col3']): 
    rowindex = rows.index(row[0]) 
    colindex = newcols.index(row[1]) 
    data[rowindex][colindex] = row[2] 

newf = pa.DataFrame(data) 
newf.columns = newcols 
newf.index = rows 

print "New data frame" 
print newf 

這適用於這個特定實例:

Original Data Frame 
    col1 col2 col3 
0 a c  1 
1 a d  2 
2 b c  3 
3 b d  4 
New data frame 
    c d 
a 1 2 
b 3 4 

如果COL3的值不是數字就會失敗。我的問題是,是否有一個更優雅/強大的方式來做到這一點?

回答

11

這看起來像a job for pivot

import pandas as pd 
oldcols = {'col1':['a','a','b','b'], 'col2':['c','d','c','d'], 'col3':[1,2,3,4]} 
a = pd.DataFrame(oldcols) 

newf = a.pivot(index='col1', columns='col2') 
print(newf) 

產量

 col3 
col2  c d 
col1   
a  1 2 
b  3 4 

如果你不想多指標列,您可以使用刪除col3

newf.columns = newf.columns.droplevel(0) 

這將然後收益率

col2 c d 
col1  
a  1 2 
b  3 4 
+0

哇,*幾乎*讓我後悔編寫代碼做'手動': - P –

+2

不要擔心 - 它讓你更加欣賞熊貓! – unutbu