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

目录

一、用法精讲

101、pandas.Series.__array__魔法方法

101-1、语法

101-2、参数

101-3、功能

101-4、返回值

101-5、说明

101-6、用法

101-6-1、数据准备

101-6-2、代码示例

101-6-3、结果输出

102、pandas.Series.get方法

102-1、语法

102-2、参数

102-3、功能

102-4、返回值

102-5、说明

102-6、用法

102-6-1、数据准备

102-6-2、代码示例

102-6-3、结果输出

103、pandas.Series.at方法

103-1、语法

103-2、参数

103-3、功能

103-4、返回值

103-5、说明

103-6、用法

103-6-1、数据准备

103-6-2、代码示例

103-6-3、结果输出

104、pandas.Series.iat方法

104-1、语法

104-2、参数

104-3、功能

104-4、返回值

104-5、说明

104-6、用法

104-6-1、数据准备

104-6-2、代码示例

104-6-3、结果输出

105、pandas.Series.loc方法

105-1、语法

105-2、参数

105-3、功能

105-4、返回值

105-5、说明

105-6、用法

105-6-1、数据准备

105-6-2、代码示例

105-6-3、结果输出

二、推荐阅读

1、Python筑基之旅

2、Python函数之旅

3、Python算法之旅

4、Python魔法之旅

5、博客个人主页

一、用法精讲

101、pandas.Series.__array__魔法方法
101-1、语法
# 101、pandas.Series.__array__魔法方法
pandas.Series.__array__(dtype=None, copy=None)
Return the values as a NumPy array.

Users should not call this directly. Rather, it is invoked by numpy.array() and numpy.asarray().

Parameters:
dtype
str or numpy.dtype, optional
The dtype to use for the resulting NumPy array. By default, the dtype is inferred from the data.

copy
bool or None, optional
Unused.

Returns:
numpy.ndarray
The values in the series converted to a numpy.ndarray with the specified dtype.
101-2、参数

101-2-1、dtype(可选,默认值为None)指定返回数组的数据类型,如果不提供,返回的数组将使用Series中数据的原始数据类型。

101-2-2、copy(可选,默认值为None)指定是否返回数组的副本,虽然在__array__方法中通常不会使用该参数,实际行为仍然可能依赖于实现和数据的具体情况。

101-3、功能

        用于将Series对象转换为NumPy数组。

101-4、返回值

        返回一个NumPy数组,其中包含Series对象中的所有数据元素。

101-5、说明

        虽然__array__方法可以用来将Series对象转换为NumPy数组,to_numpy()方法更为常用且功能更为完善。

101-6、用法
101-6-1、数据准备
101-6-2、代码示例
# 101、pandas.Series.__array__魔法方法
import pandas as pd
# 创建一个pandas Series对象
data = pd.Series([1, 2, 3, 4, 5])
# 使用 __array__ 方法将 Series 转换为 NumPy 数组
data_array = data.__array__()
# 输出结果
print("使用__array__方法转换后的NumPy数组:")
print(data_array)
# 如果需要指定数据类型
data_array_float = data.__array__(dtype='float64')
# 输出指定数据类型后的结果
print("\n指定数据类型(float64)后的NumPy数组:")
print(data_array_float)
101-6-3、结果输出
# 101、pandas.Series.__array__魔法方法
# 使用__array__方法转换后的NumPy数组:
# [1 2 3 4 5]
#
# 指定数据类型(float64)后的NumPy数组:
# [1. 2. 3. 4. 5.]
102、pandas.Series.get方法
102-1、语法
# 102、pandas.Series.get方法
pandas.Series.get(key, default=None)
Get item from object for given key (ex: DataFrame column).

Returns default value if not found.

Parameters:
key
object
Returns:
same type as items contained in object
102-2、参数

102-2-1、key(必须)指定要获取的索引标签,如果key是一个列表,则会返回这些标签对应的值组成的Series对象;如果key是一个单独的标签,返回该标签对应的单一值。

102-2-2、default(可选,默认值为None)如果指定的key不存在于Series中,则返回的默认值;如果未提供该参数,Series.get方法在key不存在时将返回None。

102-3、功能

        用于从Series对象中获取指定索引位置的值,如果指定的索引不存在,该方法允许你设置一个默认值来代替引发KeyError的异常。

102-4、返回值

102-4-1、如果key是单个标签,返回对应的值或default值/None

102-4-2、如果key是标签列表,返回一个新的Series,其中包含标签对应的值和default值/None

102-5、说明

        无

102-6、用法
102-6-1、数据准备
102-6-2、代码示例
# 102、pandas.Series.get方法
import pandas as pd
# 创建一个pandas Series对象
data = pd.Series([10, 20, 30], index=['a', 'b', 'c'])
# 使用 get 方法获取存在的索引
value_a = data.get('a')
print("索引'a'的值:", value_a)
# 使用 get 方法获取不存在的索引,提供默认值
value_d = data.get('d', default='默认值')
print("索引'd'的值:", value_d)
# 使用 get 方法获取不存在的索引,不提供默认值
value_e = data.get('e')
print("索引'e'的值:", value_e)
102-6-3、结果输出
# 102、pandas.Series.get方法
# 索引'a'的值: 10
# 索引'd'的值: 默认值
# 索引'e'的值: None
103、pandas.Series.at方法
103-1、语法
# 103、pandas.Series.at方法
pandas.Series.at
Access a single value for a row/column label pair.

Similar to loc, in that both provide label-based lookups. Use at if you only need to get or set a single value in a DataFrame or Series.

Raises:
KeyError
If getting a value and ‘label’ does not exist in a DataFrame or Series.

ValueError
If row/column label pair is not a tuple or if any label from the pair is not a scalar for DataFrame. If label is list-like (excluding NamedTuple) for Series.
103-2、参数

        无

103-3、功能

        用于快速访问Series中单个元素的方法。

103-4、返回值

        返回值是Series中指定标签的单个元素的值。当你使用Series.at[label]时,它会返回对应标签的值,类型与Series中元素的类型一致;如果你使用Series.at[label]=value来设置值,它会返回None,因为它是一个就地修改操作,不返回新的Series。

103-5、说明

103-5-1、效率:at方法专门用于访问或设置单个元素,效率高于loc,如果你只需要访问或修改单个元素,at是更优的选择。

103-5-2、标签检查:at不会提供默认值,如果你尝试访问一个不存在的标签,将引发KeyError异常。

103-6、用法
103-6-1、数据准备
103-6-2、代码示例
# 103、pandas.Series.at方法
# 103-1、单个标签
import pandas as pd
data = pd.Series([10, 20, 30], index=['a', 'b', 'c'])
# 获取标签'a'对应的值
value = data.at['a']
print(value, end='\n\n')
# 103-2、单个标签赋值
import pandas as pd
data = pd.Series([10, 20, 30], index=['a', 'b', 'c'])
# 设置标签'b'对应的值
data.at['b'] = 25
print(data)
103-6-3、结果输出
# 103、pandas.Series.at方法
# 103-1、单个标签
# 10

# 103-2、单个标签赋值
# a    10
# b    25
# c    30
# dtype: int64
104、pandas.Series.iat方法
104-1、语法
# 104、pandas.Series.iat方法
pandas.Series.iat
Access a single value for a row/column pair by integer position.

Similar to iloc, in that both provide integer-based lookups. Use iat if you only need to get or set a single value in a DataFrame or Series.

Raises:
IndexError
When integer position is out of bounds.
104-2、参数

        无

104-3、功能

        用于基于整数位置来访问或修改Series元素的方法,它与Series.at方法不同,后者是基于标签进行操作的,而iat是基于位置的。

104-4、返回值

104-4-1、Series.iat[index]返回指定整数位置的单个元素的值。

104-4-2、Series.iat[index]=value修改指定整数位置的值,并且不会返回新的Series,而是在原地修改。

104-5、说明

104-5-1、iat只能用于整数位置索引,不支持标签。

104-5-2、访问或修改的索引位置超出Series长度会引发IndexError。

104-6、用法
104-6-1、数据准备
104-6-2、代码示例
# 104、pandas.Series.iat方法
# 104-1、读取单个值
import pandas as pd
data = pd.Series([10, 20, 30], index=['a', 'b', 'c'])
value = data.iat[1]
print(value)

# 104-2、设置单个值
import pandas as pd
data = pd.Series([10, 20, 30], index=['a', 'b', 'c'])
data.iat[1] = 25
print(data.iat[1])
104-6-3、结果输出
# 104、pandas.Series.iat方法
# 104-1、读取单个值
# 20

# 104-2、设置单个值
# 25
105、pandas.Series.loc方法
105-1、语法
# 105、pandas.Series.loc方法
pandas.Series.loc
Access a group of rows and columns by label(s) or a boolean array.

.loc[] is primarily label based, but may also be used with a boolean array.

Allowed inputs are:

A single label, e.g. 5 or 'a', (note that 5 is interpreted as a label of the index, and never as an integer position along the index).

A list or array of labels, e.g. ['a', 'b', 'c'].

A slice object with labels, e.g. 'a':'f'.

Warning

Note that contrary to usual python slices, both the start and the stop are included

A boolean array of the same length as the axis being sliced, e.g. [True, False, True].

An alignable boolean Series. The index of the key will be aligned before masking.

An alignable Index. The Index of the returned selection will be the input.

A callable function with one argument (the calling Series or DataFrame) and that returns valid output for indexing (one of the above)

See more at Selection by Label.

Raises:
KeyError
If any items are not found.

IndexingError
If an indexed key is passed and its index is unalignable to the frame index.
105-2、参数

        无

105-3、功能

        用于基于标签访问和操作Series元素的方法。它与Series.iat不同,后者是基于位置的,loc方法允许你通过标签来选择和操作数据,非常适合需要通过索引标签来访问数据的场景。

105-4、返回值

105-4-1、单个标签:返回标量值。

105-4-2、多个标签:返回Series。

105-4-3、标签切片:返回Series。

105-4-4、布尔索引:返回Series。

105-5、说明

        Series.loc的用法

105-5-1访问单个值:Series.loc[label]返回与标签label相关的Series中的值。

105-5-2、访问多个值:Series.loc[label1,label2]返回与多个标签相关的Series中的值。

105-5-3、切片:Series.loc[start_label:end_label]返回从start_label到end_label范围内的Series数据。

105-5-4、布尔索引: Series.loc[boolean_mask]返回符合布尔条件的Series中的值。

105-6、用法
105-6-1、数据准备
105-6-2、代码示例
# 105、pandas.Series.loc方法
# 105-1、单个标签
import pandas as pd
data = pd.Series([10, 20, 30], index=['a', 'b', 'c'])
value = data.loc['b']
print(type(value))

# 105-2、多个标签
import pandas as pd
data = pd.Series([10, 20, 30], index=['a', 'b', 'c'])
values = data.loc[['a', 'c']]
print(type(values))

# 105-3、标签切片
import pandas as pd
data = pd.Series([10, 20, 30], index=['a', 'b', 'c'])
sliced_data = data.loc['a':'b']
print(type(sliced_data))

# 105-4、布尔索引
import pandas as pd
data = pd.Series([10, 20, 30], index=['a', 'b', 'c'])
boolean_mask = data > 15
filtered_data = data.loc[boolean_mask]
print(type(filtered_data))
105-6-3、结果输出
# 105、pandas.Series.loc方法
# 105-1、单个标签
# <class 'numpy.int64'>

# 105-2、多个标签
# <class 'pandas.core.series.Series'>

# 105-3、标签切片
# <class 'pandas.core.series.Series'>

# 105-4、布尔索引
# <class 'pandas.core.series.Series'>

二、推荐阅读

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

神奇夜光杯

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

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

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

打赏作者

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

抵扣说明:

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

余额充值