DataFrame#

构造函数#

DataFrame ([data, index, columns, dtype, copy])

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

属性和底层数据#

DataFrame.index

DataFrame 的索引(行标签)。

DataFrame.columns

DataFrame 的列标签。

DataFrame.dtypes

返回 DataFrame 中的 dtypes。

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

打印DataFrame的简洁摘要。

DataFrame.select_dtypes ([include, exclude])

根据列的数据类型返回 DataFrame 列的子集。

DataFrame.values

返回 DataFrame 的 Numpy 表示。

DataFrame.axes

返回表示 DataFrame 轴的列表。

DataFrame.ndim

返回一个整数,表示轴/数组的维度数。

DataFrame.size

返回一个整数,表示此对象中的元素数量。

DataFrame.shape

返回一个元组,表示 DataFrame 的维度。

DataFrame.memory_usage ([index, deep])

返回每列的内存使用量(以字节为单位)。

DataFrame.empty

指示 Series/DataFrame 是否为空。

DataFrame.set_flags (*[, copy, ...])

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

转换#

DataFrame.astype (dtype[, copy, errors])

将 pandas 对象转换为指定的 dtype dtype

DataFrame.convert_dtypes ([infer_objects, ...])

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

DataFrame.infer_objects ([copy])

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

DataFrame.copy ([deep])

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

DataFrame.bool ()

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

DataFrame.to_numpy ([dtype, copy, na_value])

将 DataFrame 转换为 NumPy 数组。

索引,迭代#

DataFrame.head ([n])

返回前`n`行。

DataFrame.at

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

DataFrame.iat

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

DataFrame.loc

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

DataFrame.iloc

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

DataFrame.insert (loc, column, value[, ...])

在指定位置将列插入DataFrame。

DataFrame.__iter__ ()

遍历info轴。

DataFrame.items ()

迭代(列名,Series)对。

DataFrame.keys ()

获取“info axis”(参见索引章节更多内容)。

DataFrame.iterrows ()

迭代DataFrame行作为(索引,Series)对。

DataFrame.itertuples ([index, name])

将DataFrame行迭代为命名元组。

DataFrame.pop (item)

返回指定项并从 DataFrame 中删除。

DataFrame.tail ([n])

返回最后 n 行。

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

从Series/DataFrame中返回横截面。

DataFrame.get (key[, default])

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

DataFrame.isin (values)

DataFrame中的每个元素是否包含在values中。

DataFrame.where (cond[, other, inplace, ...])

替换条件为False的值。

DataFrame.mask (cond[, other, inplace, axis, ...])

在条件为 True 的位置替换值。

DataFrame.query (expr, *[, inplace])

使用布尔表达式查询 DataFrame 列。

有关 .at, .iat, .loc.iloc 的详细信息,请参阅 indexing documentation

二元运算符函数#

DataFrame.__add__ (other)

获取DataFrame与另一个对象的逐列加法。

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

获取 DataFrame 和 other 的逐元素加法(二元运算符 add)。

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

DataFrame 与其他项的减法,逐元素(二元运算符 sub)。

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

获取 DataFrame 和 other 的乘积,逐个元素计算(二进制运算符 mul)。

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

对 DataFrame 和 other 进行浮点除法,逐元素进行(二元运算符 truediv)。

DataFrame.truediv (other[, axis, level, ...])

对 DataFrame 和 other 进行浮点除法,逐元素进行(二元运算符 truediv)。

DataFrame.floordiv (other[, axis, level, ...])

对DataFrame和其他值进行整除,逐元素进行(二元运算符 floordiv)。

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

获取 DataFrame 和 other 的模(remainder),逐个元素计算(二进制运算符 mod)。

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

获取 DataFrame 和 other 的指数幂,逐个元素计算(二进制运算符 pow)。

DataFrame.dot (other)

计算 DataFrame 和 other 之间的矩阵乘积。

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

获取 DataFrame 和其他元素的加法,逐元素进行(二元运算符 radd)。

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

获取 DataFrame 和其他元素的减法,逐元素进行(二元运算符 rsub)。

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

获取 DataFrame 和其他元素的乘法,逐元素进行(二元运算符 rmul)。

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

获取 DataFrame 和其他元素的浮点数除法,逐元素进行(二元运算符 rtruediv)。

DataFrame.rtruediv (other[, axis, level, ...])

获取 DataFrame 和其他元素的浮点数除法,逐元素进行(二元运算符 rtruediv)。

DataFrame.rfloordiv (other[, axis, level, ...])

获取 DataFrame 和其他元素的整数除法,逐元素进行(二元运算符 rfloordiv)。

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

获取 DataFrame 和其他元素的模运算,逐元素进行(二元运算符 rmod)。

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

获取 DataFrame 和其他元素的指数幂,逐元素进行(二元运算符 rpow)。

DataFrame.lt (other[, axis, level])

获取 DataFrame 和 other 小于的元素(二进制运算符 lt)。

DataFrame.gt (other[, axis, level])

对DataFrame和其他值进行大于比较,逐元素进行(二元运算符 gt)。

DataFrame.le (other[, axis, level])

获取 DataFrame 和 other 小于或等于的元素(二进制运算符 le)。

DataFrame.ge (other[, axis, level])

对DataFrame和其他值进行大于或等于比较,逐元素进行(二元运算符 ge)。

DataFrame.ne (other[, axis, level])

获取 DataFrame 和 other 不等于的元素(二进制运算符 ne)。

DataFrame.eq (other[, axis, level])

获取 DataFrame 和 other 的相等值,逐元素进行(二元运算符 eq)。

DataFrame.combine (other, func[, fill_value, ...])

将另一 DataFrame 进行逐列合并。

DataFrame.combine_first (other)

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

函数应用、GroupBy 和窗口#

DataFrame.apply (func[, axis, raw, ...])

沿 DataFrame 的轴应用函数。

DataFrame.map (func[, na_action])

将函数逐个元素应用到 DataFrame。

DataFrame.applymap (func[, na_action])

(已弃用) 逐元素应用函数到 DataFrame。

DataFrame.pipe (func, *args, **kwargs)

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

DataFrame.agg ([func, axis])

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

DataFrame.aggregate ([func, axis])

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

DataFrame.transform (func[, axis])

在self上调用``func``,生成一个与self具有相同轴形状的DataFrame。

DataFrame.groupby ([by, axis, level, ...])

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

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

提供滚动窗口计算。

DataFrame.expanding ([min_periods, axis, method])

提供扩展窗口计算。

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

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

计算 / 描述性统计#

DataFrame.abs

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

DataFrame.all (axisbool_onlyskipna

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

DataFrame.any (axisbool_onlyskipna

返回是否有任何元素是 True,可能沿轴进行。

DataFrame.clip (lowerupperaxisinplace

在输入阈值处截断值。

DataFrame.corr (methodmin_periods, ...)

计算列的成对相关性,排除 NA/null 值。

DataFrame.corrwith (otheraxisdrop, ...)

计算成对相关性。

DataFrame.count (axisnumeric_only

计算每列或每行的非 NA 单元格数。

DataFrame.cov (min_periodsddofnumeric_only

计算列的成对协方差,排除 NA/null 值。

DataFrame.cummax (axisskipna

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

DataFrame.cummin (axisskipna

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

DataFrame.cumprod (axisskipna

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

DataFrame.cumsum (axisskipna

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

DataFrame.describe (percentilesinclude, ...)

生成描述性统计信息。

DataFrame.diff (periodsaxis

元素的离散差分。

DataFrame.eval (expr, inplace)

计算描述 DataFrame 列操作的字符串。

DataFrame.kurt (axisskipnanumeric_only

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

DataFrame.kurtosis (axisskipnanumeric_only

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

DataFrame.max (axisskipnanumeric_only

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

DataFrame.mean (axisskipnanumeric_only

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

DataFrame.median (axisskipnanumeric_only

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

DataFrame.min (axisskipnanumeric_only

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

DataFrame.mode (axisnumeric_onlydropna

获取所选轴上每个元素的众数。

DataFrame.pct_change (periodsfill_method, ...)

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

DataFrame.prod (axisskipnanumeric_only, ...)

返回请求轴上值的乘积。

DataFrame.product (axisskipna, ...)

返回请求轴上值的乘积。

DataFrame.quantile (qaxisnumeric_only, ...)

在指定轴上指定分位数的返回值。

DataFrame.rank (axismethodnumeric_only, ...)

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

DataFrame.round (decimals

将 DataFrame 四舍五入到可变小数位数。

DataFrame.sem (axisskipnaddofnumeric_only

在指定轴上返回无偏标准误差。

DataFrame.skew (axisskipnanumeric_only

在指定轴上返回无偏偏度。

DataFrame.sum (axisskipnanumeric_only, ...)

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

DataFrame.std (axisskipnaddofnumeric_only

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

DataFrame.var (axisskipnaddofnumeric_only

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

DataFrame.nunique (axisdropna

计算指定轴上不同元素的数量。

DataFrame.value_counts (subsetnormalize, ...)

返回一个Series,其中包含DataFrame中每个不同行的频率。

重索引 / 选择 / 标签操作#

DataFrame.add_prefix (prefixaxis

在标签前添加字符串 prefix

DataFrame.add_suffix (suffixaxis

在标签后添加字符串 suffix

DataFrame.align (otherjoinaxislevel, ...)

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

DataFrame.at_time (timeasofaxis

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

DataFrame.between_time (start_timeend_time

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

DataFrame.drop (labelsaxisindex, ...)

删除行或列中指定的标签。

DataFrame.drop_duplicates (subsetkeep, ...)

返回删除重复行后的 DataFrame。

DataFrame.duplicated (subsetkeep

返回表示重复行的布尔 Series。

DataFrame.equals (other

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

DataFrame.filter (itemslikeregexaxis

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

DataFrame.first (offset

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

DataFrame.head ([n])

返回前`n`行。

DataFrame.idxmax (axisskipnanumeric_only

返回请求轴上最大值的第一个出现索引。

DataFrame.idxmin (axis, skipna, numeric_only)

返回请求轴上最小值第一个出现的索引。

DataFrame.last (offset)

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

DataFrame.reindex (labels, index, columns, ...)

根据可选的填充逻辑调整 DataFrame 的索引。

DataFrame.reindex_like (other [ , method, ... ])

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

DataFrame.rename (mapper, index, columns, ...)

重命名列或索引标签。

DataFrame.rename_axis (mapper, index, ...)

设置索引或列的轴名称。

DataFrame.reset_index (level, drop, ...)

重置索引或其某个级别。

DataFrame.sample (n, frac, replace, ...)

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

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

为指定轴分配所需的索引。

DataFrame.set_index (keys, * [ , drop, append, ... ])

使用现有列设置 DataFrame 索引。

DataFrame.tail ([n])

返回最后 n 行。

DataFrame.take (indices [ , axis ])

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

DataFrame.truncate (before, after, axis, copy)

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

缺失数据处理#

DataFrame.backfill (* [ , axis, inplace, ... ])

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

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

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

DataFrame.dropna (* [ , axis, how, thresh, ... ])

删除缺失值。

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

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

DataFrame.fillna (value, method, axis, ...)

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

DataFrame.interpolate (method, axis, limit, ...)

使用插值方法填充NaN值。

DataFrame.isna ()

检测缺失值。

DataFrame.isnull ()

DataFrame.isnull 是 DataFrame.isna 的别名。

DataFrame.notna ()

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

DataFrame.notnull ()

DataFrame.notnullDataFrame.notna 的别名。

DataFrame.pad (* [ , axis, inplace, limit, ... ])

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

DataFrame.replace (to_replace, value, ...)

value 替换 to_replace 中给定的值。

重塑、排序、转置#

DataFrame.droplevel (level [ , axis ])

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

DataFrame.pivot (* , columns [ , index, values ])

返回由给定的索引/列值组织的重塑后的 DataFrame。

DataFrame.pivot_table (values, index, ...)

创建电子表格风格的透视表作为 DataFrame。

DataFrame.reorder_levels (order [ , axis ])

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

DataFrame.sort_values (by, * [ , axis, ... ])

沿任一轴按值进行排序。

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

按标签(沿轴)对对象进行排序。

DataFrame.nlargest (n, columns [ , keep ])

返回按 columns 降序排序的前 n 行。

DataFrame.nsmallest (n, columns [ , keep ])

返回按 columns 升序排序的前 n 行。

DataFrame.swaplevel (i, j, axis)

交换 MultiIndex 中的级别 i 和 j。

DataFrame.stack (level, dropna, sort, ...)

将指定级别的列堆叠到索引。

DataFrame.unstack (level, fill_value, sort)

对(必然是分层的)索引标签的某个级别进行“逆透视”。

DataFrame.swapaxes (axis1, axis2 [ , copy ])

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

DataFrame.melt (id_vars, value_vars, ...)

将 DataFrame 从宽格式重塑为长格式,并可选择保留标识符。

DataFrame.explode (column [ , ignore_index ])

将类列表的每个元素转换为一行,并复制索引值。

DataFrame.squeeze (axis)

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

DataFrame.to_xarray ()

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

DataFrame.T

DataFrame 的转置。

DataFrame.transpose (*args [ , copy ])

转置索引和列。

组合 / 比较 / 连接 / 合并#

DataFrame.assign (**kwargs)

为 DataFrame 分配新列。

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

比较另一个 DataFrame 并显示差异。

DataFrame.join (other [ , on, how, lsuffix, ... ])

连接另一个DataFrame的列。

DataFrame.merge (right [ , how, on, left_on, ... ])

将 DataFrame 或命名 Series 对象与数据库风格的连接(join)进行合并。

DataFrame.update (other [ , join, overwrite, ... ])

使用另一个DataFrame中的非NA值原地修改。

标志 (Flags)#

标志 (Flags) 指的是 pandas 对象的属性。数据集的属性(例如记录日期、访问的 URL 等)应存储在 DataFrame.attrs 中。

Flags (obj, *, allows_duplicate_labels)

应用于 pandas 对象的 Flags。

元数据 (Metadata)#

DataFrame.attrs 是一个用于为此 DataFrame 存储全局元数据的字典。

警告

DataFrame.attrs 被认为是实验性的,可能会在没有警告的情况下进行更改。

DataFrame.attrs 

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

绘图 (Plotting)#

DataFrame.plot 既是一个可调用的方法,也是一个命名空间属性,用于形式为 DataFrame.plot.<kind> 的特定绘图方法。

DataFrame.plot ([x, y, kind, ax, ....])

DataFrame 绘图访问器和方法

DataFrame.plot.area ([x, y, stacked])

绘制堆叠面积图。

DataFrame.plot.bar ([x, y])

垂直条形图。

DataFrame.plot.barh ([x, y])

绘制横向条形图。

DataFrame.plot.box ([by])

绘制 DataFrame 列的箱线图。

DataFrame.plot.density ([bw_method, ind])

使用高斯核函数生成核密度估计图。

DataFrame.plot.hexbin (x, y[, C, ...])

生成六边形分箱图。

DataFrame.plot.hist ([by, bins])

绘制 DataFrame 列的直方图。

DataFrame.plot.kde ([bw_method, ind])

使用高斯核函数生成核密度估计图。

DataFrame.plot.line ([x, y])

将 Series 或 DataFrame 绘制成线图。

DataFrame.plot.pie (**kwargs)

生成饼图。

DataFrame.plot.scatter (x, y[, s, c])

创建具有不同标记点大小和颜色的散点图。

DataFrame.boxplot ([column, by, ax, ...])

从 DataFrame 列制作箱线图。

DataFrame.hist ([column, by, grid, ...])

绘制DataFrame列的直方图。

稀疏访问器 (Sparse accessor)#

稀疏数据类型 (sparse-dtype) 的特定方法和属性在 DataFrame.sparse 访问器下提供。

DataFrame.sparse.density 

稀疏点与总(密集)数据点之比。

DataFrame.sparse.from_spmatrix (data[, ...])

从 scipy 稀疏矩阵创建新的 DataFrame。

DataFrame.sparse.to_coo ()

以稀疏 SciPy COO 矩阵的形式返回 DataFrame 的内容。

DataFrame.sparse.to_dense ()

将具有稀疏值的 DataFrame 转换为密集格式。

序列化/IO/转换 (Serialization / IO / conversion)#

DataFrame.from_dict (data[, orient, dtype, ...])

从数组类或字典的字典构建DataFrame。

DataFrame.from_records (data[, index, ...])

将结构化或记录式ndarray转换为DataFrame。

DataFrame.to_orc ([path, engine, index, ...])

将 DataFrame 写入 ORC 格式。

DataFrame.to_parquet ([path, engine, ...])

将 DataFrame 写入二进制 parquet 格式。

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

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

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

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

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

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

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

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

DataFrame.to_dict ([orient, into, index])

将 DataFrame 转换为字典。

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

将对象写入 Excel 工作表。

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

将对象转换为 JSON 字符串。

DataFrame.to_html ([buf, columns, col_space, ...])

将 DataFrame 渲染为 HTML 表。

DataFrame.to_feather (path, **kwargs)

将 DataFrame 写入二进制 Feather 格式。

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

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

DataFrame.to_stata (path, *[, convert_dates, ...])

将 DataFrame 对象导出为 Stata dta 格式。

DataFrame.to_gbq (destination_table, *[, ...])

(已弃用)将 DataFrame 写入 Google BigQuery 表。

DataFrame.to_records ([index, column_dtypes, ...])

将 DataFrame 转换为 NumPy 记录数组。

DataFrame.to_string ([buf, columns, ...])

将 DataFrame 渲染成控制台友好的表格输出。

DataFrame.to_clipboard (*[, excel, sep])

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

DataFrame.to_markdown ([buf, mode, index, ...])

以 Markdown 友好的格式打印 DataFrame。

DataFrame.style

返回一个 Styler 对象。

DataFrame.__dataframe__ ([nan_as_null, ...])

返回实现互操作协议的dataframe互操作对象。