How to Get the Value Before a Specified Serial Value

advertisements
xSeries = pd.Series([1,200,5,13,3,301],index=['b', 's', 'h', 'd', 'e','a'])

When i was given the value 13,i want to get the value 200 before the value 13.How do i write the code?


If I understand you correctly, you're looking for the value 2 rows before a known value. In this case, the known value is 13.

v = 13
xSeries = pd.Series([1,200,5,13,3,301],index=['b', 's', 'h', 'd', 'e','a'])
print(xSeries)
b      1
s    200
h      5
d     13
e      3
a    301
dtype: int64    

idx = xSeries[xSeries == v].index
print(idx)
Index(['d'], dtype='object')

a = xSeries.index.get_loc(idx[0])
print(a)
3
print(xSeries.iloc[[a - 2]])
s    200
dtype: int64