2017-04-24 87 views
0

嗨我試圖從我的數據框中選擇一個特定的行,通過使用.loc從基因名稱中取出行及其相應數據,我無法通過數字進行拼接,因爲每個數據框都有不同的行。我得到的錯誤是一個關鍵的錯誤,雖然我不知道爲什麼。我已經使用type(df3)來確認我的數據仍然是一個數據框。DataFrame選擇行

for name in filenames: 
      df1 = pd.read_table(name, index_col=None, header = None, names =["Composite_Element_REF", "Beta_value", "Gene_Symbol", "Chromosome", "Genomic_Coordinate"], sep = "\t", skiprows= 2) # sep will seperate anything with space so it goes to the appropriate column, names is to lable column 
      df1 = df1.dropna() 
      df2 = df1[["Composite_Element_REF", "Beta_value","Gene_Symbol"]] 
      df3 = df2.rename(columns= {"Composite_Element_REF": "Composite_Element_REF "+ str(index), "Beta_value":"Beta_value "+str(index),"Gene_Symbol":"Gene_Symbol " +str(index)}, inplace=False) 
      #NR5A2 = df3.iloc[[2]] 
      NR5A2 = df3.loc[("Gene_Symbol " +str(index), "NR5A2"), : ] 

      index = index +1 

KeyError         Traceback (most recent call last) 
<ipython-input-78-5075955b850d> in <module>() 
    12  df3 = df2.rename(columns= {"Composite_Element_REF": "Composite_Element_REF "+ str(index), "Beta_value":"Beta_value "+str(index),"Gene_Symbol":"Gene_Symbol " +str(index)}, inplace=False) 
    13  #NR5A2 = df3.iloc[[2]] 
---> 14  NR5A2[str(index)] = df3.loc[("Gene_Symbol " +str(index), "NR5A2"), : ] 
    15 
    16  index = index +1 

回答

0

要使用.loc你必須設置一些列作爲索引。

但我認爲你會被罰款與

df3[df3["Gene_Symbol " +str(index)] == 'NR5A2']