Minwoo Kang
Minwoo Kang

Reputation: 70

Pandas dataframe conditional statement didn't give me what i expected

I have a dataframe like this

import numpy as np
import pandas as pd

lbl = [0, 1, 2, 3]
lbl2 = [0, 1, 2, 3, 4, 5]

label = lbl + lbl2

df = pd.DataFrame({"label":label})

#matching lbl and lbl2
pairs =[]
for i in range(3):
    pair = (i,i+1)
    pairs.append(pair)

when I hit this on terminal

(df.loc[:,'label'][df.index > (num_old_lbl - 1)]) & (df['label'] == pairs[0][1])

I got what I expected like this enter image description here

(df.loc[:,'label'][df.index > (num_old_lbl - 1)]) & (df['label'] == pairs[1][1])


but when I changed the line like this, every values show False. I expected row 6 will be True.

Upvotes: 0

Views: 45

Answers (1)

tlentali
tlentali

Reputation: 3455

We get the expected result using this writing (setting up num_old_lbl to 3) :

>>> (df.index > (num_old_lbl - 1)) & (df['label'] == pairs[1][1])
0    False
1    False
2    False
3    False
4    False
5    False
6     True
7    False
8    False
9    False
Name: label, dtype: bool

It also works as expected with the given example :

>>> (df.index > (num_old_lbl - 1)) & (df['label'] == pairs[0][1])
0    False
1    False
2    False
3    False
4    False
5     True
6    False
7    False
8    False
9    False
Name: label, dtype: bool

Upvotes: 1

Related Questions