当前位置:网站首页>Unpleasant error typeerror: cannot perform 'ROR_‘ with a dtyped [float64] array and scalar of type [bool]
Unpleasant error typeerror: cannot perform 'ROR_‘ with a dtyped [float64] array and scalar of type [bool]
2022-07-06 15:20:00 【Jane said Python】
Hello everyone , I'm an old watch , This series will record some problems and solutions I encounter in programming , Will directly take the error report encountered as the title , In this way, it is more convenient for other learners to search for problem solutions in the browser , You are also welcome to comment on 、 More communication in the message area , Talk about your understanding or problems .
such as Unpleasant –requests.exceptions.ProxyError Is a good demonstration .
Today, when processing a data , Executed a line of code , As a result, there was a big problem !!!
# Simple data processing , Remove recommended hot search and top hot search
wb_hot_data.drop(wb_hot_data[wb_hot_data['wb_rank']=='•' | pd.isna(wb_hot_data['wb_rank'])].index)
Look at this pile of errors , I guess the code is also wronged !!!
Let's talk directly about how to solve :
# Simple data processing , Remove recommended hot search and top hot search
wb_hot_data.drop(wb_hot_data[(wb_hot_data['wb_rank']=='•') | pd.isna(wb_hot_data['wb_rank'])].index)
Maybe you don't see what has changed , In fact, it's for wb_hot_data['wb_rank']=='•'
Added a bracket .
Next, let's talk about how this error occurs with example data ?
import pandas as pd
import numpy as np
a=np.array([['a',2,3],['b',5,6],[np.NaN,8,9],['a',4,7]])
df1=pd.DataFrame(a,columns=list('ABC'))
print(df1)
''' A B C 0 a 2 3 1 b 5 6 2 nan 8 9 3 a 4 7 '''
I want to delete A The value in the column is ’a’ perhaps None The line of :
df1.drop(df1[df1['A']=='a' | pd.isna(df1['A'])].index)
There seems to be nothing wrong with writing like this , But when running, there will be annoying errors in the title .
So Google checked , eureka An article with the same mistake , Bloggers in the comment area put parentheses around an expression , It's a priority issue .
See the answer , I understand , If you don't have a solid foundation, you will suffer losses .
df1.drop((df1[df1['A']=='a') | pd.isna(df1['A'])].index)
# Just add a bracket
''' A B C 1 b 5 6 2 nan 8 9 '''
We'll find here nan Not deleted ? Because the initialization is this nan Has been automatically converted to a string , So it can't be recognized as nan Be deleted , Let's talk about this in other articles , You can also leave a message to say your views .
Add another case , When A Column as numeric data , See what happens ?
import pandas as pd
import numpy as np
# a=np.array([['a',2,3],['b',5,6],[np.NaN,8,9],['a',4,7]])
a=np.array([[1,2,3],[2,5,6],[np.NaN,8,9],[2,4,7]])
df1=pd.DataFrame(a,columns=list('ABC'))
print(df1)
''' A B C 0 1.0 2.0 3.0 1 2.0 5.0 6.0 2 NaN 8.0 9.0 3 2.0 4.0 7.0 '''
# I want to delete A The value in the column is 2 perhaps None The line of
df1.drop(df1[df1['A']==2 | pd.isna(df1['A'])].index)
''' A B C 1 2.0 5.0 6.0 2 NaN 8.0 9.0 3 2.0 4.0 7.0 '''
At this time, we found out , No report error , But the result is obviously problematic , Why is that ?
This is because numeric types can be directly associated with bool Type data , So there was no error ; The result is wrong because by operator priority , to 2 | pd.isna(df1['A'])
, What comes out is true(2 Is a definite number , stay bool The type is true,true Comparing with the numerical value will turn into 1), And then df1[‘A’] Conduct ==
operation , You'll find only the first line 1 Is satisfied (1==ture), So the first line will be deleted .
So it's still operator priority , Or add ()
You can solve the problem .
df1.drop(df1[(df1['A']==2) | pd.isna(df1['A'])].index)
''' A B C 0 1.0 2.0 3.0 '''
# Why can we put nan Value delete ? You can talk about
So the problem we have today is caused by operator priority String type data and bool Type data cannot be |
operation .
The last attached Python List of operator priority and specificity .
Reference resources
[1] stackoverflow Answer relevant questions
https://stackoverflow.com/questions/20333435/pandas-comparison-raises-typeerror-cannot-compare-a-dtyped-float64-array-with
[2] Python List of operator priority and specificity
http://c.biancheng.net/view/2190.html
边栏推荐
- 软件测试面试要问的性能测试术语你知道吗?
- Pedestrian re identification (Reid) - data set description market-1501
- Should wildcard import be avoided- Should wildcard import be avoided?
- Servlet
- Practical cases, hand-in-hand teaching you to build e-commerce user portraits | with code
- ucore lab7
- Contest3145 - the 37th game of 2021 freshman individual training match_ A: Prizes
- CSAPP Shell Lab 实验报告
- Daily code 300 lines learning notes day 9
- CSAPP homework answers chapter 789
猜你喜欢
CSAPP家庭作業答案7 8 9章
Portapack application development tutorial (XVII) nRF24L01 launch B
想跳槽?面试软件测试需要掌握的7个技能你知道吗
Pedestrian re identification (Reid) - Overview
Leetcode simple question: check whether the numbers in the sentence are increasing
Maximum nesting depth of parentheses in leetcode simple questions
如何成为一个好的软件测试员?绝大多数人都不知道的秘密
Leetcode simple question: check whether two strings are almost equal
自动化测试你必须要弄懂的问题,精品总结
Capitalize the title of leetcode simple question
随机推荐
Expanded polystyrene (EPS) global and Chinese markets 2022-2028: technology, participants, trends, market size and share Research Report
MySQL数据库(五)视 图 、 存 储 过 程 和 触 发 器
Global and Chinese market of RF shielding room 2022-2028: Research Report on technology, participants, trends, market size and share
線程及線程池
Mysql database (V) views, stored procedures and triggers
Example 071 simulates a vending machine, designs a program of the vending machine, runs the program, prompts the user, enters the options to be selected, and prompts the selected content after the use
Global and Chinese markets of MPV ACC ECU 2022-2028: Research Report on technology, participants, trends, market size and share
The minimum sum of the last four digits of the split digit of leetcode simple problem
Description of Vos storage space, bandwidth occupation and PPS requirements
The most detailed postman interface test tutorial in the whole network. An article meets your needs
基于485总线的评分系统双机实验报告
Public key box
[oiclass] maximum formula
Oracle foundation and system table
Portapack application development tutorial (XVII) nRF24L01 launch B
Lab 8 file system
Leetcode simple question: check whether two strings are almost equal
几款开源自动化测试框架优缺点对比你知道吗?
Knowledge that you need to know when changing to software testing
Mysql database (II) DML data operation statements and basic DQL statements