Reputation: 713
datas = [['RAC1','CD0287',1.52], ['RAC1','CD0695',2.08], ['RAC1','ADN103-1',2.01], ['RAC3','CD0258',1.91], ['RAC3','ADN103-3',1.66], ['RAC8','CD0558',1.32], ['RAC8','ADN103-8',2.89]]
labels = ['Plate', 'Sample', 'LogRatio']
df = pd.DataFrame(data = datas, columns=labels, index=[8, 3, 5, 4, 12, 44, 2])
Plate Sample LogRatio
8 RAC1 CD0287 1.52
3 RAC1 CD0695 2.08
5 RAC1 ADN103-1 2.01
4 RAC3 CD0258 1.91
12 RAC3 ADN103-3 1.66
44 RAC8 CD0558 1.32
2 RAC8 ADN103-8 2.89
I would like to find the logratio value for the sample located n rows after "CD0695" sample using the index.
n = 2
indexCD0695 = df[df['Sample']=="CD0695"].index.tolist()
print(indexCD0695)
> [3]
logratio_value = df.iloc[indexCD0695[0]+n]['LogRatio']
> 1.32 #NOT THE RESULT I WOULD LIKE
I don't know how to have a single index and not a list so I just take the 1st element of the list indexCD0695[0]
, it's not my biggest issue.
My real problem is that I obtain the value at the index position 3+2 where as I would like to have the index starting with the location of CD0695 : (I can have it with just df.loc
) and have the 2nd row after this starting index :
4 RAC3 CD0258 1.91
So the logratio value is 1.91
I think I have to mix df.loc[indexCD0695]
and df.iloc[n]
but I don't know how.
Upvotes: 3
Views: 6691
Reputation: 215117
Another option is to shift your LogRatio
column by n
before extracting the value:
n = 2
df.LogRatio.shift(-n)[df.Sample == "CD0695"]
#3 1.91
#Name: LogRatio, dtype: float64
Upvotes: 1
Reputation: 394329
Use get_loc
to get the ordinal position of a specific row passing the index label, then you can use iloc
to get the nth row after this row:
In [261]:
indexCD0695 = df.index.get_loc(df[df['Sample']=="CD0695"].index[0])
indexCD0695
Out[261]:
1
In [262]:
n=2
logratio_value = df.iloc[indexCD0695+n]['LogRatio']
logratio_value
Out[262]:
1.9099999999999999
Upvotes: 4