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

2024-07-21 1123阅读

目录

一、用法精讲

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、博客个人主页

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

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

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

一、用法精讲

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、单个标签
# 
# 105-2、多个标签
# 
# 105-3、标签切片
# 
# 105-4、布尔索引
# 

二、推荐阅读

1、Python筑基之旅
2、Python函数之旅
3、Python算法之旅
4、Python魔法之旅
5、博客个人主页
VPS购买请点击我

免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们,邮箱:ciyunidc@ciyunshuju.com。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

目录[+]