【Pandas】pandas DataFrame rsub

打印 上一主题 下一主题

主题 1700|帖子 1700|积分 5100

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
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: 使用标量举行反向减法运算

  1. import pandas as pd
  2. df = pd.DataFrame({
  3.     'A': [1, 2, 3],
  4.     'B': [4, 5, 6],
  5.     'C': [7, 8, 9]
  6. })
  7. print("原始 DataFrame:")
  8. print(df)
  9. result = df.rsub(10)
  10. print("\n反向减法后的 DataFrame (使用 rsub 并指定标量 10):")
  11. print(result)
复制代码
效果:
  1. 原始 DataFrame:
  2.    A  B  C
  3. 0  1  4  7
  4. 1  2  5  8
  5. 2  3  6  9
  6. 反向减法后的 DataFrame (使用 rsub 并指定标量 10):
  7.     A   B   C
  8. 0   9   6   3
  9. 1   8   5   2
  10. 2   7   4   1
复制代码
示例 2: 使用序列举行反向减法运算

  1. import pandas as pd
  2. df = pd.DataFrame({
  3.     'A': [1, 2, 3],
  4.     'B': [4, 5, 6],
  5.     'C': [7, 8, 9]
  6. })
  7. other = pd.Series([1, 2, 3])
  8. print("原始 DataFrame:")
  9. print(df)
  10. result = df.rsub(other, axis=0)
  11. print("\n反向减法后的 DataFrame (使用 rsub 并指定序列):")
  12. print(result)
复制代码
效果:
  1. 原始 DataFrame:
  2.    A  B  C
  3. 0  1  4  7
  4. 1  2  5  8
  5. 2  3  6  9
  6. 反向减法后的 DataFrame (使用 rsub 并指定序列):
  7.     A   B   C
  8. 0   0  -3  -6
  9. 1   0  -3  -6
  10. 2   0  -3  -6
复制代码
示例 3: 使用 DataFrame 举行反向减法运算

  1. import pandas as pd
  2. df = pd.DataFrame({
  3.     'A': [1, 2, 3],
  4.     'B': [4, 5, 6],
  5.     'C': [7, 8, 9]
  6. })
  7. other_df = pd.DataFrame({
  8.     'A': [1, 2, 3],
  9.     'B': [4, 5, 6],
  10.     'C': [7, 8, 9]
  11. })
  12. print("原始 DataFrame:")
  13. print(df)
  14. result = df.rsub(other_df)
  15. print("\n反向减法后的 DataFrame (使用 rsub 并指定 DataFrame):")
  16. print(result)
复制代码
效果:
  1. 原始 DataFrame:
  2.    A  B  C
  3. 0  1  4  7
  4. 1  2  5  8
  5. 2  3  6  9
  6. 反向减法后的 DataFrame (使用 rsub 并指定 DataFrame):
  7.    A  B  C
  8. 0  0  0  0
  9. 1  0  0  0
  10. 2  0  0  0
复制代码
示例 4: 使用字典举行反向减法运算

  1. import pandas as pd
  2. df = pd.DataFrame({
  3.     'A': [1, 2, 3],
  4.     'B': [4, 5, 6],
  5.     'C': [7, 8, 9]
  6. })
  7. other_dict = {'A': 1, 'B': 2, 'C': 3}
  8. print("原始 DataFrame:")
  9. print(df)
  10. result = df.rsub(other_dict)
  11. print("\n反向减法后的 DataFrame (使用 rsub 并指定字典):")
  12. print(result)
复制代码
效果:
  1. 原始 DataFrame:
  2.    A  B  C
  3. 0  1  4  7
  4. 1  2  5  8
  5. 2  3  6  9
  6. 反向减法后的 DataFrame (使用 rsub 并指定字典):
  7.    A  B  C
  8. 0  0 -2 -4
  9. 1  0 -3 -5
  10. 2  0 -4 -6
复制代码
表明


  • 使用标量举行反向减法运算:

    • df.rsub(10) 计算 DataFrame df 中的每个元素与标量 10 的减法。
    • 效果是一个新的 DataFrame,其中每个元素是 10 减去 df 中的元素。

  • 使用序列举行反向减法运算:

    • df.rsub(other, axis=0) 计算 DataFrame df 的每一行与序列 other 的对应元素的减法。
    • 效果是一个新的 DataFrame,其中每个元素是 other 的对应元素减去 df 的元素。

  • 使用 DataFrame 举行反向减法运算:

    • df.rsub(other_df) 计算 DataFrame df 与 other_df 的对应元素的减法。
    • 效果是一个新的 DataFrame,其中每个元素是 other_df 的元素减去 df 的元素。

  • 使用字典举行反向减法运算:

    • df.rsub(other_dict) 计算 DataFrame df 的每一列与字典 other_dict 中对应键的值的减法。
    • 效果是一个新的 DataFrame,其中每个元素是字典 other_dict 中的值减去 df 的元素。

这些示例展示了 DataFrame.rsub 方法的不同用法及其效果。根据具体需求,可以选择合适的参数来举行反向减法运算。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

不到断气不罢休

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表