pandas.Series#

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

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

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

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

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

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

索引类似数组或索引(1维)

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

dtypestr, numpy.dtype, 或 ExtensionDtype, 可选

输出 Series 的数据类型。如果未指定,这将根据 data 推断。更多用法请参见 用户指南。如果 data 是 Series,则忽略此项。

名字可哈希的,默认无

要给 Series 的名称。

复制bool, 默认为 False

复制输入数据。仅影响 Series 或 1d ndarray 输入。请参见示例。

参见

DataFrame

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

索引

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

备注

更多信息请参考 用户指南

示例

从带有指定索引的字典构造系列

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

注意,索引首先用字典的键构建。之后,序列用给定的索引值重新索引,因此我们得到的结果全是NaN。

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

>>> 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=False 的 1d 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 对原始数据有一个 视图,因此数据也会被改变。

属性

T

返回转置,根据定义这是自身。

数组

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

at

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

attrs

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

返回行轴标签的列表。

dtype

返回底层数据的 dtype 对象。

dtypes

返回底层数据的 dtype 对象。

empty

指示索引是否为空。

标志

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

hasnans

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

iat

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

iloc

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

索引

Series 的索引(轴标签)。

is_monotonic_decreasing

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

is_monotonic_increasing

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

is_unique

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

loc

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

name

返回 Series 的名称。

nbytes

返回底层数据中的字节数。

ndim

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

形状

返回基础数据形状的元组。

大小

返回基础数据中的元素数量。

根据 dtype 返回 Series 作为 ndarray 或类似 ndarray 的对象。

方法

abs()

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

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

返回序列和其他的逐元素相加(二元运算符 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])

返回一个布尔序列,等价于 left <= 序列 <= right。

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

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

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

使用下一个有效观测值来填充 NA/NaN 值。

case_when(caselist)

在条件为真时替换值。

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

在输入阈值处修剪值。

combine(other, func[, fill_value])

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

combine_first(other)

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

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

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

convert_dtypes([infer_objects, ...])

将列从 numpy dtypes 转换为支持 pd.NA 的最佳 dtypes。

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

返回系列和其他的浮点除法,逐元素进行(二元运算符 truediv)。

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

返回系列和其他的浮点除法,逐元素进行(二元运算符 truediv)。

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

返回序列和其他的整数除法和模,逐元素操作(二元运算符 divmod)。

点积(other)

计算 Series 和 other 列之间的点积。

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

返回移除了指定索引标签的系列。

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

返回删除了重复值的系列。

droplevel(level[, axis])

返回删除了请求的索引/列级别的 Series/DataFrame。

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

返回一个新的 Series,其中删除了缺失值。

duplicated([keep])

指示重复的 Series 值。

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

返回序列和其他的元素相等性比较结果,逐元素进行(二元运算符 eq)。

等于(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])

返回序列和其他的整数除法,逐元素操作(二元运算符 floordiv)。

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

返回序列和另一个序列的元素大于或等于的结果,逐元素操作(二进制运算符 ge)。

获取(key[, default])

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

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

使用映射器或通过一系列列来分组系列。

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

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

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

打印一个系列的简要摘要。

插值([method, axis, limit, inplace, ...])

使用插值方法填充 NaN 值。

isin(values)

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

isna()

检测缺失值。

isnull()

Series.isnull 是 Series.isna 的别名。

item()

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

items()

惰性地迭代 (索引, 值) 元组。

()

索引的返回别名。

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

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

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

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

last_valid_index()

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

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

返回序列和小于或等于的其他元素,逐元素比较(二进制运算符 le)。

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

Return Greater than of series and other, element-wise (binary operator lt).

map(arg[, na_action])

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

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

在条件为真时替换值。

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

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

pct_change([periods, fill_method, freq])

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

pipe(func, *args, **kwargs)

应用期望 Series 或 DataFrames 的可链接函数。

pop(item)

返回系列中的物品和掉落物。

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

返回序列和其他元素的指数幂(二元运算符 pow)。

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

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

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

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

quantile([q, interpolation])

返回给定分位数的值。

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

返回序列和其他的元素加法(二元运算符 radd)。

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

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

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

返回序列和其他的浮点数除法,逐元素操作(二元运算符 rtruediv)。

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

返回序列和其他的整数除法和模,逐元素操作(二元运算符 rdivmod)。

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

使用可选的填充逻辑将系列调整为新的索引。

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

返回一个对象,其索引与其他对象匹配。

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

修改系列索引标签或名称。

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

设置索引的轴名称。

reorder_levels(order)

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

repeat(repeats[, axis])

重复一个序列的元素。

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

to_replace 中的值替换为 value

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

重采样时间序列数据。

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

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

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

返回序列和其他的整数除法,逐元素操作(二元运算符 rfloordiv)。

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

返回序列和其他的模数,逐元素操作(二元运算符 rmod)。

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

返回序列和其他的逐元素乘法(二元运算符 rmul)。

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

提供滚动窗口计算。

round([decimals])

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

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

返回序列和其他元素的指数幂(二元运算符 rpow)。

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

返回序列和其他的元素减法(二元运算符 rsub)。

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

返回序列和其他的浮点数除法,逐元素操作(二元运算符 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, ...])

按索引标签排序系列。

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

按值排序。

squeeze([axis])

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

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

返回请求轴上的样本标准偏差。

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

返回序列和其他的元素减法(二元运算符 sub)。

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

返回序列和其他的元素减法(二元运算符 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 转换为 {label -> value} 字典或类似字典的对象。

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

将对象写入 Excel 表格。

to_frame([name])

将系列转换为数据框。

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 表格、长表格或嵌套表格。

to_list()

返回一个值的列表。

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

以 Markdown 友好格式打印系列。

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

将时间戳转换为时间段的*开始*的 DatetimeIndex。

to_xarray()

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

tolist()

返回一个值的列表。

transform(func[, axis])

在自身上调用 func ,生成一个与自身轴形状相同的 Series。

transpose(*args, **kwargs)

返回转置,根据定义这是自身。

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

返回系列和其他的浮点除法,逐元素进行(二元运算符 truediv)。

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

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

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

将 tz-aware 轴转换为目标时区。

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

将 Series 或 DataFrame 的时区未指定索引本地化为目标时区。

unique()

返回 Series 对象的唯一值。

unstack([level, fill_value, sort])

取消堆叠,也称为透视,具有多索引的系列以生成数据框。

更新(其他)

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

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

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

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

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

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

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

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

从 Series/DataFrame 返回横截面。