繁体   English   中英

Pandas Dataframe:检查满足条件的特定行

[英]Pandas Dataframe: check specific rows that is satisfied condition

下面是我的代码。

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)

我想制作登录代码。 所以我认为示例返回 True。

因为在数据索引 0 中满足样本的条件。

但它返回

    Name          ID    PW
0  Hello  administer  1234
0    False

为什么样本是假的?

您的条件格式错误:

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

你是说过滤吗?

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

你只是忘记了一个括号。

尝试更换:

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

和 :

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM