Pandas2.2 DataFrame
Binary operator functions
方法 | 描述 |
---|---|
DataFrame.add(other) | 用于執行 DataFrame 與另一個對象(如 DataFrame、Series 或標量)的逐元素加法操作 |
DataFrame.add(other[, axis, level, fill_value]) | 用于執行 DataFrame 與另一個對象(如 DataFrame、Series 或標量)的逐元素加法操作 |
DataFrame.sub(other[, axis, level, fill_value]) | 用于執行逐元素的減法操作 |
DataFrame.mul(other[, axis, level, fill_value]) | 用于執行逐元素的乘法操作 |
DataFrame.div(other[, axis, level, fill_value]) | 用于執行逐元素的除法操作 |
DataFrame.truediv(other[, axis, level, …]) | 用于執行逐元素的真除法操作 |
DataFrame.floordiv(other[, axis, level, …]) | 用于執行逐元素的地板除法操作 |
DataFrame.mod(other[, axis, level, fill_value]) | 用于執行逐元素的取模操作 |
DataFrame.pow(other[, axis, level, fill_value]) | 用于對 DataFrame 中的元素進行冪運算 |
DataFrame.dot(other) | 用于計算兩個 DataFrame(或 DataFrame 與 Series/數組)之間的**矩陣點積(矩陣乘法)**的方法 |
DataFrame.radd(other[, axis, level, fill_value]) | 用于執行反向加法運算 |
DataFrame.rsub(other[, axis, level, fill_value]) | 用于執行反向減法運算 |
pandas.DataFrame.rsub()
pandas.DataFrame.rsub
方法用于執行反向減法運算。具體來說,它相當于調用 other - self
,其中 self
是調用該方法的 DataFrame。以下是該方法的參數說明及其功能:
參數說明
- other: 用于進行減法運算的值,可以是標量、序列、DataFrame 或字典。
- axis: 指定沿哪個軸進行運算。
0
或'index'
表示沿行進行運算,1
或'columns'
表示沿列進行運算。默認為1
。 - level: 如果
other
是一個 MultiIndex,則指定沿哪個級別進行運算。默認為None
。 - fill_value: 用于填充缺失值的值。默認為
None
。
示例及結果
示例 1: 使用標量進行反向減法運算
import pandas as pddf = pd.DataFrame({'A': [1, 2, 3],'B': [4, 5, 6],'C': [7, 8, 9]
})print("原始 DataFrame:")
print(df)result = df.rsub(10)
print("\n反向減法后的 DataFrame (使用 rsub 并指定標量 10):")
print(result)
結果:
原始 DataFrame:A B C
0 1 4 7
1 2 5 8
2 3 6 9反向減法后的 DataFrame (使用 rsub 并指定標量 10):A B C
0 9 6 3
1 8 5 2
2 7 4 1
示例 2: 使用序列進行反向減法運算
import pandas as pddf = pd.DataFrame({'A': [1, 2, 3],'B': [4, 5, 6],'C': [7, 8, 9]
})other = pd.Series([1, 2, 3])print("原始 DataFrame:")
print(df)result = df.rsub(other, axis=0)
print("\n反向減法后的 DataFrame (使用 rsub 并指定序列):")
print(result)
結果:
原始 DataFrame:A B C
0 1 4 7
1 2 5 8
2 3 6 9反向減法后的 DataFrame (使用 rsub 并指定序列):A B C
0 0 -3 -6
1 0 -3 -6
2 0 -3 -6
示例 3: 使用 DataFrame 進行反向減法運算
import pandas as pddf = pd.DataFrame({'A': [1, 2, 3],'B': [4, 5, 6],'C': [7, 8, 9]
})other_df = pd.DataFrame({'A': [1, 2, 3],'B': [4, 5, 6],'C': [7, 8, 9]
})print("原始 DataFrame:")
print(df)result = df.rsub(other_df)
print("\n反向減法后的 DataFrame (使用 rsub 并指定 DataFrame):")
print(result)
結果:
原始 DataFrame:A B C
0 1 4 7
1 2 5 8
2 3 6 9反向減法后的 DataFrame (使用 rsub 并指定 DataFrame):A B C
0 0 0 0
1 0 0 0
2 0 0 0
示例 4: 使用字典進行反向減法運算
import pandas as pddf = pd.DataFrame({'A': [1, 2, 3],'B': [4, 5, 6],'C': [7, 8, 9]
})other_dict = {'A': 1, 'B': 2, 'C': 3}print("原始 DataFrame:")
print(df)result = df.rsub(other_dict)
print("\n反向減法后的 DataFrame (使用 rsub 并指定字典):")
print(result)
結果:
原始 DataFrame:A B C
0 1 4 7
1 2 5 8
2 3 6 9反向減法后的 DataFrame (使用 rsub 并指定字典):A B C
0 0 -2 -4
1 0 -3 -5
2 0 -4 -6
解釋
-
使用標量進行反向減法運算:
df.rsub(10)
計算 DataFramedf
中的每個元素與標量10
的減法。- 結果是一個新的 DataFrame,其中每個元素是
10
減去df
中的元素。
-
使用序列進行反向減法運算:
df.rsub(other, axis=0)
計算 DataFramedf
的每一行與序列other
的對應元素的減法。- 結果是一個新的 DataFrame,其中每個元素是
other
的對應元素減去df
的元素。
-
使用 DataFrame 進行反向減法運算:
df.rsub(other_df)
計算 DataFramedf
與other_df
的對應元素的減法。- 結果是一個新的 DataFrame,其中每個元素是
other_df
的元素減去df
的元素。
-
使用字典進行反向減法運算:
df.rsub(other_dict)
計算 DataFramedf
的每一列與字典other_dict
中對應鍵的值的減法。- 結果是一個新的 DataFrame,其中每個元素是字典
other_dict
中的值減去df
的元素。
這些示例展示了 DataFrame.rsub
方法的不同用法及其效果。根據具體需求,可以選擇合適的參數來進行反向減法運算。