2016-04-26 98 views
2

一個數據幀我有一個名爲staticData數據框,看起來像這樣:迭代通過指數

      narrow_sector  broad_sector country exchange \ 
unique_id                  
BBG.MTAA.STM.S   Semiconductors   Technology  CH  MTAA 
BBG.MTAA.CNHI.S Machinery-Diversified   Industrial  GB  MTAA 
BBG.MTAA.FCA.S  Auto Manufacturers Consumer Cyclical  GB  MTAA 
BBG.MTAA.A2A.S    Electric   Utilities  IT  MTAA 
BBG.MTAA.ACE.S    Electric   Utilities  IT  MTAA 

我trting按行挑選出兩位的信息索引(UNIQUE_ID)通過數據幀行迭代和找的零錢。我在索引上迭代出現問題。請參閱我的代碼:

for i, row in staticData.iterrows(): 

     unique_id = staticData.ix[i] 

     exchange = row['exchange'] 

我已經試過UNIQUE_ID =行[「UNIQUE_ID」],但不能讓它開始工作......

我試圖返回地說ROW1

unique_id = BBG.MTAA.STM.S 
exchange = MTAA 

任何幫助非常讚賞

感謝

回答

1

你想以下幾點:

for i, row in staticData.iterrows(): 
    unique_id = i 
    exchange = row['exchange'] 

我將是索引標籤值

實施例:

In [57]: 
df = pd.DataFrame(np.random.randn(5,3), index=list('abcde'), columns=list('fgh')) 
df 

Out[57]: 
      f   g   h 
a -0.900835 -0.913989 -0.624536 
b -0.854091 0.286364 -0.869539 
c 1.090133 -0.771667 1.258372 
d -0.721753 -0.329211 0.479295 
e 0.520786 0.273722 0.824172 

In [62]: 
for i, row in df.iterrows(): 
    print('index: ', i, 'col g:', row['g']) 

index: a col g: -0.913988608754 
index: b col g: 0.286363847188 
index: c col g: -0.771666520074 
index: d col g: -0.329211394286 
index: e col g: 0.273721527592 
+0

Doh,thanks EdChum – Stacey

2

可能更pandasian方式?

staticData.apply((lambda x: (x.name, x['exchange'])), axis=1)