pandas.Series#

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

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

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

系列(+、-、/、*, * *)根据相关索引值对齐值--它们的长度不必相同。结果索引将是两个索引的排序并集。

参数
data类数组、可迭代、DICT或标量值

包含存储在系列中的数据。如果数据是字典,则参数顺序保持不变。

index类似数组或索引(一维)

值必须是可散列的,并且长度必须与 data 。允许使用非唯一索引值。如果未提供,则默认为RangeIndex(0,1,2,...,n)。如果数据类似于字典并且索引为NONE,则数据中的键被用作索引。如果索引不是None,则使用索引值重新索引生成的Series。

dtype字符串、数字.dtype或ExtensionDtype,可选

输出系列的数据类型。如果未指定,这将从 data 。请参阅 user guide 有更多的用法。

name字符串,可选

要为该系列指定的名称。

copy布尔值,默认为False

复制输入数据。仅影响系列或一维ndarray输入。请参见示例。

注意事项

请参考 User Guide 了解更多信息。

示例

从指定了索引的字典构造序列

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

使用从列表构造级数 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 原始数据,即使 copy=False ,因此数据保持不变。

用一维阵列构造级数 copy=False

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

由于输入数据类型的原因,该系列具有 view 在原始数据上,因此数据也会更改。

属性

T 

返回转置,根据定义,转置是self。

array 

支持此系列或索引的数据的扩展数组。

at 

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

attrs 

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

axes 

返回行轴标签列表。

dtype 

返回底层数据的dtype对象。

dtypes 

返回底层数据的dtype对象。

flags 

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

hasnans 

如果有任何NAN,则返回True。

iat 

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

iloc 

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

index 

系列的索引(轴标签)。

is_monotonic 

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

is_monotonic_decreasing 

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

is_monotonic_increasing 

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

is_unique 

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

loc 

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

name 

返回系列的名称。

nbytes 

返回基础数据中的字节数。

ndim 

根据定义1,基础数据的维度数。

shape 

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

size 

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

values 

根据数据类型将系列返回为ndarray或类似ndarray。

empty

方法:

abs \()

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

add \(其他[, level, fill_value, axis] )

按元素(二元运算符)返回级数和其他的相加 add )。

add_prefix \(前缀)

使用字符串为标签添加前缀 prefix

add_suffix \(后缀)

使用字符串为标签添加后缀 suffix

agg \([func, axis] )

使用指定轴上的一个或多个操作进行聚合。

aggregate \([func, axis] )

使用指定轴上的一个或多个操作进行聚合。

align \(其他[, join, axis, level, copy, ...] )

将两个对象的轴向与指定的联接方法对齐。

all \([axis, bool_only, skipna, level] )

返回是否所有元素都为True,可能是在某个轴上。

any \([axis, bool_only, skipna, level] )

返回是否有任何元素为True,可能是在轴上。

append \(至_追加[, ignore_index, ...] )

(已弃用)串联两个或多个系列。

apply \(函数[, convert_dtype, args] )

对系列的值调用函数。

argmax \([axis, skipna] )

返回序列中最大值的整型位置。

argmin \([axis, skipna] )

返回序列中最小值的int位置。

argsort \([axis, kind, order] )

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

asfreq \(频率 [, method, how, normalize, ...] )

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

asof \(其中 [, subset] )

返回之前没有任何NAN的最后一行 where

astype \(dtype[, copy, errors] )

将Pandas对象强制转换为指定的dtype dtype

at_time \(时间[, asof, axis] )

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

autocorr \([lag] )

计算LAG-N自相关。

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

的同义词 DataFrame.fillna() 使用 method='bfill'

between \(左、右[, inclusive] )

返回等同于Left<=Series<=Right的布尔级数。

between_time \(开始_时间,结束_时间 [, ...] )

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

bfill \([axis, inplace, limit, downcast] )

的同义词 DataFrame.fillna() 使用 method='bfill'

bool \()

返回单个元素Series或DataFrame的布尔值。

cat 

:py:class:`pandas.core.arrays.categorical.CategoricalAccessor`的别名

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

修剪输入阈值上的值。

combine \(其他、函数 [, fill_value] )

根据将系列与系列或标量组合在一起 func

combine_first \(其他)

使用‘Other’中相同位置的值更新空元素。

compare \(其他[, align_axis, keep_shape, ...] )

与其他系列进行比较,并显示不同之处。

convert_dtypes \([infer_objects, ...] )

使用支持的数据类型将列转换为最佳数据类型 pd.NA

copy \([deep] )

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

corr \(其他[, method, min_periods] )

计算相关性与 other 序列,不包括缺失值。

count \([level] )

返回序列中非NA/NULL观测值的数量。

cov \(其他[, min_periods, ddof] )

计算系列协方差,不包括缺失值。

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 \(其他[, level, fill_value, axis] )

返回按元素(二元运算符)的级数和其他的浮点除法 truediv )。

divide \(其他[, level, fill_value, axis] )

返回按元素(二元运算符)的级数和其他的浮点除法 truediv )。

divmod \(其他[, level, fill_value, axis] )

返回级数和其他元素的整数除法和模运算(二元运算符 divmod )。

dot \(其他)

计算级数和其他列之间的点积。

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

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

drop_duplicates \([keep, inplace] )

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

droplevel \(级别[, axis] )

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

dropna \([axis, inplace, how] )

返回删除了缺失值的新系列。

duplicated \([keep] )

表示重复的系列值。

eq \(其他[, level, fill_value, axis] )

按元素(二元运算符)返回级数和其他的等于 eq )。

equals \(其他)

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

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

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

expanding \([min_periods, center, axis, method] )

提供扩展窗口计算。

explode \([ignore_index] )

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

factorize \([sort, na_sentinel] )

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

ffill \([axis, inplace, limit, downcast] )

的同义词 DataFrame.fillna() 使用 method='ffill'

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

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

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

根据指定的索引标签子集数据帧的行或列。

first \(偏移)

根据日期偏移量选择时间序列数据的初始期间。

first_valid_index \()

返回第一个非NA值的索引,如果没有找到非NA值,则返回NONE。

floordiv \(其他[, level, fill_value, axis] )

返回级数和其他元素的整数除法(二元运算符 floordiv )。

ge \(其他[, level, fill_value, axis] )

按元素(二元运算符)返回大于或等于级数和其他 ge )。

get \(密钥[, default] )

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

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

使用映射器或按一系列柱对系列进行分组。

gt \(其他[, level, fill_value, axis] )

按元素(二元运算符)返回系列和其他的大于 gt )。

head \([n] )

返回第一个 n 排好了。

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

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

idxmax \([axis, skipna] )

返回最大值的行标签。

idxmin \([axis, skipna] )

返回最小值的行标签。

infer_objects \()

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

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

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

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

使用插值法填充NaN值。

isin \(值)

系列中的元素是否包含在 values

isna \()

检测缺少的值。

isnull \()

Series.isull是Series.isna的别名。

item \()

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

items \()

懒惰地迭代(索引、值)元组。

iteritems \()

懒惰地迭代(索引、值)元组。

keys \()

返回索引的别名。

kurt \([axis, skipna, level, numeric_only] )

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

kurtosis \([axis, skipna, level, numeric_only] )

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

last \(偏移)

根据日期偏移量选择时间序列数据的最终期间。

last_valid_index \()

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

le \(其他[, level, fill_value, axis] )

按元素(二元运算符)返回小于或等于的级数和其他 le )。

lt \(其他[, level, fill_value, axis] )

按元素(二元运算符)返回小于级数和其他 lt )。

mad \([axis, skipna, level] )

(已弃用)返回值在请求的轴上的平均绝对偏差。

map \(参数[, na_action] )

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

mask \(条件[, other, inplace, axis, level, ...] )

替换条件为True的值。

max \([axis, skipna, level, numeric_only] )

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

mean \([axis, skipna, level, numeric_only] )

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

median \([axis, skipna, level, numeric_only] )

返回值在请求的轴上的中位数。

memory_usage \([index, deep] )

返回该系列的内存使用情况。

min \([axis, skipna, level, numeric_only] )

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

mod \(其他[, level, fill_value, axis] )

级数和其他元素的返回模数(二元运算符 mod )。

mode \([dropna] )

返回系列的模式。

mul \(其他[, level, fill_value, axis] )

级数和其他元素的返回乘法(二元运算符 mul )。

multiply \(其他[, level, fill_value, axis] )

级数和其他元素的返回乘法(二元运算符 mul )。

ne \(其他[, level, fill_value, axis] )

按元素(二元运算符)返回不等于的级数和其他 ne )。

nlargest \([n, keep] )

退回最大的 n 元素。

notna \()

检测现有(非缺失)值。

notnull \()

Series.notull是Series.notna的别名。

nsmallest \([n, keep] )

返回最小的 n 元素。

nunique \([dropna] )

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

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

的同义词 DataFrame.fillna() 使用 method='ffill'

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

当前元素和前一个元素之间的百分比变化。

pipe \(函数,*args, * *kwargs)

应用预期为Series或DataFrame的可链接函数。

plot 

:py:class:`pandas.plotting._core.PlotAccessor`的别名

pop \(项目)

从系列中退货和退货。

pow \(其他[, level, fill_value, axis] )

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

prod \([axis, skipna, level, numeric_only, ...] )

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

product \([axis, skipna, level, numeric_only, ...] )

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

quantile \([q, interpolation] )

返回给定分位数的值。

radd \(其他[, level, fill_value, axis] )

按元素(二元运算符)返回级数和其他的相加 radd )。

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

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

ravel \([order] )

将拼合的基础数据作为ndarray返回。

rdiv \(其他[, level, fill_value, axis] )

返回按元素(二元运算符)的级数和其他的浮点除法 rtruediv )。

rdivmod \(其他[, level, fill_value, axis] )

返回级数和其他元素的整数除法和模运算(二元运算符 rdivmod )。

reindex \(*args, * *kwargs)

通过可选的填充逻辑使系列符合新的索引。

reindex_like \(其他[, method, copy, limit, ...] )

将索引匹配的对象作为其他对象返回。

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

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

rename_axis \([mapper, index, columns, axis, ...] )

设置索引或列的轴的名称。

reorder_levels \(订单)

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

repeat \(重复[, axis] )

重复系列的元素。

replace \([to_replace, value, inplace, limit, ...] )

替换中给出的值 to_replace 使用 value

resample \(规则[, axis, closed, label, ...] )

重新采样时间序列数据。

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

在重置索引的情况下生成新的DataFrame或Series。

rfloordiv \(其他[, level, fill_value, axis] )

返回级数和其他元素的整数除法(二元运算符 rfloordiv )。

rmod \(其他[, level, fill_value, axis] )

级数和其他元素的返回模数(二元运算符 rmod )。

rmul \(其他[, level, fill_value, axis] )

级数和其他元素的返回乘法(二元运算符 rmul )。

rolling \(窗口[, min_periods, center, ...] )

提供滚动窗口计算。

round \([decimals] )

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

rpow \(其他[, level, fill_value, axis] )

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

rsub \(其他[, level, fill_value, axis] )

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

rtruediv \(其他[, level, fill_value, axis] )

返回按元素(二元运算符)的级数和其他的浮点除法 rtruediv )。

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

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

searchsorted \(值[, side, sorter] )

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

sem \([axis, skipna, level, ddof, numeric_only] )

返回请求轴上平均值的无偏标准误差。

set_axis \(标签[, axis, inplace] )

将所需的索引指定给给定轴。

set_flags \(*[, copy, allows_duplicate_labels] )

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

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

使用可选时间按所需期间数移位索引 freq

skew \([axis, skipna, level, numeric_only] )

返回请求的轴上的无偏斜。

slice_shift \([periods, axis] )

(已弃用)等同于 shift 而不复制数据。

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

按索引标签对系列进行排序。

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

按值排序。

sparse 

:py:class:`pandas.core.arrays.sparse.accessor.SparseAccessor`的别名

squeeze \([axis] )

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

std \([axis, skipna, level, ddof, numeric_only] )

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

str 

:py:class:`pandas.core.strings.accessor.StringMethods`的别名

sub \(其他[, level, fill_value, axis] )

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

subtract \(其他[, level, fill_value, axis] )

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

sum \([axis, skipna, level, numeric_only, ...] )

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

swapaxes \(axis1、axis2[, copy] )

适当地互换轴和交换值轴。

swaplevel \([i, j, copy] )

中交换级别i和j。 MultiIndex

tail \([n] )

退还最后一张 n 排好了。

take \(索引[, axis, is_copy] )

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

to_clipboard \([excel, sep] )

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

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

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

to_dict \([into] )

将系列转换为{标签->值}词典或类似词典的对象。

to_excel \(EXCEL_编写器[, sheet_name, na_rep, ...] )

将对象写入Excel工作表。

to_frame \([name] )

将系列转换为DataFrame。

to_hdf \(路径_或_buf,键[, mode, complevel, ...] )

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

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

将对象转换为JSON字符串。

to_latex \([buf, columns, col_space, header, ...] )

将对象呈现到LaTeX表格、长表或嵌套表。

to_list \()

返回值列表。

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

以支持降价的格式打印系列。

to_numpy \([dtype, copy, na_value] )

表示此系列或索引中的值的NumPy ndarray。

to_period \([freq, copy] )

将系列从DatetimeIndex转换为PerodIndex。

to_pickle \(路径[, compression, protocol, ...] )

Pickle(序列化)对象到文件。

to_sql \(名称,控制 [, schema, if_exists, ...] )

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

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

呈现序列的字符串表示形式。

to_timestamp \([freq, how, copy] )

强制转换为时间戳的DatetimeIndex,位于 起头 属于那个时期。

to_xarray \()

从Pandas对象返回一个XARRAY对象。

tolist \()

返回值列表。

transform \(函数[, axis] )

打电话 func 关于自己产生与自己具有相同轴形的级数。

transpose \(*args, * *kwargs)

返回转置,根据定义,转置是self。

truediv \(其他[, level, fill_value, axis] )

返回按元素(二元运算符)的级数和其他的浮点除法 truediv )。

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

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

tshift \([periods, freq, axis] )

(已弃用)移动时间索引,使用索引的频率(如果可用)。

tz_convert \(TZ[, axis, level, copy] )

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

tz_localize \(TZ[, axis, level, copy, ...] )

将Series或DataFrame的Tz-naive索引本地化到目标时区。

unique \()

返回Series对象的唯一值。

unstack \([level, fill_value] )

取消堆叠,也称为Pivot,与多索引串联以生成DataFrame。

update \(其他)

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

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

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

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

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

view \([dtype] )

创建系列的新视图。

where \(条件[, other, inplace, axis, level, ...] )

替换条件为FALSE的值。

xs \(密钥[, axis, level, drop_level] )

从系列/数据帧返回横截面。

dt