JeeYem - 2 months ago 5

Python Question

Data has three columns. I need to find the row that has the number intersecting between second and third columns. For example, which row has number 15 between second and third column in the data below?

`a 1 5`

b 7 10

c 13 17

d 20 24

The ideal result should be

`c 13 17`

as it has '15' intersecting that row between second and third column. Is there a way to do it using Python (specifically Python 2.7)?

Answer

You mean this?

```
import pandas as pd
df = pd.DataFrame([('a', 1, 5), ('b', 7, 10), ('c', 13, 17), ('d', 20, 24)],
columns=['col1', 'col2', 'col3'])
df[(df.col2 < 15) & (df.col3 > 15)]
```

yields...

```
col1 col2 col3
2 c 13 17
```

Source (Stackoverflow)

Comments