Pandas binary operation

Fingertip programming 2021-04-07 16:46:33
pandas binary operation

Pandas Binary operations between data structures , Pay attention to the following two key points :

  • Multidimensional (DataFrame) And low dimension (Series) The broadcast mechanism between objects
  • Missing value processing in calculation

These two problems can be solved at the same time , But let's introduce how to deal with it separately .

matching / Broadcast mechanism

DataFrame Support add()、sub()、mul()、div()、radd()、rsub() And so on . The broadcast mechanism focuses on the input Series, adopt axis keyword , matching index or columns You can call these functions .

You can also use Series Multi level index DataFrame A certain level of .

Series And Index And support divmod() Built in functions , This function performs both integer division down and modular operations , Returns two tuples of the same type as the left .

divmod It also supports element level operations :

Missing values and fill in missing values

Series And DataFrame The arithmetic function of supports fill_value Options , That is to replace the missing value of a position with the specified value . such as , Two DataFrame Add up , Except for two DataFrame There are missing values at the same position in the , The sum of them is still NaN, If only one DataFrame There is a missing value in , Available fill_value Specify a value to replace NaN, Of course , It can also be used. fillna hold NaN Replace with the desired value .

Comparison operation

Series And DataFrame And support eq、ne、lt、gt、le、ge And so on :

These operations generate a... Of the same type as the input object on the left Pandas object , namely dtype by bool.boolean Object can be used for index operations .

Boolean simplification

empty、any、all、bool Data aggregation can be reduced to a single Boolean value .

You can further simplify the above result to a single Boolean value .

adopt empty attribute , Can verify Pandas Whether the object is empty .

use bool Method validation single element Pandas Boolean value of the object

Compare whether the objects are equivalent

In general , There are many ways to get the same structure . With df+df And df*2 For example . Test whether the structure of the two calculation methods is consistent , Most people can use (df+df==df*2).all(), But the result of this expression is False.

Boolean type DataFrame df+df==df*2 There is False The value is because of two NaN The comparison of the values is unequal :

To verify that the data is equivalent ,Series And DataFrame etc. N Dimension framework provides equals() Method , Use this method to verify NaN The result of the value is equal .

But here we have to pay attention to Series And DataFrame The order of indexes must be consistent , The verification result can be True.

Compare array Type object

Use scalar values and Pandas The data structure is very simple compared to the data elements :

Pandas You can also compare two equal lengths array Data elements in objects :

It's not equal Index or Series Object will trigger valueError:

But the operation here is related to NumPy The broadcasting mechanism is different :

NumPy When the broadcast operation cannot be performed , return False:

If you want to learn Python, But we can't find the learning path and resources , Welcome Fingertip programming .

Online interactive learning Python, Faster and better !

本文为[Fingertip programming]所创,转载请带上原文链接,感谢

  1. 【Python从零到壹】面向对象的封装,继承和多态
  2. Scikit-learn 机器学习库介绍!【Python入门】
  3. Python能代替shell吗?有什么特点?
  4. 【Python从零到壹】Python文件的操作详解
  5. 【Python从零到壹】Python爬虫部分开篇
  6. 【Python从零到壹】学习Python爬虫前,你需要先掌握这些内容
  7. 【Python从零到壹】python爬虫系列-网络请求
  8. Object oriented encapsulation, inheritance and polymorphism
  9. Python经典面试题(附答案)!
  10. Scikit learn machine learning library introduction! [introduction to Python]
  11. 菜谱系统小成阶段,Python Web 领域终于攻占一个小山头
  12. Python Web 菜谱项目再次前进一步,从应用层了解内置用户认证系统
  13. Can Python replace shell? What are the features?
  14. Detailed explanation of Python file operation
  15. Python from zero to one
  16. [Python from zero to one] before learning Python crawler, you need to master these contents
  17. [Python from zero to one] Python crawler series - web request
  18. Python经典面试题(附答案)!
  19. Python classic interview questions (with answers)!
  20. When the recipe system was in its infancy, python web finally took over a small hill
  21. Python web menu project takes another step forward to learn about the built-in user authentication system from the application layer
  22. Python classic interview questions (with answers)!
  23. 【Python从零到壹】Python的循环结构详解
  24. 【Python从零到壹】Python列表详解
  25. 【Python从零到壹】Python的字典详解
  26. 【Python从零到壹】Python的字符串详解
  27. 【Python从零到壹】Python基础之函数的应用
  28. 【Python从零到壹】用Python实现植物大战僵尸里的面向对象
  29. Detailed explanation of Python loop structure
  30. Detailed explanation of Python list
  31. Detailed explanation of Python dictionary
  32. Detailed explanation of Python string
  33. [Python from zero to one] the application of Python basic functions
  34. [Python from zero to one] using Python to realize object-oriented in plant vs. zombie
  35. 用 Python 实现微信版飞机大战
  36. 用 Python 实现***帝国中的数字雨落既视感
  37. 想知道未来孩子长相?Python人脸融合告诉你
  38. 我用 Python 做了一个全球疫情数据大屏
  39. Using Python to realize wechat aircraft war
  40. Using Python to realize the visual sense of digital rain in the Empire of the Communist Party of China
  41. Want to know what kids will look like in the future? Python face fusion tells you
  42. I made a big screen of global epidemic data with Python
  43. python你TM太皮了——区区30行代码就能记录键盘的一举一动
  44. Python you TM too skinny - just 30 lines of code can record every move of the keyboard
  45. python的装饰器概念学习基础基础版
  46. Python decorator concept learning basic edition
  47. SQL配合Python-Flask的中转注入
  48. python3使用kivy生成安卓程序
  49. 不到 150 行代码写一个 Python 版的贪吃蛇
  50. Transfer injection of SQL and python flash
  51. Using Kivy to generate Android program in Python 3
  52. Less than 150 lines of code to write a python version of the snake
  53. Python面向对象练习题
  54. Python数据分析入门(八):Pandas统计计算和描述
  55. Python面向对象练习题
  56. Python object oriented exercises
  57. Introduction to Python data analysis (8): Pandas statistical calculation and description
  58. Python object oriented exercises
  59. WEB4-通过python获得flag
  60. python-web5