pandas.Series#

class pandas.Series(data=None, index=None, dtype=None, name=None, copy=None)[source]#

带轴标签的一维 ndarray(包括时间序列)。

标签不必是唯一的,但必须是可哈希的类型。该对象支持整数和基于标签的索引,并提供了大量用于执行涉及索引的操作的方法。ndarray 的统计方法已被覆盖,以便自动排除缺失数据(当前表示为 NaN)。

Series 之间的操作(+、-、/、*、**)根据其关联的索引值对齐值——它们不必具有相同的长度。结果索引将是两个索引的排序并集。

参数:
data类数组、可迭代对象、字典或标量值

包含存储在 Series 中的数据。如果 data 是 dict,则会保留参数顺序。不支持无序集合。

index类数组或 Index(一维)

值必须是可哈希的,并且与 data 的长度相同。允许非唯一的索引值。如果未提供,则默认为 RangeIndex (0, 1, 2, …, n)。如果 data 是类 dict 且 index 为 None,则 data 中的键将用作索引。如果 index 不是 None,则生成的 Series 将使用 index 值进行重新索引。

dtypestr、numpy.dtype 或 ExtensionDtype,可选

输出 Series 的数据类型。如果未指定,将从 data 推断。有关更多用法,请参阅 用户指南

name可哈希,默认为 None

要分配给 Series 的名称。

copybool,默认为 None

是否复制输入数据,仅对数组、Series 和 Index 输入相关(对于其他输入,例如列表,总是会创建一个新数组)。对于数组输入,默认为 True,对于 Index/Series,默认为 False。即使对于 Index/Series 为 False,也会创建数据的浅拷贝。如果知道输入数据不会在其他地方修改,则将其设置为 False 以避免复制数组输入(风险自负)。设置为 True 以强制立即复制 Series/Index 输入。

另请参阅

DataFrame

二维的、大小可变的、可能异构的表格数据。

索引

用于索引和对齐的不可变序列。

注意

请参考 用户指南 获取更多信息。

示例

从具有指定 Index 的字典构造 Series

>>> d = {"a": 1, "b": 2, "c": 3}
>>> ser = pd.Series(data=d, index=["a", "b", "c"])
>>> ser
a   1
b   2
c   3
dtype: int64

字典的键与 Index 值匹配,因此 Index 值无效。

>>> d = {"a": 1, "b": 2, "c": 3}
>>> ser = pd.Series(data=d, index=["x", "y", "z"])
>>> ser
x   NaN
y   NaN
z   NaN
dtype: float64

请注意,Index 首先使用字典中的键进行构建。之后,Series 使用给定的 Index 值进行重新索引,因此我们得到的结果是全部 NaN。

使用 copy=False 从列表构造 Series。

>>> r = [1, 2]
>>> ser = pd.Series(r, copy=False)
>>> ser.iloc[0] = 999
>>> r
[1, 2]
>>> ser
0    999
1      2
dtype: int64

由于输入数据类型,即使 copy=False,Series 仍然拥有原始数据的 copy,因此数据保持不变。

使用 copy=False 从一维 ndarray 构造 Series。

>>> r = np.array([1, 2])
>>> ser = pd.Series(r, copy=False)
>>> ser.iloc[0] = 999
>>> r
array([999,   2])
>>> ser
0    999
1      2
dtype: int64

由于输入数据类型,Series 拥有原始数据的 view,因此数据也会被更改。

属性

T

返回转置,按定义是自身。

array

支持此 Series 或 Index 的数据的 ExtensionArray。

at

通过行/列标签对访问单个值。

attrs

此数据集的全局属性字典。

axes

返回行轴标签的列表。

dtype

返回底层数据的 dtype 对象。

数据类型

返回底层数据的 dtype 对象。

empty

指示 Index 是否为空。

flags

获取与此 pandas 对象相关的属性。

hasnans

如果存在任何 NaN,则返回 True。

iat

通过整数位置通过行/列对访问单个值。

iloc

纯粹基于整数位置的索引,用于按位置选择。

index

Series 的索引(轴标签)。

is_monotonic_decreasing

如果对象中的值单调递减,则返回 True。

is_monotonic_increasing

如果对象中的值是单调递增的,则返回 True。

is_unique

如果对象中的值是唯一的,则返回 True。

loc

通过标签或布尔数组访问一组行和列。

name

返回 Series 的名称。

nbytes

返回底层数据的字节数。

ndim

底层数据的维度数,定义为 1。

shape

返回底层数据的 shape 元组。

size

返回底层数据的元素数量。

values

根据 dtype 返回 Series 作为 ndarray 或 ndarray-like。

Methods

abs()

返回一个 Series/DataFrame,其中包含每个元素的绝对数值。

add(other[, level, fill_value, axis])

返回 Series 与 other 的相加结果,逐元素进行 (二元运算符 add)。

add_prefix(prefix[, axis])

为标签添加前缀字符串prefix

add_suffix(suffix[, axis])

为标签添加后缀字符串suffix

agg([func, axis])

沿指定轴对一个或多个操作进行聚合。

aggregate([func, axis])

沿指定轴对一个或多个操作进行聚合。

align(other[, join, axis, level, copy, ...])

使用指定的连接方法将两个对象按其轴对齐。

all(*[, axis, bool_only, skipna])

返回所有元素是否为 True,可能沿轴。

any(*[, axis, bool_only, skipna])

返回任何元素是否为 True,可能沿轴。

apply(func[, args, by_row])

在 Series 的值上调用函数。

argmax([axis, skipna])

返回 Series 中最大值的整数位置。

argmin([axis, skipna])

返回 Series 中最小值的整数位置。

argsort([axis, kind, order, stable])

返回对 Series 值进行排序的整数索引。

asfreq(freq[, method, how, normalize, ...])

将时间序列转换为指定频率。

asof(where[, subset])

返回where之前最后一个没有 NaN 的行(或行)。

astype(dtype[, copy, errors])

将 pandas 对象强制转换为指定的数据类型 dtype

at_time(time[, asof, axis])

选择一天中特定时间的值(例如,上午 9:30)。

autocorr([lag])

计算滞后 N 的自相关。

between(left, right[, inclusive])

返回一个布尔 Series,等价于 left <= series <= right。

between_time(start_time, end_time[, ...])

选择一天中特定时间段内的值(例如,上午 9:00-9:30)。

bfill(*[, axis, inplace, limit, limit_area])

使用下一个有效观测值填充 NA/NaN 值以填补空白。

case_when(caselist)

在条件为 True 的地方替换值。

clip([lower, upper, axis, inplace])

在输入阈值处修剪值。

combine(other, func[, fill_value])

根据 func 将 Series 与 Series 或标量组合。

combine_first(other)

用“other”中相同位置的值更新 null 元素。

compare(other[, align_axis, keep_shape, ...])

与另一个 Series 进行比较并显示差异。

convert_dtypes([infer_objects, ...])

将列从 numpy 数据类型转换为支持 pd.NA 的最佳数据类型。

copy([deep])

复制此对象的索引和数据。

corr(other[, method, min_periods])

计算与 other Series 的相关性,排除缺失值。

count()

返回 Series 中非 NA/null 观测的数量。

cov(other[, min_periods, ddof])

计算与 Series 的协方差,排除缺失值。

cummax([axis, skipna])

返回 Series 的累积最大值。

cummin([axis, skipna])

返回 Series 上的累积最小值。

cumprod([axis, skipna])

返回 Series 的累积乘积。

cumsum([axis, skipna])

返回 Series 的累积和。

describe([percentiles, include, exclude])

生成描述性统计信息。

diff([periods])

Series 元素的离散差分。

div(other[, level, fill_value, axis])

返回 Series 与 other 的浮点数除法,逐元素进行(二元运算符 truediv)。

divide(other[, level, fill_value, axis])

返回 Series 与 other 的浮点数除法,逐元素进行(二元运算符 truediv)。

divmod(other[, level, fill_value, axis])

返回 Series 和 other 的整除和模,逐元素(二元运算符 divmod)。

dot(other)

计算 Series 和 other 列的点积。

drop([labels, axis, index, columns, level, ...])

返回移除指定索引标签的 Series。

drop_duplicates(*[, keep, inplace, ignore_index])

返回移除重复值的 Series。

droplevel(level[, axis])

返回删除指定索引/列级别的 Series/DataFrame。

dropna(*[, axis, inplace, how, ignore_index])

返回一个移除了缺失值的新 Series。

duplicated([keep])

指示 Series 中的重复值。

eq(other[, level, fill_value, axis])

返回 Series 和 other 的相等比较,逐元素(二元运算符 eq)。

equals(other)

测试两个对象是否包含相同元素。

ewm([com, span, halflife, alpha, ...])

提供指数加权 (EW) 计算。

expanding([min_periods, method])

提供扩展窗口计算。

explode([ignore_index])

将列表类元素转换为行。

factorize([sort, use_na_sentinel])

将对象编码为枚举类型或分类变量。

ffill(*[, axis, inplace, limit, limit_area])

通过传播最后一个有效观测值来填充 NA/NaN 值。

fillna(value, *[, axis, inplace, limit])

value填充 NA/NaN 值。

filter([items, like, regex, axis])

根据指定的索引标签子集化 DataFrame 或 Series。

first_valid_index()

返回第一个非缺失值的索引,如果没有找到值,则返回 None。

floordiv(other[, level, fill_value, axis])

返回 Series 和 other 的整除,逐元素(二元运算符 floordiv)。

from_arrow(data)

从 Arrow 对象(类数组)构造 Series。

ge(other[, level, fill_value, axis])

返回 Series 和 other 的大于或等于比较,逐元素(二元运算符 ge)。

get(key[, default])

获取对象中给定键(例如 DataFrame 列)的项。

groupby([by, level, as_index, sort, ...])

使用映射器或列Series对Series进行分组。

gt(other[, level, fill_value, axis])

返回 Series 和 other 的大于比较,逐元素(二元运算符 gt)。

head([n])

返回前 n 行。

hist([by, ax, grid, xlabelsize, xrot, ...])

使用 matplotlib 绘制输入 Series 的直方图。

idxmax([axis, skipna])

返回最大值的行标签。

idxmin([axis, skipna])

返回最小值行标签。

infer_objects([copy])

尝试为 object 列推断更合适的数据类型。

info([verbose, buf, max_cols, memory_usage, ...])

打印 Series 的简洁摘要。

interpolate([method, axis, limit, inplace, ...])

使用插值方法填充 NaN 值。

isin(values)

Series 中的元素是否包含在 values 中。

isna()

检测缺失值。

isnull()

Series.isnull 是 Series.isna 的别名。

item()

将底层数据的第一个元素作为 Python 标量返回。

items()

惰性地迭代 (index, value) 元组。

keys()

返回 index 的别名。

kurt(*[, axis, skipna, numeric_only])

返回所请求轴上的无偏峰度。

kurtosis(*[, axis, skipna, numeric_only])

返回所请求轴上的无偏峰度。

last_valid_index()

返回最后一个非缺失值的索引,如果没有找到值,则返回 None。

le(other[, level, fill_value, axis])

返回 series 和 other 的小于或等于(元素级)(二进制运算符 le)。

lt(other[, level, fill_value, axis])

返回 Series 和 other 的小于比较,逐元素(二元运算符 lt)。

map([func, na_action, engine])

根据输入映射或函数映射 Series 的值。

mask(cond[, other, inplace, axis, level])

替换条件为 True 的值。

max(*[, axis, skipna, numeric_only])

返回所请求轴上值的最大值。

mean(*[, axis, skipna, numeric_only])

返回所请求轴上值的平均值。

median(*[, axis, skipna, numeric_only])

返回所请求轴上值的中间值。

memory_usage([index, deep])

返回 Series 的内存使用情况。

min(*[, axis, skipna, numeric_only])

返回所请求轴上值的最小值。

mod(other[, level, fill_value, axis])

返回 Series 和 other 的模,逐元素(二元运算符 mod)。

mode([dropna])

返回 Series 的众数。

mul(other[, level, fill_value, axis])

返回 Series 和 other 的乘法,逐元素(二元运算符 mul)。

multiply(other[, level, fill_value, axis])

返回 Series 和 other 的乘法,逐元素(二元运算符 mul)。

ne(other[, level, fill_value, axis])

返回 Series 和 other 的不等于比较,逐元素(二元运算符 ne)。

nlargest([n, keep])

返回最大的 n 个元素。

notna()

检测存在的(非缺失)值。

notnull()

Series.notnull 是 Series.notna 的别名。

nsmallest([n, keep])

返回最小的 n 个元素。

nunique([dropna])

返回对象中唯一元素的数量。

pct_change([periods, fill_method, freq])

当前元素与先前元素之间的分数变化。

pipe(func, *args, **kwargs)

应用期望 Series 或 DataFrame 的链式函数。

pop(item)

返回 item 并从 Series 中删除。

pow(other[, level, fill_value, axis])

返回 Series 和 other 的指数幂,逐元素(二元运算符 pow)。

prod(*[, axis, skipna, numeric_only, min_count])

返回所请求轴上值的乘积。

product(*[, axis, skipna, numeric_only, ...])

返回所请求轴上值的乘积。

quantile([q, interpolation])

返回给定分位数的值。

radd(other[, level, fill_value, axis])

返回 Series 和 other 的加法,逐元素(二元运算符 radd)。

rank([axis, method, numeric_only, ...])

沿轴计算数值数据排名(1 到 n)。

rdiv(other[, level, fill_value, axis])

返回 Series 和 other 的浮点除法,逐元素(二元运算符 rtruediv)。

rdivmod(other[, level, fill_value, axis])

返回 Series 和 other 的整除和模,逐元素(二元运算符 rdivmod)。

reindex([index, axis, method, copy, level, ...])

使用可选的填充逻辑来统一 Series 的新索引。

reindex_like(other[, method, copy, limit, ...])

返回一个具有与另一个对象匹配的索引的对象。

rename([index, axis, copy, inplace, level, ...])

更改 Series 索引标签或名称。

rename_axis([mapper, index, axis, copy, inplace])

为索引设置轴的名称。

reorder_levels(order)

使用输入顺序重新排列索引级别。

repeat(repeats[, axis])

重复 Series 的元素。

replace([to_replace, value, inplace, regex])

value替换to_replace中给定的值。

resample(rule[, closed, label, convention, ...])

对时间序列数据进行重采样。

reset_index([level, drop, name, inplace, ...])

生成一个索引重置后的新 DataFrame 或 Series。

rfloordiv(other[, level, fill_value, axis])

返回 Series 和 other 的整除,逐元素(二元运算符 rfloordiv)。

rmod(other[, level, fill_value, axis])

返回 Series 和 other 的模,逐元素进行计算(二元运算符 rmod)。

rmul(other[, level, fill_value, axis])

返回 Series 和 other 的乘法,逐元素(二元运算符 rmul)。

rolling(window[, min_periods, center, ...])

提供滚动窗口计算。

round([decimals])

将 Series 中的每个值四舍五入到指定的小数位数。

rpow(other[, level, fill_value, axis])

返回 Series 和 other 的指数幂,逐元素(二元运算符 rpow)。

rsub(other[, level, fill_value, axis])

返回 Series 与 other 的元素级减法 (二进制操作符 rsub )。

rtruediv(other[, level, fill_value, axis])

返回 Series 和 other 的浮点除法,逐元素(二元运算符 rtruediv)。

sample([n, frac, replace, weights, ...])

从对象的轴返回随机样本项。

searchsorted(value[, side, sorter])

查找元素应插入以保持顺序的索引。

sem(*[, axis, skipna, ddof, numeric_only])

返回请求轴上未偏倚的均值标准误差。

set_axis(labels, *[, axis, copy])

(已弃用) 为给定轴设置所需的索引。

set_flags(*[, copy, allows_duplicate_labels])

返回一个带有更新标志的新对象。

shift([periods, freq, axis, fill_value, suffix])

按所需的周期数移动索引,并可选择指定时间freq

skew(*[, axis, skipna, numeric_only])

返回请求轴上未偏倚的偏度。

sort_index(*[, axis, level, ascending, ...])

按索引标签对 Series 进行排序。

sort_values(*[, axis, ascending, inplace, ...])

按值排序。

squeeze([axis])

将一维轴对象压缩成标量。

std(*[, axis, skipna, ddof, numeric_only])

返回样本标准差。

sub(other[, level, fill_value, axis])

返回 Series 和 other 的减法,逐元素(二元运算符 sub)。

subtract(other[, level, fill_value, axis])

返回 Series 和 other 的减法,逐元素(二元运算符 sub)。

sum(*[, axis, skipna, numeric_only, min_count])

返回请求轴上的值之和。

swaplevel([i, j, copy])

交换 MultiIndex 的级别 i 和 j。

tail([n])

返回最后 n 行。

take(indices[, axis])

沿轴返回给定位置索引中的元素。

to_clipboard(*[, excel, sep])

将对象复制到系统剪贴板。

to_csv([path_or_buf, sep, na_rep, ...])

将对象写入逗号分隔值 (csv) 文件。

to_dict(*[, into])

将 Series 转换为 {标签 -> 值} 字典或类字典对象。

to_excel(excel_writer, *[, sheet_name, ...])

将对象写入 Excel 工作表。

to_frame([name])

将 Series 转换为 DataFrame。

to_hdf(path_or_buf, *, key[, mode, ...])

使用 HDFStore 将包含的数据写入 HDF5 文件。

to_json([path_or_buf, orient, date_format, ...])

将对象转换为 JSON 字符串。

to_latex([buf, columns, header, index, ...])

将对象渲染为 LaTeX tabular、longtable 或嵌套表。

to_list()

返回值的列表。

to_markdown([buf, mode, index, storage_options])

以 Markdown 友好的格式打印 Series。

to_numpy([dtype, copy, na_value])

Series 或 Index 中值的 NumPy ndarray 表示形式。

to_period([freq, copy])

将 Series 从 DatetimeIndex 转换为 PeriodIndex。

to_pickle(path, *[, compression, protocol, ...])

将对象 pickle(序列化)到文件。

to_sql(name, con, *[, schema, if_exists, ...])

将存储在 DataFrame 中的记录写入 SQL 数据库。

to_string([buf, na_rep, float_format, ...])

渲染 Series 的字符串表示形式。

to_timestamp([freq, how, copy])

在周期的 *开始* 处转换为 Timestamps 的 DatetimeIndex。

to_xarray()

从 pandas 对象返回一个 xarray 对象。

tolist()

返回值的列表。

transform(func[, axis])

调用 func 作用于 self,生成一个与 self 具有相同轴形状的 Series。

transpose(*args, **kwargs)

返回转置,按定义是自身。

truediv(other[, level, fill_value, axis])

返回 Series 与 other 的浮点数除法,逐元素进行(二元运算符 truediv)。

truncate([before, after, axis, copy])

截断 Series 或 DataFrame 在某个索引值之前和之后的部分。

tz_convert(tz[, axis, level, copy])

将时区感知的轴转换为目标时区。

tz_localize(tz[, axis, level, copy, ...])

将 Series 或 DataFrame 的时区感知索引本地化到目标时区。

unique()

返回 Series 对象的唯一值。

unstack([level, fill_value, sort])

Unstack(也称为 pivot)Series,其中 MultiIndex 用于生成 DataFrame。

update(other)

使用传入 Series 的值修改 Series(原地)。

value_counts([normalize, sort, ascending, ...])

返回一个包含唯一值计数的 Series。

var(*[, axis, skipna, ddof, numeric_only])

返回请求轴上的未偏倚方差。

where(cond[, other, inplace, axis, level])

替换条件为 False 的值。

xs(key[, axis, level, drop_level])

从 Series/DataFrame 返回横截面。