helloworld
helloworld

Reputation: 169

Pandas Dataframe: check specific rows that is satisfied condition

Below is my code.

def check_file():
    if os.path.isfile('client.csv'): #파일 위치 
        data = pd.read_csv('client.csv')
        print(data)
        return data

    else :
        data = pd.DataFrame({'Name':'Hello' , 'ID':'administer', 'PW': '1234'},index=[0])
        data.to_csv('client.csv')
        print(data)
        return data

    
data = check_file()
sample = ((data['ID'] == 'administer') & data['PW'] == '1234'))
print(data)
print(sample)

I want to make login code. So I think sample returns True.

Because in data index 0 satisfy sample's condition.

But it returns

    Name          ID    PW
0  Hello  administer  1234
0    False

Why sample is False?

Upvotes: 1

Views: 761

Answers (2)

Hunted
Hunted

Reputation: 88

You've simply forgotten a parenthesis.

Try replacing :

sample = ((data['ID'] == 'administer') & data['PW'] == '1234')
print(sample)
0    False
dtype: bool

with :

sample = ((data['ID'] == 'administer') & (data['PW'] == '1234'))
print(sample)
0    True
dtype: bool

Upvotes: 0

wasif
wasif

Reputation: 15478

Your condition is malformed:

(data['ID'] == 'administer') & (data['PW'] == '1234')

Did you mean to filter?

data[(data['ID'] == 'administer') & (data['PW'] == '1234')]

Upvotes: 3

Related Questions