參考連接: Python數據分析與展現
參考連接: Pandas官網
參考連接: User Guide
參考連接: Getting started tutorialsjavascript
算術運算:
html
算術運算法則: 算術運算根據行列索引,補齊後運算,運算默認產生浮點數 補齊時缺項填充NaN (空值) 二維和一維、一維和零維間爲廣播運算 採用+ ‐ * /符號進行的二元運算產生新的對象
實驗演示1:
java
Microsoft Windows [版本 10.0.18363.1198] (c) 2019 Microsoft Corporation。保留全部權利。 C:\Users\chenxuqi>python Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import pandas as pd >>> import numpy as np >>> a = pd.DataFrame(np.arange(12).reshape(3,4)) >>> a 0 1 2 3 0 0 1 2 3 1 4 5 6 7 2 8 9 10 11 >>> b = pd.DataFrame(np.arange(20).reshape(4,5)) >>> b 0 1 2 3 4 0 0 1 2 3 4 1 5 6 7 8 9 2 10 11 12 13 14 3 15 16 17 18 19 >>> a + b 0 1 2 3 4 0 0.0 2.0 4.0 6.0 NaN 1 9.0 11.0 13.0 15.0 NaN 2 18.0 20.0 22.0 24.0 NaN 3 NaN NaN NaN NaN NaN >>> a * b 0 1 2 3 4 0 0.0 1.0 4.0 9.0 NaN 1 20.0 30.0 42.0 56.0 NaN 2 80.0 99.0 120.0 143.0 NaN 3 NaN NaN NaN NaN NaN >>> >>>
算術運算的使用方法:
python
方法 | 說明 |
---|---|
.add(d, **argws) | 類型間加法運算,可選參數 |
.sub(d, **argws) | 類型間減法運算,可選參數 |
.mul(d, **argws) | 類型間乘法運算,可選參數 |
.div(d, **argws) | 類型間除法運算,可選參數 |
實驗演示2:
web
>>> >>> a = pd.DataFrame(np.arange(12).reshape(3,4)) >>> a 0 1 2 3 0 0 1 2 3 1 4 5 6 7 2 8 9 10 11 >>> b = pd.DataFrame(np.arange(20).reshape(4,5)) >>> b 0 1 2 3 4 0 0 1 2 3 4 1 5 6 7 8 9 2 10 11 12 13 14 3 15 16 17 18 19 >>> b.add(a) 0 1 2 3 4 0 0.0 2.0 4.0 6.0 NaN 1 9.0 11.0 13.0 15.0 NaN 2 18.0 20.0 22.0 24.0 NaN 3 NaN NaN NaN NaN NaN >>> b.add(a,fill_value=20200910) 0 1 2 3 4 0 0.0 2.0 4.0 6.0 20200914.0 1 9.0 11.0 13.0 15.0 20200919.0 2 18.0 20.0 22.0 24.0 20200924.0 3 20200925.0 20200926.0 20200927.0 20200928.0 20200929.0 >>> >>> a.mul(b) 0 1 2 3 4 0 0.0 1.0 4.0 9.0 NaN 1 20.0 30.0 42.0 56.0 NaN 2 80.0 99.0 120.0 143.0 NaN 3 NaN NaN NaN NaN NaN >>> a.mul(b,fill_value=20200910) 0 1 2 3 4 0 0.0 1.0 4.0 9.0 80803640.0 1 20.0 30.0 42.0 56.0 181808190.0 2 80.0 99.0 120.0 143.0 282812740.0 3 303013650.0 323214560.0 343415470.0 363616380.0 383817290.0 >>> >>> c = pd.Series(np.arange(4)) >>> c 0 0 1 1 2 2 3 3 dtype: int32 >>> >>> # 一維和零維之間的廣播 ... >>> c - 20200910 0 -20200910 1 -20200909 2 -20200908 3 -20200907 dtype: int32 >>> >>> # 二維與一維之間的廣播,默認在1軸上進行廣播 ... >>> b - c 0 1 2 3 4 0 0.0 0.0 0.0 0.0 NaN 1 5.0 5.0 5.0 5.0 NaN 2 10.0 10.0 10.0 10.0 NaN 3 15.0 15.0 15.0 15.0 NaN >>> b.sub(c,axis=0) 0 1 2 3 4 0 0 1 2 3 4 1 4 5 6 7 8 2 8 9 10 11 12 3 12 13 14 15 16 >>> b.sub(c) 0 1 2 3 4 0 0.0 0.0 0.0 0.0 NaN 1 5.0 5.0 5.0 5.0 NaN 2 10.0 10.0 10.0 10.0 NaN 3 15.0 15.0 15.0 15.0 NaN >>> >>>
比較運算:
算法
比較運算法則: 比較運算只能比較相同索引的元素,不進行補齊 二維和一維、一維和零維間爲廣播運算 採用> < >= <= == !=等符號進行的二元運算產生布爾對象
實驗演示3:
ide
>>> >>> # 比較運算 ... # 比較運算只能比較相同索引的元素,不進行補齊 ... # 二維和一維、一維和零維間爲廣播運算 ... # 採用> < >= <= == !=等符號進行的二元運算產生布爾對象 ... >>> a = pd.DataFrame(np.arange(12).reshape(3,4)) >>> a 0 1 2 3 0 0 1 2 3 1 4 5 6 7 2 8 9 10 11 >>> d = pd.DataFrame(np.arange(12,0,-1).reshape(3,4)) >>> d 0 1 2 3 0 12 11 10 9 1 8 7 6 5 2 4 3 2 1 >>> a > d 0 1 2 3 0 False False False False 1 False False False True 2 True True True True >>> a == d 0 1 2 3 0 False False False False 1 False False True False 2 False False False False >>> >>> >>> >>> >>> a = pd.DataFrame(np.arange(12).reshape(3,4)) >>> c = pd.Series(np.arange(4)) >>> a 0 1 2 3 0 0 1 2 3 1 4 5 6 7 2 8 9 10 11 >>> c 0 0 1 1 2 2 3 3 dtype: int32 >>> a > c 0 1 2 3 0 False False False False 1 True True True True 2 True True True True >>> c > 0 0 False 1 True 2 True 3 True dtype: bool >>> >>>
本文同步分享在 博客「敲代碼的小風」(CSDN)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。svg