python之Pandas笔记
Pandas 是基于 NumPy 构建的库,在数据处理方面可以把它理解为 NumPy 加强版,同时 Pandas 也是一项开源项目。它基于 Cython,因此读取与处理数据非常快,并且还能轻松处理浮点数据中的缺失数据(表示为 NaN)以及非浮点数据。
pandas适合于许多不同类型的数据,包括:
- 具有异构类型列的表格数据,例如SQL表格或Excel数据
- 有序和无序(不一定是固定频率)时间序列数据。
- 具有行列标签的任意矩阵数据(均匀类型或不同类型)
- 任何其他形式的观测/统计数据集。
核心数据结构
pandas最核心的就是Series和DataFrame两个数据结构。
DataFrame可以看做是Series的容器,即:一个DataFrame中可以包含若干个Series。
名称 | 维度 | 说明 |
---|---|---|
Series | 1维 | 带有标签的同构类型数组 |
DataFrame | 2维 | 表格结构,带有标签,大小可变,且可以包含异构的数据列 |
Series
Series的创建
|
|
编号 | 参数 | 描述 |
---|---|---|
1 | data |
数据采取各种形式,如:ndarray ,list ,constants |
2 | index |
索引值必须是唯一的和散列的,与数据的长度相同。 默认np.arange(n) 如果没有索引被传递。 |
3 | dtype |
dtype 用于数据类型。如果没有,将推断数据类型 |
4 | copy |
复制数据,默认为false 。 |
|
|
Series基本功能
编号 | 属性或方法 | 描述 |
---|---|---|
1 | axes |
返回行轴标签列表。 |
2 | dtype |
返回对象的数据类型(dtype )。 |
3 | empty |
如果系列为空,则返回True 。 |
4 | ndim |
返回底层数据的维数,默认定义:1 。 |
5 | size |
返回基础数据中的元素数。 |
6 | values |
将系列作为ndarray 返回。 |
7 | head() |
返回前n 行。 |
8 | tail() |
返回最后n 行。 |
|
|
Series其他方法
|
|
DataFrame
DataFrame创建
|
|
编号 | 参数 | 描述 |
---|---|---|
1 | data |
数据采取各种形式,如:ndarray ,series ,map ,lists ,dict ,constant 和另一个DataFrame 。 |
2 | index |
对于行标签,要用于结果帧的索引是可选缺省值np.arrange(n) ,如果没有传递索引值。 |
3 | columns |
对于列标签,可选的默认语法是 - np.arange(n) 。 这只有在没有索引传递的情况下才是这样。 |
4 | dtype |
每列的数据类型。 |
5 | copy |
如果默认值为False ,则此命令(或任何它)用于复制数据。 |
|
|
DataFrame基本功能
编号 | 属性或方法 | 描述 |
---|---|---|
1 | T |
转置行和列。 |
2 | axes |
返回一个列,行轴标签和列轴标签作为唯一的成员。 |
3 | dtypes |
返回此对象中的数据类型(dtypes )。 |
4 | empty |
如果NDFrame 完全为空[无项目],则返回为True ; 如果任何轴的长度为0 。 |
5 | ndim |
轴/数组维度大小。 |
6 | shape |
返回表示DataFrame 的维度的元组。 |
7 | size |
NDFrame 中的元素数。 |
8 | values |
NDFrame的Numpy表示。 |
9 | head() |
返回开头前n 行。 |
10 | tail() |
返回最后n 行。 |
|
|
Pandas常用函数
统计函数
编号 | 函数 | 描述 |
---|---|---|
1 | count() |
非空观测数量 |
2 | sum() |
所有值之和 |
3 | mean() |
所有值的平均值 |
4 | median() |
所有值的中位数 |
5 | mode() |
值的模值 |
6 | std() |
值的标准偏差 |
7 | min() |
所有值中的最小值 |
8 | max() |
所有值中的最大值 |
9 | abs() |
绝对值 |
10 | prod() |
数组元素的乘积 |
11 | cumsum() |
累计总和 |
12 | cumprod() |
累计乘积 |
13 | pct_change() |
计算变化百分比 |
14 | conv() |
计算协方差 |
15 | corr() |
相关性计算 |
16 | rank() |
数据排名 |
|
|
应用函数
要将自定义或其他库的函数应用于Pandas对象,有三个重要的方法,下面来讨论如何使用这些方法。使用适当的方法取决于函数是否期望在整个
DataFrame
,行或列或元素上进行操作。
- 表合理函数应用:
pipe()
- 行或列函数应用:
apply()
- 元素函数应用:
applymap()
|
|
迭代函数
Pandas
对象之间的基本迭代的行为取决于类型。当迭代一个系列时,它被视为数组式,基本迭代产生这些值。其他数据结构,如:DataFrame
,遵循类似惯例迭代对象的键。要遍历数据帧(
DataFrame
)中的行,可以使用以下函数 -
iteritems()
- 迭代(key,value)
对iterrows()
- 将行迭代为(索引,系列)对itertuples()
- 以namedtuples
的形式迭代行
|
|
字符与文本处理函数
编号 | 函数 | 描述 |
---|---|---|
1 | lower() |
将Series/Index 中的字符串转换为小写。 |
2 | upper() |
将Series/Index 中的字符串转换为大写。 |
3 | len() |
计算字符串长度。 |
4 | strip() |
帮助从两侧的系列/索引中的每个字符串中删除空格(包括换行符)。 |
5 | split(' ') |
用给定的模式拆分每个字符串。 |
6 | cat(sep=' ') |
使用给定的分隔符连接系列/索引元素。 |
7 | get_dummies() |
返回具有单热编码值的数据帧(DataFrame)。 |
8 | contains(pattern) |
如果元素中包含子字符串,则返回每个元素的布尔值True ,否则为False 。 |
9 | replace(a,b) |
将值a 替换为值b 。 |
10 | repeat(value) |
重复每个元素指定的次数。 |
11 | count(pattern) |
返回模式中每个元素的出现总数。 |
12 | startswith(pattern) |
如果系列/索引中的元素以模式开始,则返回true 。 |
13 | endswith(pattern) |
如果系列/索引中的元素以模式结束,则返回true 。 |
14 | find(pattern) |
返回模式第一次出现的位置。 |
15 | findall(pattern) |
返回模式的所有出现的列表。 |
16 | swapcase |
变换字母大小写。 |
17 | islower() |
检查系列/索引中每个字符串中的所有字符是否小写,返回布尔值 |
18 | isupper() |
检查系列/索引中每个字符串中的所有字符是否大写,返回布尔值 |
19 | isnumeric() |
检查系列/索引中每个字符串中的所有字符是否为数字,返回布尔值。 |
几乎这些方法都使用Python字符串函数。 因此,将Series对象转换为String对象,然后执行以上操作即可。
窗口函数
rolling()
:滚动函数expanding()
:展开函数ewm()
:指数移动窗口shift()
:移位函数窗口函数主要搭配统计函数来计算。
|
|
Pandas索引与选择
Python
和NumPy
索引运算符"[]"
和属性运算符"."
。 可以在广泛的用例中快速轻松地访问Pandas数据结构。然而,由于要访问的数据类型不是预先知道的,所以直接使用标准运算符具有一些优化限制。
*Pandas
*现在支持三种类型的多轴索引:
编号 | 索引 | 描述 |
---|---|---|
1 | .loc() |
基于标签 |
2 | .iloc() |
基于整数 |
3 | .ix() |
基于标签和整数 |
|
|
Pandas分组聚合
任何分组(
groupby
)操作都涉及原始对象的以下操作之一。它们是 :
- 分割对象
- 应用一个函数
- 结合的结果
在许多情况下,我们将数据分成多个集合,并在每个子集上应用一些函数。在应用函数中,可以执行以下操作 :
- 聚合 - 计算汇总统计
- 转换 - 执行一些特定于组的操作
- 过滤 - 在某些情况下丢弃数据
Pandas
对象可以分成任何对象。有多种方式来拆分对象
obj.groupby(‘key’)
obj.groupby([‘key1’,’key2’])
obj.groupby(key,axis=1)
|
|
聚合函数为每个组返回单个聚合值。当创建了分组(group by)对象,就可以对分组数据执行多个聚合操作。
一个常用的方法是
agg
分组或列上的转换返回索引大小与被分组的索引相同的对象。因此,转换应该返回与组块大小相同的结果。
转换的方法是
transform
过滤根据定义的标准过滤数据并返回数据的子集。
过滤用的方法是
filter
|
|
Pandas合并与级联
Pandas合并
Pandas提供了一个单独的
merge()
函数来进行连接。
|
|
- left - 一个DataFrame对象。
- right - 另一个DataFrame对象。
- on - 列(名称)连接,必须在左和右DataFrame对象中存在(找到)。
- left_on - 左侧DataFrame中的列用作键,可以是列名或长度等于DataFrame长度的数组。
- right_on - 来自右的DataFrame的列作为键,可以是列名或长度等于DataFrame长度的数组。
- left_index - 如果为
True
,则使用左侧DataFrame中的索引(行标签)作为其连接键。 在具有MultiIndex(分层)的DataFrame的情况下,级别的数量必须与来自右DataFrame的连接键的数量相匹配。- right_index - 与右DataFrame的left_index具有相同的用法。
- how - 它是left, right, outer以及inner之中的一个,默认为内inner。 下面将介绍每种方法的用法。
- sort - 按照字典顺序通过连接键对结果DataFrame进行排序。默认为
True
,设置为False
时,在很多情况下大大提高性能。
|
|
how
参数与sql
:
合并方法 | SQL等效 | 描述 |
---|---|---|
left |
LEFT OUTER JOIN |
使用左侧对象的键 |
right |
RIGHT OUTER JOIN |
使用右侧对象的键 |
outer |
FULL OUTER JOIN |
使用键的联合 |
inner |
INNER JOIN |
使用键的交集 |
|
|
Pandas级联
|
|
- objs - 这是Series,DataFrame或Panel对象的序列或映射。
- axis -
{0,1,...}
,默认为0
,这是连接的轴。- join -
{'inner', 'outer'}
,默认inner
。如何处理其他轴上的索引。联合的外部和交叉的内部。- ignore_index − 布尔值,默认为
False
。如果指定为True
,则不要使用连接轴上的索引值。结果轴将被标记为:0,...,n-1
。- join_axes - 这是Index对象的列表。用于其他
(n-1)
轴的特定索引,而不是执行内部/外部集逻辑。
|
|
|
|
Pandas时间工具
|
|
Pandas类别数据
通常实时的数据包括重复的文本列。例如:性别,国家和代码等特征总是重复的。这些是分类数据的例子。
分类变量只能采用有限的数量,而且通常是固定的数量。除了固定长度,分类数据可能有顺序,但不能执行数字操作。 分类是*
Pandas
*数据类型。分类数据类型在以下情况下非常有用 :
- 一个字符串变量,只包含几个不同的值。将这样的字符串变量转换为分类变量将会节省一些内存。
- 变量的词汇顺序与逻辑顺序(
"one"
,"two"
,"three"
)不同。 通过转换为分类并指定类别上的顺序,排序和最小/最大将使用逻辑顺序,而不是词法顺序。- 作为其他python库的一个信号,这个列应该被当作一个分类变量(例如,使用合适的统计方法或
plot
类型)。分类对象可以通过多种方式创建。
pandas
对象创建中将dtype
指定为category
pd.Categorical
|
|
Pandas IO工具
Pandas I/O API是一套
pd.read_xxx()
,返回Pandas
对象的顶级读取器函数。例如:
read_csv
read_excel
read_table
read_sql
- 等等
|
|
Pandas自定义设置
API由五个相关函数组成。它们分别是:
get_option()
set_option()
reset_option()
describe_option()
option_context()
编号 | 参数 | 描述 |
---|---|---|
1 | display.max_rows |
要显示的最大行数 |
2 | display.max_columns |
要显示的最大列数 |
3 | display.expand_frame_repr |
显示数据帧以拉伸页面 |
4 | display.max_colwidth |
显示最大列宽 |
5 | display.precision |
显示十进制数的精度 |
|
|
Pandas运用实例
|
|
Pandas参考函数API
构造函数
方法 | 描述 |
---|---|
DataFrame([data, index, columns, dtype, copy]) | 构造数据框 |
属性和数据
方法 | 描述 |
---|---|
Axes | index: row labels;columns: column labels |
DataFrame.as_matrix([columns]) | 转换为矩阵 |
DataFrame.dtypes | 返回数据的类型 |
DataFrame.ftypes | Return the ftypes (indication of sparse/dense and dtype) in this object. |
DataFrame.get_dtype_counts() | 返回数据框数据类型的个数 |
DataFrame.get_ftype_counts() | Return the counts of ftypes in this object. |
DataFrame.select_dtypes([include, exclude]) | 根据数据类型选取子数据框 |
DataFrame.values | Numpy的展示方式 |
DataFrame.axes | 返回横纵坐标的标签名 |
DataFrame.ndim | 返回数据框的纬度 |
DataFrame.size | 返回数据框元素的个数 |
DataFrame.shape | 返回数据框的形状 |
DataFrame.memory_usage([index, deep]) | Memory usage of DataFrame columns. |
类型转换
方法 | 描述 |
---|---|
DataFrame.astype(dtype[, copy, errors]) | 转换数据类型 |
DataFrame.copy([deep]) | 复制数据框 |
DataFrame.isnull() | 以布尔的方式返回空值 |
DataFrame.notnull() | 以布尔的方式返回非空值 |
索引和迭代
方法 | 描述 |
---|---|
DataFrame.head([n]) | 返回前n行数据 |
DataFrame.at | 快速标签常量访问器 |
DataFrame.iat | 快速整型常量访问器 |
DataFrame.loc | 标签定位 |
DataFrame.iloc | 整型定位 |
DataFrame.insert(loc, column, value[, …]) | 在特殊地点插入行 |
DataFrame.iter() | Iterate over infor axis |
DataFrame.iteritems() | 返回列名和序列的迭代器 |
DataFrame.iterrows() | 返回索引和序列的迭代器 |
DataFrame.itertuples([index, name]) | Iterate over DataFrame rows as namedtuples, with index value as first element of the tuple. |
DataFrame.lookup(row_labels, col_labels) | Label-based “fancy indexing” function for DataFrame. |
DataFrame.pop(item) | 返回删除的项目 |
DataFrame.tail([n]) | 返回最后n行 |
DataFrame.xs(key[, axis, level, drop_level]) | Returns a cross-section (row(s) or column(s)) from the Series/DataFrame. |
DataFrame.isin(values) | 是否包含数据框中的元素 |
DataFrame.where(cond[, other, inplace, …]) | 条件筛选 |
DataFrame.mask(cond[, other, inplace, axis, …]) | Return an object of same shape as self and whose corresponding entries are from self where cond is False and otherwise are from other. |
DataFrame.query(expr[, inplace]) | Query the columns of a frame with a boolean expression. |
二元运算
方法 | 描述 |
---|---|
DataFrame.add(other[, axis, level, fill_value]) | 加法,元素指向 |
DataFrame.sub(other[, axis, level, fill_value]) | 减法,元素指向 |
DataFrame.mul(other[, axis, level, fill_value]) | 乘法,元素指向 |
DataFrame.div(other[, axis, level, fill_value]) | 小数除法,元素指向 |
DataFrame.truediv(other[, axis, level, …]) | 真除法,元素指向 |
DataFrame.floordiv(other[, axis, level, …]) | 向下取整除法,元素指向 |
DataFrame.mod(other[, axis, level, fill_value]) | 模运算,元素指向 |
DataFrame.pow(other[, axis, level, fill_value]) | 幂运算,元素指向 |
DataFrame.radd(other[, axis, level, fill_value]) | 右侧加法,元素指向 |
DataFrame.rsub(other[, axis, level, fill_value]) | 右侧减法,元素指向 |
DataFrame.rmul(other[, axis, level, fill_value]) | 右侧乘法,元素指向 |
DataFrame.rdiv(other[, axis, level, fill_value]) | 右侧小数除法,元素指向 |
DataFrame.rtruediv(other[, axis, level, …]) | 右侧真除法,元素指向 |
DataFrame.rfloordiv(other[, axis, level, …]) | 右侧向下取整除法,元素指向 |
DataFrame.rmod(other[, axis, level, fill_value]) | 右侧模运算,元素指向 |
DataFrame.rpow(other[, axis, level, fill_value]) | 右侧幂运算,元素指向 |
DataFrame.lt(other[, axis, level]) | 类似Array.lt |
DataFrame.gt(other[, axis, level]) | 类似Array.gt |
DataFrame.le(other[, axis, level]) | 类似Array.le |
DataFrame.ge(other[, axis, level]) | 类似Array.ge |
DataFrame.ne(other[, axis, level]) | 类似Array.ne |
DataFrame.eq(other[, axis, level]) | 类似Array.eq |
DataFrame.combine(other, func[, fill_value, …]) | Add two DataFrame objects and do not propagate NaN values, so if for a |
DataFrame.combine_first(other) | Combine two DataFrame objects and default to non-null values in frame calling the method. |
函数应用&分组&窗口
方法 | 描述 |
---|---|
[DataFrame.apply(func, axis, broadcast, …]) | 应用函数 |
DataFrame.applymap(func) | Apply a function to a DataFrame that is intended to operate elementwise, i.e. |
DataFrame.aggregate(func[, axis]) | Aggregate using callable, string, dict, or list of string/callables |
DataFrame.transform(func, *args, **kwargs) | Call function producing a like-indexed NDFrame |
DataFrame.groupby([by, axis, level, …]) | 分组 |
DataFrame.rolling(window[, min_periods, …]) | 滚动窗口 |
DataFrame.expanding([min_periods, freq, …]) | 拓展窗口 |
DataFrame.ewm([com, span, halflife, alpha, …]) | 指数权重窗口 |
描述统计学
方法 | 描述 |
---|---|
DataFrame.abs() | 返回绝对值 |
DataFrame.all([axis, bool_only, skipna, level]) | Return whether all elements are True over requested axis |
DataFrame.any([axis, bool_only, skipna, level]) | Return whether any element is True over requested axis |
DataFrame.clip([lower, upper, axis]) | Trim values at input threshold(s). |
DataFrame.clip_lower(threshold[, axis]) | Return copy of the input with values below given value(s) truncated. |
DataFrame.clip_upper(threshold[, axis]) | Return copy of input with values above given value(s) truncated. |
DataFrame.corr([method, min_periods]) | 返回本数据框成对列的相关性系数 |
DataFrame.corrwith(other[, axis, drop]) | 返回不同数据框的相关性 |
DataFrame.count([axis, level, numeric_only]) | 返回非空元素的个数 |
DataFrame.cov([min_periods]) | 计算协方差 |
DataFrame.cummax([axis, skipna]) | Return cumulative max over requested axis. |
DataFrame.cummin([axis, skipna]) | Return cumulative minimum over requested axis. |
DataFrame.cumprod([axis, skipna]) | 返回累积 |
DataFrame.cumsum([axis, skipna]) | 返回累和 |
DataFrame.describe([percentiles, include, …]) | 整体描述数据框 |
DataFrame.diff([periods, axis]) | 1st discrete difference of object |
DataFrame.eval(expr[, inplace]) | Evaluate an expression in the context of the calling DataFrame instance. |
DataFrame.kurt([axis, skipna, level, …]) | 返回无偏峰度Fisher’s (kurtosis of normal == 0.0). |
DataFrame.mad([axis, skipna, level]) | 返回偏差 |
DataFrame.max([axis, skipna, level, …]) | 返回最大值 |
DataFrame.mean([axis, skipna, level, …]) | 返回均值 |
DataFrame.median([axis, skipna, level, …]) | 返回中位数 |
DataFrame.min([axis, skipna, level, …]) | 返回最小值 |
DataFrame.mode([axis, numeric_only]) | 返回众数 |
DataFrame.pct_change([periods, fill_method, …]) | 返回百分比变化 |
DataFrame.prod([axis, skipna, level, …]) | 返回连乘积 |
DataFrame.quantile([q, axis, numeric_only, …]) | 返回分位数 |
DataFrame.rank([axis, method, numeric_only, …]) | 返回数字的排序 |
DataFrame.round([decimals]) | Round a DataFrame to a variable number of decimal places. |
DataFrame.sem([axis, skipna, level, ddof, …]) | 返回无偏标准误 |
DataFrame.skew([axis, skipna, level, …]) | 返回无偏偏度 |
DataFrame.sum([axis, skipna, level, …]) | 求和 |
DataFrame.std([axis, skipna, level, ddof, …]) | 返回标准误差 |
DataFrame.var([axis, skipna, level, ddof, …]) | 返回无偏误差 |
从新索引&选取&标签操作
方法 | 描述 |
---|---|
DataFrame.add_prefix(prefix) | 添加前缀 |
DataFrame.add_suffix(suffix) | 添加后缀 |
DataFrame.align(other[, join, axis, level, …]) | Align two object on their axes with the |
DataFrame.drop(labels[, axis, level, …]) | 返回删除的列 |
[DataFrame.drop_duplicates(subset, keep, …]) | Return DataFrame with duplicate rows removed, optionally only |
[DataFrame.duplicated(subset, keep]) | Return boolean Series denoting duplicate rows, optionally only |
DataFrame.equals(other) | 两个数据框是否相同 |
DataFrame.filter([items, like, regex, axis]) | 过滤特定的子数据框 |
DataFrame.first(offset) | Convenience method for subsetting initial periods of time series data based on a date offset. |
DataFrame.head([n]) | 返回前n行 |
DataFrame.idxmax([axis, skipna]) | Return index of first occurrence of maximum over requested axis. |
DataFrame.idxmin([axis, skipna]) | Return index of first occurrence of minimum over requested axis. |
DataFrame.last(offset) | Convenience method for subsetting final periods of time series data based on a date offset. |
[DataFrame.reindex(index, columns]) | Conform DataFrame to new index with optional filling logic, placing NA/NaN in locations having no value in the previous index. |
[DataFrame.reindex_axis(labels, axis, …]) | Conform input object to new index with optional filling logic, placing NA/NaN in locations having no value in the previous index. |
[DataFrame.reindex_like(other, method, …]) | Return an object with matching indices to myself. |
[DataFrame.rename(index, columns]) | Alter axes input function or functions. |
DataFrame.rename_axis(mapper[, axis, copy, …]) | Alter index and / or columns using input function or functions. |
[DataFrame.reset_index(level, drop, …]) | For DataFrame with multi-level index, return new DataFrame with labeling information in the columns under the index names, defaulting to ‘level_0’, ‘level_1’, etc. |
DataFrame.sample([n, frac, replace, …]) | 返回随机抽样 |
DataFrame.select(crit[, axis]) | Return data corresponding to axis labels matching criteria |
[DataFrame.set_index(keys, drop, append, …]) | Set the DataFrame index (row labels) using one or more existing columns. |
DataFrame.tail([n]) | 返回最后几行 |
DataFrame.take(indices[, axis, convert, is_copy]) | Analogous to ndarray.take |
DataFrame.truncate([before, after, axis, copy]) | Truncates a sorted NDFrame before and/or after some particular index value. |
处理缺失值
方法 | 描述 |
---|---|
[DataFrame.dropna(axis, how, thresh, …]) | Return object with labels on given axis omitted where alternately any |
[DataFrame.fillna(value, method, axis, …]) | 填充空值 |
[DataFrame.replace(to_replace, value, …]) | Replace values given in ‘to_replace’ with ‘value’. |
从新定型&排序&转变形态
方法 | 描述 |
---|---|
DataFrame.pivot([index, columns, values]) | Reshape data (produce a “pivot” table) based on column values. |
DataFrame.reorder_levels(order[, axis]) | Rearrange index levels using input order. |
[DataFrame.sort_values(by, axis, ascending, …]) | Sort by the values along either axis |
[DataFrame.sort_index(axis, level, …]) | Sort object by labels (along an axis) |
[DataFrame.nlargest(n, columns, keep]) | Get the rows of a DataFrame sorted by the n largest values of columns. |
[DataFrame.nsmallest(n, columns, keep]) | Get the rows of a DataFrame sorted by the n smallest values of columns. |
DataFrame.swaplevel([i, j, axis]) | Swap levels i and j in a MultiIndex on a particular axis |
DataFrame.stack([level, dropna]) | Pivot a level of the (possibly hierarchical) column labels, returning a DataFrame (or Series in the case of an object with a single level of column labels) having a hierarchical index with a new inner-most level of row labels. |
DataFrame.unstack([level, fill_value]) | Pivot a level of the (necessarily hierarchical) index labels, returning a DataFrame having a new level of column labels whose inner-most level consists of the pivoted index labels. |
DataFrame.melt([id_vars, value_vars, …]) | “Unpivots” a DataFrame from wide format to long format, optionally |
DataFrame.T | Transpose index and columns |
DataFrame.to_panel() | Transform long (stacked) format (DataFrame) into wide (3D, Panel) format. |
DataFrame.to_xarray() | Return an xarray object from the pandas object. |
DataFrame.transpose(*args, **kwargs) | Transpose index and columns |
Combining& joining&merging
方法 | 描述 |
---|---|
[DataFrame.append(other, ignore_index, …]) | 追加数据 |
DataFrame.assign(**kwargs) | Assign new columns to a DataFrame, returning a new object (a copy) with all the original columns in addition to the new ones. |
DataFrame.join(other[, on, how, lsuffix, …]) | Join columns with other DataFrame either on index or on a key column. |
DataFrame.merge(right[, how, on, left_on, …]) | Merge DataFrame objects by performing a database-style join operation by columns or indexes. |
DataFrame.update(other[, join, overwrite, …]) | Modify DataFrame in place using non-NA values from passed DataFrame. |
时间序列
方法 | 描述 |
---|---|
DataFrame.asfreq(freq[, method, how, …]) | 将时间序列转换为特定的频次 |
DataFrame.asof(where[, subset]) | The last row without any NaN is taken (or the last row without |
DataFrame.shift([periods, freq, axis]) | Shift index by desired number of periods with an optional time freq |
DataFrame.first_valid_index() | Return label for first non-NA/null value |
DataFrame.last_valid_index() | Return label for last non-NA/null value |
DataFrame.resample(rule[, how, axis, …]) | Convenience method for frequency conversion and resampling of time series. |
DataFrame.to_period([freq, axis, copy]) | Convert DataFrame from DatetimeIndex to PeriodIndex with desired |
DataFrame.to_timestamp([freq, how, axis, copy]) | Cast to DatetimeIndex of timestamps, at beginning of period |
DataFrame.tz_convert(tz[, axis, level, copy]) | Convert tz-aware axis to target time zone. |
DataFrame.tz_localize(tz[, axis, level, …]) | Localize tz-naive TimeSeries to target time zone. |
作图
方法 | 描述 |
---|---|
[DataFrame.plot(x, y, kind, ax, ….]) | DataFrame plotting accessor and method |
DataFrame.plot.area([x, y]) | 面积图Area plot |
DataFrame.plot.bar([x, y]) | 垂直条形图Vertical bar plot |
DataFrame.plot.barh([x, y]) | 水平条形图Horizontal bar plot |
DataFrame.plot.box([by]) | 箱图Boxplot |
DataFrame.plot.density(**kwds) | 核密度Kernel Density Estimate plot |
DataFrame.plot.hexbin(x, y[, C, …]) | Hexbin plot |
DataFrame.plot.hist([by, bins]) | 直方图Histogram |
DataFrame.plot.kde(**kwds) | 核密度Kernel Density Estimate plot |
DataFrame.plot.line([x, y]) | 线图Line plot |
DataFrame.plot.pie([y]) | 饼图Pie chart |
DataFrame.plot.scatter(x, y[, s, c]) | 散点图Scatter plot |
DataFrame.boxplot([column, by, ax, …]) | Make a box plot from DataFrame column optionally grouped by some columns or |
DataFrame.hist(data[, column, by, grid, …]) | Draw histogram of the DataFrame’s series using matplotlib / pylab. |
转换为其他格式
方法 | 描述 |
---|---|
DataFrame.from_csv(path[, header, sep, …]) | Read CSV file (DEPRECATED, please use pandas.read_csv() instead). |
DataFrame.from_dict(data[, orient, dtype]) | Construct DataFrame from dict of array-like or dicts |
DataFrame.from_items(items[, columns, orient]) | Convert (key, value) pairs to DataFrame. |
DataFrame.from_records(data[, index, …]) | Convert structured or record ndarray to DataFrame |
DataFrame.info([verbose, buf, max_cols, …]) | Concise summary of a DataFrame. |
DataFrame.to_pickle(path[, compression, …]) | Pickle (serialize) object to input file path. |
DataFrame.to_csv([path_or_buf, sep, na_rep, …]) | Write DataFrame to a comma-separated values (csv) file |
DataFrame.to_hdf(path_or_buf, key, **kwargs) | Write the contained data to an HDF5 file using HDFStore. |
DataFrame.to_sql(name, con[, flavor, …]) | Write records stored in a DataFrame to a SQL database. |
DataFrame.to_dict([orient, into]) | Convert DataFrame to dictionary. |
DataFrame.to_excel(excel_writer[, …]) | Write DataFrame to an excel sheet |
DataFrame.to_json([path_or_buf, orient, …]) | Convert the object to a JSON string. |
DataFrame.to_html([buf, columns, col_space, …]) | Render a DataFrame as an HTML table. |
DataFrame.to_feather(fname) | write out the binary feather-format for DataFrames |
DataFrame.to_latex([buf, columns, …]) | Render an object to a tabular environment table. |
DataFrame.to_stata(fname[, convert_dates, …]) | A class for writing Stata binary dta files from array-like objects |
DataFrame.to_msgpack([path_or_buf, encoding]) | msgpack (serialize) object to input file path |
DataFrame.to_gbq(destination_table, project_id) | Write a DataFrame to a Google BigQuery table. |
DataFrame.to_records([index, convert_datetime64]) | Convert DataFrame to record array. |
DataFrame.to_sparse([fill_value, kind]) | Convert to SparseDataFrame |
DataFrame.to_dense() | Return dense representation of NDFrame (as opposed to sparse) |
DataFrame.to_string([buf, columns, …]) | Render a DataFrame to a console-friendly tabular output. |
DataFrame.to_clipboard([excel, sep]) | Attempt to write text representation of object to the system clipboard This can be pasted into Excel, for example. |