TypeError:|:'str'和'bool'不支持的操作数类型 [英] TypeError: unsupported operand type(s) for |: 'str' and 'bool'

查看:65
本文介绍了TypeError:|:'str'和'bool'不支持的操作数类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有具有不同列的数据框 users .我的目标是在密码与每个用户的名字或姓氏相同时添加[ uses_name ]列,该列应为 True .

I have dataframe users with different columns. My goal is to add the column [uses_name] which should be True when a password is the same as each users first or last name.

例如,十二行中的[ user_name ]包含 milford.hubbard .然后,在[ uses_name ]中将为 True ,因为[ password ]和[ last_name ]相同.

For example, [user_name] in twelve row contain milford.hubbard. Then in [uses_name] will be True, because the [password] and [last_name] are the same.

为此,我创建了两个带有正则表达式的列[ first_name ]和[ last_name ].创建[ uses_name ]时,我遇到了 | 运算符的麻烦.我在pandas文档中读到了更多有关布尔索引的信息,但找不到答案.

To do this, I create two columns [first_name] and [last_name] with regular expressions. When create [uses_name] I have trouble with | operator. I am read more in pandas doc about Boolean indexing but not find an answer.

我的代码:

import pandas as pd

users = pd.read_csv('datasets/users.csv')

# Extracting first and last names into their own columns

users['first_name'] = users['user_name'].str.extract(r'(^\w+)', expand=False)

users['last_name'] = users['user_name'].str.extract(r'(\w+$)', expand=False)

# Flagging the users with passwords that matches their names

users['uses_name'] = users['password'].isin(users['first_name'] | users['last_name'])

# Counting and printing the number of users using names as passwords

print(users['uses_name'].count())

# Taking a look at the 12 first rows

print(users.head(12))

当我尝试对此进行编译时,出现错误:

When I try to compile this, I give an error:

TypeError: unsupported operand type(s) for |: 'str' and 'bool'

users 数据框中具有创建的 first_name last_name 列的前12行:

First 12 rows in users dataframe with created first_name and last_name columns:

id          user_name            password   first_name  last_name
0    1    vance.jennings          joobheco      vance    jennings
1    2    consuelo.eaton        0869347314   consuelo       eaton
2    3   mitchel.perkins        fabypotter    mitchel     perkins
3    4    odessa.vaughan         aharney88     odessa     vaughan
2    3   mitchel.perkins        fabypotter    mitchel     perkins
3    4    odessa.vaughan         aharney88     odessa     vaughan
4    5    araceli.wilder        acecdn3000    araceli      wilder
5    6  shawn.harrington           5278049      shawn  harrington
6    7        evelyn.gay            master     evelyn         gay
7    8       noreen.hale            murphy     noreen        hale
8    9       gladys.ward           lwsves2     gladys        ward
9   10   brant.zimmerman  1190KAREN5572497      brant   zimmerman
10  11     leanna.abbott          aivlys24     leanna      abbott
11  12   milford.hubbard           hubbard    milford     hubbard

推荐答案

这有效:

users['uses_name']= (users['password']==users['first_name'] )| (users['password']==users['last_name'])

这篇关于TypeError:|:'str'和'bool'不支持的操作数类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆