目录
203、pandas.Series.reindex_like方法
205、pandas.Series.rename_axis方法
一、用法精讲
201、pandas.Series.last方法
201-1、语法
# 201、pandas.Series.last方法
pandas.Series.last(offset)
Select final periods of time series data based on a date offset.
Deprecated since version 2.1: last() is deprecated and will be removed in a future version. Please create a mask and filter using .loc instead.
For a DataFrame with a sorted DatetimeIndex, this function selects the last few rows based on a date offset.
Parameters:
offset
str, DateOffset, dateutil.relativedelta
The offset length of the data that will be selected. For instance, ‘3D’ will display all the rows having their index within the last 3 days.
Returns:
Series or DataFrame
A subset of the caller.
Raises:
TypeError
If the index is not a DatetimeIndex
201-2、参数
201-2-1、offset(必须):一个表示时间偏移的字符串或DataOffset对象,例如'1D'(一天)、'2W'(两周)、'1M'(一个月),该参数定义了要从时间序列的末尾向前回溯的时间范围。
201-3、功能
用于返回一个Series中最后一部分的数据,基于给定的时间偏移量(offset),该方法通常用于时间序列数据的操作。
201-4、返回值
返回符合偏移量条件的时间序列数据的最后部分。
201-5、说明
无
201-6、用法
201-6-1、数据准备
无
201-6-2、代码示例
# 201、pandas.Series.last方法
import pandas as pd
# 创建一个时间序列
dates = pd.date_range(start='2024-07-29', periods=10, freq='W')
data = pd.Series(range(10), index=dates)
# 获取最后两周的数据
result = data.last('2W')
print(result)
201-6-3、结果输出
# 201、pandas.Series.last方法
print(result)
# 2024-09-29 8
# 2024-10-06 9
# Freq: W-SUN, dtype: int64
202、pandas.Series.reindex方法
202-1、语法
# 202、pandas.Series.reindex方法
pandas.Series.reindex(index=None, *, axis=None, method=None, copy=None, level=None, fill_value=None, limit=None, tolerance=None)
Conform Series to new index with optional filling logic.
Places NA/NaN in locations having no value in the previous index. A new object is produced unless the new index is equivalent to the current one and copy=False.
Parameters:
indexarray-like, optional
New labels for the index. Preferably an Index object to avoid duplicating data.
axisint or str, optional
Unused.
method{None, ‘backfill’/’bfill’, ‘pad’/’ffill’, ‘nearest’}
Method to use for filling holes in reindexed DataFrame. Please note: this is only applicable to DataFrames/Series with a monotonically increasing/decreasing index.
None (default): don’t fill gaps
pad / ffill: Propagate last valid observation forward to next valid.
backfill / bfill: Use next valid observation to fill gap.
nearest: Use nearest valid observations to fill gap.
copybool, default True
Return a new object, even if the passed indexes are the same.
Note
The copy keyword will change behavior in pandas 3.0. Copy-on-Write will be enabled by default, which means that all methods with a copy keyword will use a lazy copy mechanism to defer the copy and ignore the copy keyword. The copy keyword will be removed in a future version of pandas.
You can already get the future behavior and improvements through enabling copy on write pd.options.mode.copy_on_write = True
levelint or name
Broadcast across a level, matching Index values on the passed MultiIndex level.
fill_valuescalar, default np.nan
Value to use for missing values. Defaults to NaN, but can be any “compatible” value.
limitint, default None
Maximum number of consecutive elements to forward or backward fill.
toleranceoptional
Maximum distance between original and new labels for inexact matches. The values of the index at the matching locations most satisfy the equation abs(index[indexer] - target) <= tolerance.
Tolerance may be a scalar value, which applies the same tolerance to all values, or list-like, which applies variable tolerance per element. List-like includes list, tuple, array, Series, and must be the same size as the index and its dtype must exactly match the index’s type.
Returns:
Series with changed index.
202-2、参数
202-3-1、index(可选,默认值为None):指定新的索引,如果设为None,则不改变索引。
202-3-2、axis(可选,默认值为None):在Series中通常不需要设置此参数,因为Series只有一个轴(轴0),设置为0或'index'是等效的。
202-3-3、method(可选,默认值为None):用于填充缺失值的方法,如果指定了method
,则会用该方法填补重新索引后缺失的值。pad
或ffill
是前向填充;backfill
或 bfill
是后向填充。
202-3-4、copy(可选,默认值为None):是否返回副本,通常不需要设置,除非你明确需要控制是否复制数据。
202-3-5、level(可选,默认值为None):如果使用多级索引(MultiIndex),则用于指定级别进行重新索引。
202-3-6、fill_value(可选,默认值为None):用于填充新索引中缺失的值,如果设置了method
,则fill_value
会被忽略。
202-3-7、limit(可选,默认值为None):指定填充的最大数量,即限制填充操作影响的最大数量。
202-3-8、tolerance(可选,默认值为None):在使用method
时,指定容忍的范围,用于匹配填充的值,用于填充时限制允许的误差范围。
202-3、功能
对Series对象进行重新索引,并根据指定的参数调整数据,它可以用来调整索引的顺序,添加新的索引或者填充缺失的数据。
202-4、返回值
返回一个新的Series对象,其索引是指定的新的索引,该Series对象的数据会根据新的索引进行调整,缺失的值会根据参数method
和fill_value
进行填充。
202-5、说明
无
202-6、用法
202-6-1、数据准备
无
202-6-2、代码示例
# 202、pandas.Series.reindex方法
import pandas as pd
s = pd.Series([1, 2, 3], index=['a', 'b', 'c'])
# 重新索引并使用前向填充
s_reindexed = s.reindex(['a', 'b', 'c', 'd', 'e'], method='ffill')
print(s_reindexed)
202-6-3、结果输出
# 202、pandas.Series.reindex方法
# a 1
# b 2
# c 3
# d 3
# e 3
# dtype: int64
203、pandas.Series.reindex_like方法
203-1、语法
# 203、pandas.Series.reindex_like方法
pandas.Series.reindex_like(other, method=None, copy=None, limit=None, tolerance=None)
Return an object with matching indices as other object.
Conform the object to the same index on all axes. Optional filling logic, placing NaN in locations having no value in the previous index. A new object is produced unless the new index is equivalent to the current one and copy=False.
Parameters:
otherObject of the same data type
Its row and column indices are used to define the new indices of this object.
method{None, ‘backfill’/’bfill’, ‘pad’/’ffill’, ‘nearest’}
Method to use for filling holes in reindexed DataFrame. Please note: this is only applicable to DataFrames/Series with a monotonically increasing/decreasing index.
None (default): don’t fill gaps
pad / ffill: propagate last valid observation forward to next valid
backfill / bfill: use next valid observation to fill gap
nearest: use nearest valid observations to fill gap.
copybool, default True
Return a new object, even if the passed indexes are the same.
Note
The copy keyword will change behavior in pandas 3.0. Copy-on-Write will be enabled by default, which means that all methods with a copy keyword will use a lazy copy mechanism to defer the copy and ignore the copy keyword. The copy keyword will be removed in a future version of pandas.
You can already get the future behavior and improvements through enabling copy on write pd.options.mode.copy_on_write = True
limitint, default None
Maximum number of consecutive labels to fill for inexact matches.
toleranceoptional
Maximum distance between original and new labels for inexact matches. The values of the index at the matching locations must satisfy the equation abs(index[indexer] - target) <= tolerance.
Tolerance may be a scalar value, which applies the same tolerance to all values, or list-like, which applies variable tolerance per element. List-like includes list, tuple, array, Series, and must be the same size as the index and its dtype must exactly match the index’s type.
Returns:
Series or DataFrame
Same type as caller, but with changed indices on each axis.
203-2、参数
203-2-1、other(必须):表示要用来重新索引当前Series
的目标对象,Series
或DataFrame
的索引会用来重新索引当前的Series
。
203-2-2、method(可选,默认值为None):用于填充缺失值的方法,常见的选项包括'ffill'(前向填充)和'bfill'(后向填充),如果设置为None
,则不进行填充。
203-2-3、copy(可选,默认值为None):是否返回原数据的副本,如果设置为True,则会返回数据的副本;如果为False,则会尝试在原地修改数据。默认为None,通常会根据other的索引和数据是否需要复制来自动决定。
203-2-4、limit(可选,默认值为None):在填充时最大填充的数量。如果填充方法为'ffill'或'bfill',这个参数控制填充的最大数量,默认情况下不限制填充数量。
203-2-5、tolerance(可选,默认值为None):在填充时允许的容差范围,通常与时间序列相关,用于在时间索引中填充近似值,默认值为None,表示没有容差限制。
203-3、功能
用于根据另一个Series
或DataFrame
对象的索引来重新索引当前Series
的方法。
203-4、返回值
返回一个新的Series
,其索引与目标对象的索引相同,原始Series
的数据会被重新排列以符合目标对象的索引。
203-5、说明
无
203-6、用法
203-6-1、数据准备
无
203-6-2、代码示例
# 203、pandas.Series.reindex_like方法
import pandas as pd
# 创建两个Series
s1 = pd.Series([1, 2, 3], index=['a', 'b', 'c'])
s2 = pd.Series([10, 20, 30], index=['a', 'd', 'e'])
# 使用s2的索引来重新索引s1
s1_reindexed = s1.reindex_like(s2, method='ffill')
print(s1_reindexed)
203-6-3、结果输出
# 203、pandas.Series.reindex_like方法
# a 1
# d 3
# e 3
# dtype: int64
204、pandas.Series.rename方法
204-1、语法
# 204、pandas.Series.rename方法
pandas.Series.rename(index=None, *, axis=None, copy=None, inplace=False, level=None, errors='ignore')
Alter Series index labels or name.
Function / dict values must be unique (1-to-1). Labels not contained in a dict / Series will be left as-is. Extra labels listed don’t throw an error.
Alternatively, change Series.name with a scalar value.
See the user guide for more.
Parameters:
indexscalar, hashable sequence, dict-like or function optional
Functions or dict-like are transformations to apply to the index. Scalar or hashable sequence-like will alter the Series.name attribute.
axis{0 or ‘index’}
Unused. Parameter needed for compatibility with DataFrame.
copybool, default True
Also copy underlying data.
Note
The copy keyword will change behavior in pandas 3.0. Copy-on-Write will be enabled by default, which means that all methods with a copy keyword will use a lazy copy mechanism to defer the copy and ignore the copy keyword. The copy keyword will be removed in a future version of pandas.
You can already get the future behavior and improvements through enabling copy on write pd.options.mode.copy_on_write = True
inplacebool, default False
Whether to return a new Series. If True the value of copy is ignored.
levelint or level name, default None
In case of MultiIndex, only rename labels in the specified level.
errors{‘ignore’, ‘raise’}, default ‘ignore’
If ‘raise’, raise KeyError when a dict-like mapper or index contains labels that are not present in the index being transformed. If ‘ignore’, existing keys will be renamed and extra keys will be ignored.
Returns:
Series or None
Series with index labels or name altered or None if inplace=True.
204-2、参数
204-2-1、index(可选,默认值为None):字典或函数,指定要重命名的索引映射,可以传递一个字典,其中键是旧的索引值,值是新的索引值;也可以传递一个函数,应用于每个索引值。
204-2-2、axis(可选,默认值为None):对于Series对象,axis参数没有实际作用,因为Series只有一个轴。
204-2-3、copy(可选,默认值为None):布尔值,是否复制Series对象。默认值为True
,表示创建Series的副本;如果设置为False,则不创建副本,而是在原Series上进行修改。
204-2-4、inplace(可选,默认值为False):布尔值,是否在原地修改Series,如果设置为True,则直接在原Series上进行修改,不会返回新的Series对象,默认值为False,即返回一个新的Series对象。
204-2-5、level(可选,默认值为None):用于具有多级索引(MultiIndex)的Series,指定要重命名的索引级别,如果Series只有一个级别的索引,这个参数不需要设置。
204-2-6、errors(可选,默认值为'ignore'):处理索引中不存在的标签时的行为,默认为'ignore',表示如果索引标签不存在则忽略它们;如果设置为'raise',则会抛出错误,提示标签不存在。
204-3、功能
用于重命名Series对象的索引,并返回一个新的Series对象,除非inplace参数被设置为True。
204-4、返回值
默认情况下,rename方法返回一个新的Series对象,其中索引已经根据提供的映射或函数进行了重命名。如果inplace=True,则直接在原Series上进行修改,不返回新的Series对象。
204-5、说明
无
204-6、用法
204-6-1、数据准备
无
204-6-2、代码示例
# 204、pandas.Series.rename方法
import pandas as pd
# 创建一个Series对象
s = pd.Series([10, 20, 30], index=['a', 'b', 'c'])
# 使用字典重命名索引
s_renamed = s.rename(index={'a': 'alpha', 'b': 'beta'})
# 输出重命名后的Series
print("重命名后的Series:")
print(s_renamed)
# 使用函数重命名索引
s_renamed_func = s.rename(index=lambda x: x.upper())
# 输出重命名后的Series
print("\n使用函数重命名后的Series:")
print(s_renamed_func)
# 在原地重命名索引
s.rename(index={'a': 'alpha', 'b': 'beta'}, inplace=True)
# 输出原Series
print("\n原地重命名后的Series:")
print(s)
204-6-3、结果输出
# 204、pandas.Series.rename方法
# 重命名后的Series:
# alpha 10
# beta 20
# c 30
# dtype: int64
#
# 使用函数重命名后的Series:
# A 10
# B 20
# C 30
# dtype: int64
#
# 原地重命名后的Series:
# alpha 10
# beta 20
# c 30
# dtype: int64
205、pandas.Series.rename_axis方法
205-1、语法
# 205、pandas.Series.rename_axis方法
pandas.Series.rename_axis(mapper=_NoDefault.no_default, *, index=_NoDefault.no_default, axis=0, copy=True, inplace=False)
Set the name of the axis for the index or columns.
Parameters:
mapperscalar, list-like, optional
Value to set the axis name attribute.
index, columnsscalar, list-like, dict-like or function, optional
A scalar, list-like, dict-like or functions transformations to apply to that axis’ values. Note that the columns parameter is not allowed if the object is a Series. This parameter only apply for DataFrame type objects.
Use either mapper and axis to specify the axis to target with mapper, or index and/or columns.
axis{0 or ‘index’, 1 or ‘columns’}, default 0
The axis to rename. For Series this parameter is unused and defaults to 0.
copybool, default None
Also copy underlying data.
Note
The copy keyword will change behavior in pandas 3.0. Copy-on-Write will be enabled by default, which means that all methods with a copy keyword will use a lazy copy mechanism to defer the copy and ignore the copy keyword. The copy keyword will be removed in a future version of pandas.
You can already get the future behavior and improvements through enabling copy on write pd.options.mode.copy_on_write = True
inplacebool, default False
Modifies the object directly, instead of creating a new Series or DataFrame.
Returns:
Series, DataFrame, or None
The same type as the caller or None if inplace=True.
205-2、参数
205-2-1、mapper(可选):用于重新命名轴标签的字典或函数,如果提供的是字典,将现有标签映射到新标签;如果提供的是函数,将现有标签传递给函数,并将函数返回值用作新标签。
205-2-2、index(可选):类似于mapper,但仅用于重命名索引(即行标签),提供字典或函数来重命名索引。如果mapper已指定且index未指定,则使用mapper的值。
205-2-3、axis(可选,默认值为0):要重命名的轴,对于Series,唯一有效的值是0或'index',因为Series只有一个轴,即索引。
205-2-4、copy(可选,默认值为True):如果为True,则返回一个副本,并且数据始终复制;如果为False,则尝试避免复制数据,但不能保证不会复制。
205-2-5、inplace(可选,默认值为False):如果为True,则直接在原对象上进行修改,并返回None;否则,返回一个新的Series对象。
205-3、功能
通过提供一个映射(字典)或一个函数,用户可以对Series
的索引标签进行重命名,该方法可以帮助在数据分析和操作过程中对索引标签进行更改,使其更加符合实际需求或更加易于理解。
205-4、返回值
205-4-1、返回一个新的Series
对象:如果inplace=False(默认值),该方法返回一个新的Series对象,其中包含已重命名的索引标签。
205-4-2、原地修改Series
对象:如果inplace=True,该方法在原Series对象上进行修改,并返回None,原始对象的索引标签会被直接修改。
205-5、说明
无
205-6、用法
205-6-1、数据准备
无
205-6-2、代码示例
# 205、pandas.Series.rename_axis方法
import pandas as pd
s = pd.Series(["dog", "cat", "monkey"])
s.rename_axis("animal")
print(s)
205-6-3、结果输出
# 205、pandas.Series.rename_axis方法
# 0 dog
# 1 cat
# 2 monkey
# dtype: object