
- Python Pandas 教程
- Python Pandas - 首頁
- Python Pandas - 簡介
- Python Pandas - 環境設定
- Python Pandas - 基礎知識
- Python Pandas - 資料結構介紹
- Python Pandas - 索引物件
- Python Pandas - Panel
- Python Pandas - 基本功能
- Python Pandas - 索引和資料選擇
- Python Pandas - Series
- Python Pandas - Series
- Python Pandas - 切片Series物件
- Python Pandas - Series物件的屬性
- Python Pandas - 數列物件的算術運算
- Python Pandas - 將Series轉換為其他物件
- Python Pandas - DataFrame
- Python Pandas - DataFrame
- Python Pandas - 訪問DataFrame
- Python Pandas - 切片DataFrame物件
- Python Pandas - 修改DataFrame
- Python Pandas - 從DataFrame中刪除行
- Python Pandas - DataFrame的算術運算
- Python Pandas - I/O工具
- Python Pandas - I/O工具
- Python Pandas - 使用CSV格式
- Python Pandas - 讀取和寫入JSON檔案
- Python Pandas - 從Excel檔案讀取資料
- Python Pandas - 將資料寫入Excel檔案
- Python Pandas - 使用HTML資料
- Python Pandas - 剪貼簿
- Python Pandas - 使用HDF5格式
- Python Pandas - 與SQL的比較
- Python Pandas - 資料處理
- Python Pandas - 排序
- Python Pandas - 重新索引
- Python Pandas - 迭代
- Python Pandas - 連線
- Python Pandas - 統計函式
- Python Pandas - 描述性統計
- Python Pandas - 處理文字資料
- Python Pandas - 函式應用
- Python Pandas - 選項和自定義
- Python Pandas - 視窗函式
- Python Pandas - 聚合
- Python Pandas - 合併/連線
- Python Pandas - 多級索引
- Python Pandas - 多級索引基礎
- Python Pandas - 使用多級索引進行索引
- Python Pandas - 使用多級索引進行高階重新索引
- Python Pandas - 重新命名多級索引標籤
- Python Pandas - 對多級索引進行排序
- Python Pandas - 二元運算
- Python Pandas - 二元比較運算
- Python Pandas - 布林索引
- Python Pandas - 布林掩碼
- Python Pandas - 資料重塑和透視
- Python Pandas - 透視表
- Python Pandas - 堆疊和解堆疊
- Python Pandas - 熔化
- Python Pandas - 計算虛擬變數
- Python Pandas - 分類資料
- Python Pandas - 分類資料
- Python Pandas - 分類資料的排序
- Python Pandas - 分類資料的比較
- Python Pandas - 處理缺失資料
- Python Pandas - 缺失資料
- Python Pandas - 填充缺失資料
- Python Pandas - 缺失值的插值
- Python Pandas - 刪除缺失資料
- Python Pandas - 使用缺失資料進行計算
- Python Pandas - 處理重複項
- Python Pandas - 重複資料
- Python Pandas - 計數和檢索唯一元素
- Python Pandas - 重複標籤
- Python Pandas - 分組和聚合
- Python Pandas - GroupBy
- Python Pandas - 時間序列資料
- Python Pandas - 日期功能
- Python Pandas - Timedelta
- Python Pandas - 稀疏資料結構
- Python Pandas - 稀疏資料
- Python Pandas - 視覺化
- Python Pandas - 視覺化
- Python Pandas - 其他概念
- Python Pandas - 注意事項
- Python Pandas 有用資源
- Python Pandas - 快速指南
- Python Pandas - 有用資源
- Python Pandas - 討論
Python Pandas - 數列物件的算術運算
Pandas Series是主要的 資料結構之一,它儲存一維標記資料。資料可以是任何型別,例如整數、浮點數或字串。使用Pandas Series的主要優點之一是能夠以向量化方式執行算術運算。這意味著對Series的算術運算無需手動迴圈遍歷元素。
在本教程中,我們將學習如何將加法(+)、減法(-)、乘法(*)和除法(/)等算術運算應用於單個Series或兩個Series物件之間。
對Series與標量值的算術運算
Pandas Series物件的算術運算可以直接應用於整個Series元素,這意味著該運算將逐元素地對所有值執行。這與NumPy陣列的操作方式非常相似。
以下是Pandas Series中常用算術運算的列表:
運算 | 示例 | 描述 |
---|---|---|
加法 | s + 2 | 將2加到每個元素 |
減法 | s - 2 | 從每個元素中減去2 |
乘法 | s * 2 | 將每個元素乘以2 |
除法 | s / 2 | 將每個元素除以2 |
冪運算 | s ** 2 | 將每個元素的冪提升到2 |
取模 | s % 2 | 求除以2的餘數 |
地板除 | s // 2 | 除法並向下取整 |
示例
以下示例演示瞭如何將所有算術運算應用於帶有標量值的Series物件。
import pandas as pd s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e']) # Display the Input Series print('Input Series\n',s) # Apply all Arithmetic Operation and Display the Results print('\nAddition:\n',s+2) print('\nSubtraction:\n', s-2) print('\nMultiplication:\n', s * 2) print('\nDivision:\n', s/2) print('\nExponentiation:\n', s**2) print('\nModulus:\n', s%2) print('\nFloor Division:\n', s//2)
以下是上述程式碼的輸出:
Input Series a 1 b 2 c 3 d 4 e 5 dtype: int64 Addition: a 3 b 4 c 5 d 6 e 7 dtype: int64 Subtraction: a -1 b 0 c 1 d 2 e 3 dtype: int64 Multiplication: a 2 b 4 c 6 d 8 e 10 dtype: int64 Division: a 0.5 b 1.0 c 1.5 d 2.0 e 2.5 dtype: float64 Exponentiation: a 1 b 4 c 9 d 16 e 25 dtype: int64 Modulus: a 1 b 0 c 1 d 0 e 1 dtype: int64 Floor Division: a 0 b 1 c 1 d 2 e 2 dtype: int64
兩個Series之間的算術運算
您可以在兩個Series物件之間執行算術運算。Pandas會自動根據索引標籤對齊資料。如果其中一個Series物件沒有索引,而另一個有索引,則該索引的結果值將為NaN。
示例
此示例演示了對兩個Series物件應用算術運算。
import pandas as pd s1 = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e']) s2 = pd.Series([9, 8, 6, 5], index=['x','a','b','c']) # Apply all Arithmetic Operations and Display the Results print('\nAddition:\n',s1+s2) print('\nSubtraction:\n', s1-s2) print('\nMultiplication:\n', s1 * s2) print('\nDivision:\n', s1/s2)
以下是上述程式碼的輸出:
Addition: a 9.0 b 8.0 c 8.0 d NaN e NaN x NaN dtype: float64 Subtraction: a -7.0 b -4.0 c -2.0 d NaN e NaN x NaN dtype: float64 Multiplication: a 8.0 b 12.0 c 15.0 d NaN e NaN x NaN dtype: float64 Division: a 0.125000 b 0.333333 c 0.600000 d NaN e NaN x NaN dtype: float64
廣告