Python酷库之旅-第三方库Pandas(035)

目录

一、用法精讲

106、pandas.Series.iloc方法

106-1、语法

106-2、参数

106-3、功能

106-4、返回值

106-5、说明

106-6、用法

106-6-1、数据准备

106-6-2、代码示例

106-6-3、结果输出

107、pandas.Series.__iter__魔法方法

107-1、语法

107-2、参数

107-3、功能

107-4、返回值

107-5、说明

107-6、用法

107-6-1、数据准备

107-6-2、代码示例

107-6-3、结果输出

108、pandas.Series.items方法

108-1、语法

108-2、参数

108-3、功能

108-4、返回值

108-5、说明

108-6、用法

108-6-1、数据准备

108-6-2、代码示例

108-6-3、结果输出

109、pandas.Series.keys方法

109-1、语法

109-2、参数

109-3、功能

109-4、返回值

109-5、说明

109-6、用法

109-6-1、数据准备

109-6-2、代码示例

109-6-3、结果输出

110、pandas.Series.pop方法

110-1、语法

110-2、参数

110-3、功能

110-4、返回值

110-5、说明

110-6、用法

110-6-1、数据准备

110-6-2、代码示例

110-6-3、结果输出

二、推荐阅读

1、Python筑基之旅

2、Python函数之旅

3、Python算法之旅

4、Python魔法之旅

5、博客个人主页

一、用法精讲

106、pandas.Series.iloc方法
106-1、语法
# 106、pandas.Series.iloc方法
pandas.Series.iloc
Purely integer-location based indexing for selection by position.

Deprecated since version 2.2.0: Returning a tuple from a callable is deprecated.

.iloc[] is primarily integer position based (from 0 to length-1 of the axis), but may also be used with a boolean array.

Allowed inputs are:

An integer, e.g. 5.

A list or array of integers, e.g. [4, 3, 0].

A slice object with ints, e.g. 1:7.

A boolean array.

A callable function with one argument (the calling Series or DataFrame) and that returns valid output for indexing (one of the above). This is useful in method chains, when you don’t have a reference to the calling object, but would like to base your selection on some value.

A tuple of row and column indexes. The tuple elements consist of one of the above inputs, e.g. (0, 1).

.iloc will raise IndexError if a requested indexer is out-of-bounds, except slice indexers which allow out-of-bounds indexing (this conforms with python/numpy slice semantics).

See more at Selection by Position.
106-2、参数

        无

106-3、功能

        用于基于整数位置选择数据的索引器,它允许你通过行的整数位置来选择数据, 主要用于选择特定位置上的元素、切片或者行列组合,该方法在数据处理和分析中非常有用,特别是在你不关心具体标签而只关心位置时。

106-4、返回值

        返回值类型取决于你选择的数据的类型和数量,下面详细说明几种常见的返回值类型:

106-4-1、单个元素:如果你通过iloc选择了单个元素(例如s.iloc[2]),返回值将是该元素的具体值,这可以是整数、浮点数、字符串等,具体取决于Series中的元素类型。

106-4-2、多个元素:如果你选择了多个元素(例如s.iloc[1,3]),返回值将是一个pandas.Series对象,包含所选位置的元素,索引仍然是原始的索引值。

106-4-3、切片:如果你使用了切片(例如s.iloc[1:3]),返回值将是一个pandas.Series对象,包含所选范围内的所有元素,切片返回的Series保留了原始的索引。

106-4-4、行和列的组合:如果在DataFrame中使用iloc选择特定的行和列(例如df.iloc[0,1]),返回值将是该位置的具体值。

106-5、说明

106-5-1、iloc只接受整数类型的索引或者整数类型的切片。

106-5-2、如果索引超出了范围,会引发IndexError。

106-6、用法
106-6-1、数据准备
106-6-2、代码示例
# 106、pandas.Series.iloc方法
# 106-1、选择单个元素
import pandas as pd
s = pd.Series([10, 20, 30, 40])
print(s.iloc[2], end='\n\n')

# 106-2、选择多个元素
import pandas as pd
s = pd.Series([10, 20, 30, 40])
print(s.iloc[[1, 3]], end='\n\n')

# 106-3、切片
import pandas as pd
s = pd.Series([10, 20, 30, 40])
print(s.iloc[1:3], end='\n\n')  

# 106-4、选择特定位置的行和列
import pandas as pd
# 对于Series,通常只涉及单一维度,但对于DataFrame,iloc可以用于选择特定的行和列。
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
print(df.iloc[0, 1])
106-6-3、结果输出
# 106、pandas.Series.iloc方法
# 106-1、选择单个元素
# 30

# 106-2、选择多个元素
# 1    20
# 3    40
# dtype: int64

# 106-3、切片 
# 1    20
# 2    30
# dtype: int64

# 106-4、选择特定位置的行和列
# 4
107、pandas.Series.__iter__魔法方法
107-1、语法
# 107、pandas.Series.__iter__方法
pandas.Series.__iter__()
Return an iterator of the values.

These are each a scalar type, which is a Python scalar (for str, int, float) or a pandas scalar (for Timestamp/Timedelta/Interval/Period)

Returns:
iterator
107-2、参数

        无

107-3、功能

        让pandas.Series对象支持迭代。

107-4、返回值

        返回一个迭代器,可以逐个访问Series对象中的元素。

107-5、说明

107-5-1、返回值__iter__方法返回一个迭代器,该迭代器可以按Series对象中元素的顺序迭代。

107-5-2、性能:这个方法设计得非常高效,通常是通过直接访问底层的数据结构(如NumPy数组)来实现的,避免了不必要的内存开销。

107-5-3、内部实现__iter__ 方法内部通常会使用pandas库的数据结构(如Index对象和values属性)来实现高效的迭代。

107-5-4、用法:此方法使得Series可以与Python的其他迭代工具(如for循环、列表推导式等)无缝集成,简化了数据访问和处理的代码。

107-6、用法
107-6-1、数据准备
107-6-2、代码示例
# 107、pandas.Series.__iter__方法
import pandas as pd
# 创建一个Series对象
s = pd.Series([10, 20, 30, 40])
# 使用__iter__方法(隐式调用)
for value in s:
    print(value)
107-6-3、结果输出
# 107、pandas.Series.__iter__方法
# 10
# 20
# 30
# 40
108、pandas.Series.items方法
108-1、语法
# 108、pandas.Series.items方法
pandas.Series.items()
Lazily iterate over (index, value) tuples.

This method returns an iterable tuple (index, value). This is convenient if you want to create a lazy iterator.

Returns:
iterable
Iterable of tuples containing the (index, value) pairs from a Series.
108-2、参数

        无

108-3、功能

        用于返回一个生成器,这个生成器可以逐个提供pandas.Series对象中的每一个键值对,每个键值对以元组的形式返回,其中包含了Series的索引和值。

108-4、返回值

        返回一个生成器对象。每次迭代生成器时,都会返回一个包含Series索引和对应值的元组(index,value)

108-5、说明

108-5-1、返回类型:items方法返回的是一个生成器,这使得它在处理大数据集时比较高效,因为它不会一次性将所有数据加载到内存中。

108-5-2、用途:items方法通常用于需要同时访问索引和值的场景。例如,在进行数据遍历、构建报告或处理数据时,可以方便地获取每个索引和对应的值。

108-5-3、效率:由于生成器是惰性计算的,items方法的性能较好,尤其是当Series很大时,它不会立即生成所有的键值对,而是按需生成。

108-6、用法
108-6-1、数据准备
108-6-2、代码示例
# 108、pandas.Series.items方法
import pandas as pd
# 创建一个Series对象
s = pd.Series([10, 20, 30], index=['a', 'b', 'c'])
# 使用items方法获取生成器
for index, value in s.items():
    print(f"Index: {index}, Value: {value}")
108-6-3、结果输出
# 108、pandas.Series.items方法
# Index: a, Value: 10
# Index: b, Value: 20
# Index: c, Value: 30
109、pandas.Series.keys方法
109-1、语法
# 109、pandas.Series.keys方法
pandas.Series.keys()
Return alias for index.

Returns:
Index
Index of the Series.
109-2、参数

        无

109-3、功能

        用于返回pandas.Series对象的索引,它提供了一种方便的方式来获取Series中所有索引标签的列表。

109-4、返回值

        返回一个Index对象,该对象包含了Series的所有索引标签。

109-5、说明

109-5-1、返回类型:keys方法返回一个Index对象,这是pandas中用于存储轴标签的基本对象。Index对象提供了一些有关标签的属性和方法,如tolist()将Index转换为列表。

109-5-2、用途:keys方法适用于需要获取Series索引标签的场景,特别是在处理数据时需要知道索引信息时。

109-5​​​​​​​-3、等效性:keys方法与index属性等效,实际上,调用s.keys()和s.index会得到相同的结果。

109-6、用法
109-6-1、数据准备
109-6-2、代码示例
# 109、pandas.Series.keys方法
import pandas as pd
# 创建一个Series对象
s = pd.Series([10, 20, 30], index=['a', 'b', 'c'])
# 使用keys方法获取索引
index_labels = s.keys()
print(index_labels)
109-6-3、结果输出
# 109、pandas.Series.keys方法
# Index(['a', 'b', 'c'], dtype='object')
110、pandas.Series.pop方法
110-1、语法
# 110、pandas.Series.pop方法
pandas.Series.pop(item)
Return item and drops from series. Raise KeyError if not found.

Parameters:
item
label
Index of the element that needs to be removed.

Returns:
Value that is popped from series.
110-2、参数

110-2-1、item(必须)表示要移除的索引标签。

110-3、功能

        用于从pandas.Series对象中删除一个指定的元素,并返回这个元素的值。

110-4、返回值

        返回被删除项的值,如果指定的标签不存在,则会引发一个KeyError。

110-5、说明

        无

110-6、用法
110-6-1、数据准备
110-6-2、代码示例
# 110、pandas.Series.pop方法
import pandas as pd
# 创建一个Series对象
s = pd.Series([10, 20, 30], index=['a', 'b', 'c'])
# 使用pop方法删除索引为'b'的项
value = s.pop('b')
print(value, end='\n\n')
# 打印修改后的Series对象
print(s)
110-6-3、结果输出
# 110、pandas.Series.pop方法
# 20
#
# a    10
# c    30
# dtype: int64

二、推荐阅读

1、Python筑基之旅
2、Python函数之旅
3、Python算法之旅
4、Python魔法之旅
5、博客个人主页
  • 44
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 17
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

神奇夜光杯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值