pandas.Series#

class pandas.Series(data=None, index=None, dtype=None, name=None, copy=None, fastpath=_NoDefault.no_default)[源代码]#

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

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

Series 之间的运算(+, -, /, , *)会根据它们关联的索引值对齐值——这两个 Series 的长度不必相同。结果的索引将是两个索引的已排序并集。

Parameters:
data类似数组(array-like)、可迭代对象(Iterable)、字典(dict)或标量值

包含存储在 Series 中的数据。如果 data 是一个字典,参数的顺序将被保留。

index类似数组(array-like)或索引(Index)(一维)

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

dtypestr, numpy.dtype, or ExtensionDtype, optional

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

name可哈希,默认为 None

要为 Series 指定的名称。

copybool,默认 False

复制输入数据。仅影响 Series 或一维 ndarray 输入。请参阅示例。

Notes

有关更多信息,请参阅 User Guide

Examples

从字典构造 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

字典的键与索引值匹配,因此索引值没有影响。

>>> 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

请注意,索引首先使用字典中的键进行构建。之后,Series 将使用给定的索引值重新索引,因此我们得到全部是 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,因此数据也会被更改。

Attributes

T

返回转置,其定义就是自身。

array

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

at

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

attrs

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

axes

返回行轴标签的列表。

dtype

返回底层数据的 dtype 对象。

dtypes

返回底层数据的 dtype 对象。

empty

指示 Series/DataFrame 是否为空。

flags

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

hasnans

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

iat

按整数位置的行/列对访问单个值。

iloc

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

index

Series 的索引(轴标签)。

is_monotonic_decreasing

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

is_monotonic_increasing

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

is_unique

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

loc

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

name

返回 Series 的名称。

nbytes

返回底层数据的字节数。

ndim

底层数据的维度数,默认为 1。

shape

返回底层数据形状的元组。

size

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

values

根据 dtype 不同,将 Series 返回为 ndarray 或类似 ndarray的对象。

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[, convert_dtype, 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 dtype

at_time (time[, asof, axis])

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

autocorr ([lag])

计算滞后 N 的自相关。

backfill (*[, axis, inplace, limit, downcast])

(已弃用) 使用下一个有效观测值填充 NaN/NA 值以填补间隙。

between (left, right[, inclusive])

返回布尔 Series,等效于 left <= series <= right。

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

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

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

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

bool ()

(已弃用) 返回单个元素的 Series 或 DataFrame 的布尔值。

case_when (caselist)

替换条件为 True 的值。

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

在输入阈值处截断值。

combine (other, func[, fill_value])

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

combine_first (other)

用 'other' 中相同位置的值更新空元素。

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

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

convert_dtypes ([infer_objects, ...])

使用支持 pd.NA 的 dtype 将列转换为最佳可能的 dtype。

copy ([deep])

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

corr (other[, method, min_periods])

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

count ()

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

cov (other[, min_periods, ddof])

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

cummax ([axis, skipna])

返回 DataFrame 或 Series 轴上的累积最大值。

cummin ([axis, skipna])

返回 DataFrame 或 Series 轴上的累积最小值。

cumprod ([axis, skipna])

返回 DataFrame 或 Series 轴上的累积乘积。

cumsum ([axis, skipna])

返回 DataFrame 或 Series 轴上的累积和。

describe ([percentiles, include, exclude])

生成描述性统计信息。

diff ([periods])

元素的离散差分。

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, axis, method])

提供扩展窗口计算。

explode ([ignore_index])

将类似列表的每个元素转换为一行。

factorize ([sort, use_na_sentinel])

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

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

使用最后一个有效观测值传播NA/NaN值。

fillna ([value, method, axis, inplace, ...])

使用指定的方法填充NA/NaN值。

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

根据指定的索引标签对DataFrame的行或列进行子集选择。

first (offset)

(已弃用) 根据日期偏移量选择时间序列数据的初始时段。

first_valid_index ()

返回第一个非NA值的索引,如果找不到非NA值,则返回None。

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

返回序列与另一序列的整数除法,逐元素计算(二元运算符 floordiv)。

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

返回序列与另一序列的“大于或等于”比较,逐元素计算(二元运算符 ge)。

get (key[, default])

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

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

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

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

返回序列与另一序列的“大于”比较,逐元素计算(二元运算符 gt)。

head ([n])

返回前`n`行。

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

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

idxmax ([axis, skipna])

返回最大值的行标签。

idxmin ([axis, skipna])

返回最小值的行标签。

infer_objects ([copy])

尝试为对象列推断更好的数据类型(dtypes)。

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 (offset)

(已弃用) 根据日期偏移量选择时间序列数据的最后时段。

last_valid_index ()

返回最后一个非NA值的索引,如果找不到非NA值,则返回None。

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

返回序列与另一序列的“小于或等于”比较,逐元素计算(二元运算符 le)。

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

返回序列与另一序列的“小于”比较,逐元素计算(二元运算符 lt)。

map (arg[, na_action])

根据输入映射或函数映射 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])

返回序列与另一序列的模运算,逐元素计算(二元运算符 mod)。

mode ([dropna])

返回 Series 的众数。

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

返回序列与另一序列的乘法,逐元素计算(二元运算符 mul)。

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

返回序列与另一序列的乘法,逐元素计算(二元运算符 mul)。

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

返回序列与另一序列的“不等于”比较,逐元素计算(二元运算符 ne)。

nlargest ([n, keep])

返回最大的 n 个元素。

notna ()

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

notnull ()

Series.notnull 是 Series.notna 的别名。

nsmallest ([n, keep])

返回最小的 n 个元素。

nunique ([dropna])

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

pad (*[axis, inplace, limit, downcast])

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

pct_change ([periods, fill_method, limit, freq])

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

pipe (func, *args, **kwargs)

应用可链式调用的函数,这些函数期望 Series 或 DataFrame。

pop (item)

从Series中返回项并删除。

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

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

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

返回请求轴上值的乘积。

product ([axis, skipna, numeric_only, min_count])

返回请求轴上值的乘积。

quantile ([q, interpolation])

在给定的分位数处返回值。

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

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

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

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

ravel ([order])

(已弃用) 以ndarray或ExtensionArray的形式返回展平的底层数据。

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, ...])

返回具有与 other 对象匹配的索引的对象。

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, limit, ...])

value 替换 to_replace 中给定的值。

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

重采样时间序列数据。

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 与其他元素的减法,逐元素执行(二进制运算符 sub)。

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

返回 Series 与其他元素的减法,逐元素执行(二进制运算符 sub)。

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

返回所请求轴上的值的总和。

swapaxes (axis1, axis2[, copy])

(已弃用)交换轴并相应地交换值轴。

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 转换为 {标签 -> 值} 的 dict 或类 dict 对象。

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])

将 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])

在周期*开始*时强制转换为 Timestamp 的 DatetimeIndex。

to_xarray ()

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

tolist ()

返回值的列表。

transform (func[, axis])

在 self 上调用 func,生成一个与 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])

取消堆叠,也称为透视,将具有 MultiIndex 的 Series 转换为 DataFrame。

update (other)

使用传入 Series 中的值就地修改 Series。

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

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

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

返回所请求轴上无偏方差。

view ([dtype])

(已弃用) 创建 Series 的新视图。

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

替换条件为False的值。

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

从Series/DataFrame中返回横截面。