dancemc15 dancemc15 - 1 month ago 7
Python Question

How do I only look at certain rows in pandas dataframe?

I want to only look at the information of rows that correspond with 'U' and 'L' and 'R' under the category 'LU'. What exactly do I have to write to grab that specific data?

PTYPE LU
1 E
2 U
3 R
4 L
5 R


test = data[data['LU'] == 'U' | 'L' | 'R' ]

I thought it might be the following statement above, but I got an error. 'U' 'L' and 'R' are also not integers.

Answer

method 1
query

data.query('LU in ("U", "L", "R")')

method 2
isin

data[data.LU.isin(list('ULR'))]

both result in

enter image description here