2

Taking the below example, if I want to select all the values in column 'WHY' which correspond to certain 'EX' values, I can use the pandas .isin() function.

However, I want to select rows based on a specific series of EX values. In the below example, I would like only rows 4-7 to be selected. How can I achieve this?

import numpy as np
import pandas as pd

x_values = [1,3,5,7,9,11,13,12,11,10,9,8,7,8,9,10,12,14,16,18]
y_values = np.arange(103,123)
df = pd.DataFrame(list(zip(x_values, y_values)), columns = ['EX', 'WHY'])

    EX  WHY
0    1  103
1    3  104
2    5  105
3    7  106
4    9  107
5   11  108
6   13  109
7   12  110
8   11  111
9   10  112
10   9  113
11   8  114
12   7  115
13   8  116
14   9  117
15  10  118
16  12  119
17  14  120
18  16  121
19  18  122 

For this dataframe,

df[df['EX'].isin([9,11,13,12])]['WHY']

gives:

4     107
5     108
6     109
7     110
8     111
10    113
14    117
16    119
Michael G
  • 41
  • 2
  • Which certain 'EX' values do you want to specify? – Gavin Wong Jul 10 '20 at 11:27
  • Did you try `df.loc[4:7]` – Code Different Jul 10 '20 at 12:12
  • does the order of the sequence matter? what if it was `[9,11,12,13]` – Umar.H Jul 10 '20 at 12:20
  • What I'm trying to do is find the sections in a large dataframe where certain sequences appear, and then perform a function on those sections. So I can't use .loc, because I don't know the indexes where the sequences appear. Yes, the sequence matters, because the sequence is what I'm looking for. If you imagine the dataframe column as a list, I'm looking for the section of that list that matches, in this example, [9,11,13,12]. – Michael G Jul 10 '20 at 12:41

0 Answers0