Python中文
首页
教程
问答
标签
搜索
登录
注册
TypeError:“str”和“bool”的操作数类型不受支持
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>我有不同列的dataframe<code>users</code>。我的目标是添加列[<code>uses_name</code>],当密码与每个用户的名字或姓氏相同时,它应该是<code>True</code>。在</p> <p>例如,十二行中的[<code>user_name</code>]包含<code>milford.hubbard</code>。那么在[<code>uses_name</code>]中是<code>True</code>,因为[<code>password</code>]和[<code>last_name</code>]是相同的。在</p> <p>为此,我使用正则表达式创建两个列[<code>first_name</code>]和[<code>last_name</code>]。当create[<code>uses_name</code>]时,<code>|</code>运算符有问题。我在pandas文档中读到了更多关于布尔索引的内容,但没有找到答案。在</p> <p>我的代码:</p> <pre><code>import pandas as pd users = pd.read_csv('<a href="https://www.cnpython.com/pypi/dataset" class="inner-link">dataset</a>s/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)) </code></pre> <p>当我试图编译这个时,我给出了一个错误:</p> ^{pr2}$ <p><code>users</code>数据帧中的前12行,其中创建了<code>first_name</code>和<code>last_name</code>列:</p> <pre><code>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 </code></pre>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>你可以合并,因为它们都是串联的</p> <pre><code>users['password'].isin(pd.concat([users['first_name'],users['last_name']])) </code></pre> <p>既然你改变了问题,就更新一个</p> ^{pr2}$
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
尝试加密和解密文本文件(Python)
1 回答
尝试加密文本文件会导致类型错误
8 回答
尝试加密时溢出错误
4 回答
尝试加载.plist文件时出现“无此类文件或目录”错误
5 回答
尝试加载“blog.templatetags.blog_标记”时引发ImportError:没有名为“markdown”的模块
8 回答
尝试加载“menus.templatetags.menus\u标记”时引发ImportError:无法从“menus.models”导入名称“menus”
10 回答
尝试加载5GB文本fi时发生Python内存错误
6 回答
尝试加载5GB文本文件时出现MemoryError
6 回答
尝试加载Django模板时出现渲染错误
1 回答
尝试加载exi时出错
2 回答
尝试加载imag时使用精灵表失败
5 回答
尝试加载json.Python时出错
1 回答
尝试加载json文件时遇到此错误:json.decoder.jsondeCoderror:预期值:第1行第1列(char 0)
7 回答
尝试加载keras和tensorflow,我仍然得到ModuleNotFoundError:在我的Anaconda环境中没有名为'tensorflow_core.estimator'的模块
7 回答
尝试加载Librosa的示例fi时出现“找不到文件”错误
5 回答
尝试加载matplotlib时发生iPython笔记本错误
4 回答
尝试加载MP3时出现PyGame错误
1 回答
尝试加载pickled matplotlib figu时发生AttributeError
1 回答
尝试加载pickle文件时出错
6 回答
尝试加载Pytoch模型时遇到问题:“在模块中找不到标识”
6 回答