piRSquared - 3 months ago 13x

Python Question

Consider the series

`s`

`s = pd.Series(np.arange(18, 0, -3), list('ABCDEF'))`

s

A 18

B 15

C 12

D 9

E 6

F 3

dtype: int32

I want to be able to access all elements after index

`'D'`

`E 6`

F 3

dtype: int32

If I

`s.loc['D':]`

I get

`D 9`

E 6

F 3

dtype: int32

Not what I wanted. I could

`s.loc['D':].iloc[1:]`

But this doesn't give me a convenient way to assign back to the dataframe.

Answer

**UPDATE:** thanks to @Alex he has reminded that indices are not always monotonically increasing:

```
In [85]: s
Out[85]:
F 18
B 15
D 12
A 9
C 6
E 3
dtype: int32
In [86]: s.iloc[s.index.get_loc('D') + 1:]
Out[86]:
A 9
C 6
E 3
dtype: int32
```

try this:

```
In [57]: s[s.index > 'D']
Out[57]:
E 6
F 3
dtype: int32
```

or using indexers:

```
In [59]: s.ix[s.index > 'D'] = 100
In [60]: s
Out[60]:
A 18
B 15
C 12
D 9
E 100
F 100
dtype: int32
```

Source (Stackoverflow)

Comments