python pandas series items方法_Python Pandas Series.rsub()用法及代码示例

Pandas 系列是带有轴标签的一维ndarray。标签不必是唯一的,但必须是可哈希的类型。该对象同时支持基于整数和基于标签的索引,并提供了许多方法来执行涉及索引的操作。

Pandas Series.rsub()函数返回系列和其他逐元素的减法(二进制运算符rsub)。相当于other - series,但支持用fill_value代替输入之一中的丢失数据。

用法: Series.rsub(other, level=None, fill_value=None, axis=0)

参数:

other:序列或标量值

fill_value:填写现有的缺失(NaN)值

level:在一个级别上广播,在传递的MultiIndex级别上匹配索引值

返回:结果:系列

范例1:采用Series.rsub()函数,以标量元素为单位执行给定Series对象的反向减法。

# importing pandas as pd

import pandas as pd

# Creating the Series

sr = pd.Series([100, 25, 32, 118, 24, 65])

# Create the Index

index_ = ['Coca Cola', 'Sprite', 'Coke', 'Fanta', 'Dew', 'ThumbsUp']

# set the index

sr.index = index_

# Print the series

print(sr)

输出:

6580f5254d69c739aa804cabf8273a70.png

现在我们将使用Series.rsub()函数使用标量对给定Series对象执行逐元素的逆减法。

# perform reverse subtraction with 1000

selected_items = sr.rsub(other = 1000)

# Print the returned Series object

print(selected_items)

输出:

f77ea63ae557d0c45ed09c85307d2e5d.png

正如我们在输出中看到的,Series.rsub()函数已成功返回标量与给定Series对象的反减。

范例2:采用Series.rsub()函数,以标量元素为单位执行给定Series对象的反向减法。给定的Series对象还包含一些缺少的值。

# importing pandas as pd

import pandas as pd

# Creating the Series

sr = pd.Series([19.5, 16.8, None, 22.78, None, 20.124, None, 18.1002, None])

# Print the series

print(sr)

输出:

38a673d26a6ae039369f4688877f0307.png

现在我们将使用Series.rsub()函数使用标量对给定Series对象执行逐元素的逆减法。我们还将在给定Series对象中所有缺失值的位置替换100。

# perform reverse subtraction with 1000

# fill 100 at the place of all missing values

selected_items = sr.rsub(other = 1000, fill_value = 100)

# Print the returned Series object

print(selected_items)

输出:

909012f37c0aa7ca33c5483841f9245d.png

正如我们在输出中看到的,Series.rsub()函数已成功返回标量与给定Series对象的反减。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值