DataFrame选择行

时间:2017-04-24 23:37:02

标签: python pandas

您好我正在尝试从我的数据框中选择一个特定的行,使用.loc通过基因名称取出行及其相应的数据,我不能通过数字拼接,因为每个数据帧都有不同的行。我得到的错误是一个关键错误,虽然我不知道为什么。我使用了类型(df3)来确认我的数据仍然是数据帧。

 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

1 个答案:

答案 0 :(得分:0)

要使用.loc,您必须设置一些列作为索引。

但我认为你会没事的

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