DataFrame是一个二维标记数据结构,具有可能不同类型的列。您可以将其视为电子表格或SQL表,或Series对象的字典。它通常是最常用的pandas对象。与Series一样,DataFrame接受许多不同类型的输入:

1.1D ndarray,list,dicts或Series的Dict
2.二维numpy.ndarray
3.结构化或记录 ndarray
4.Series
5.DataFrame

没我们想象的那么复杂,对于DataFrame的操作无非也就是增删改查。我们也经常将DataFrame称之为数据帧。

DataFrame的使用方法如下:

DataFrame(columnsMap)
pandas.DataFrame( data, index, columns, dtype, copy)
data:上面介绍的各种数据类型
index:行标签,可选缺省值np.arrange(n)
columns:列标签,可选缺省值 np.arange(n)
dtype:列的数据类型
copy:默认值为False,则此命令(或任何它)用于复制数据。

创建一个空的DataFrame:

python dataframe生成表格 python3 dataframe_数据

从list或者1D ndarray构建

python dataframe生成表格 python3 dataframe_变量名_02

通过index和columns属性分别访问行标签和列标签

python dataframe生成表格 python3 dataframe_元组_03

指定行列索引

python dataframe生成表格 python3 dataframe_变量名_04

python dataframe生成表格 python3 dataframe_数据_05

访问列:变量名[列名]

python dataframe生成表格 python3 dataframe_变量名_06

访问行:变量名[n:m] 访问从n开始到m-1行的数据

python dataframe生成表格 python3 dataframe_数据_07

同样也可以通过索引名来访问:

python dataframe生成表格 python3 dataframe_变量名_08

访问行和列(块):
变量名.iloc[n1:n2,m1:m2] 访问n1到(n2-1)行,m1到(m2-1)列

python dataframe生成表格 python3 dataframe_数据_09

访问指定位置:变量名.at[行名,列名]

python dataframe生成表格 python3 dataframe_变量名_10


如果key不存在的话,会报如上错误。

访问指定位置:变量名.loc[行索引名,列索引名]

python dataframe生成表格 python3 dataframe_数据_11

使用字典dict

python dataframe生成表格 python3 dataframe_元组_12

如果使用单纯的字典数据,那么一定要记得指定index,否则会报错。

python dataframe生成表格 python3 dataframe_数据_13

使用来自Series 的 dict

得到的索引将是各个Series的索引的并集。如果有任何嵌套的dict,则首先将这些dicts转换为Series。如果没有传递列,则列将是dict键的有序列表。

python dataframe生成表格 python3 dataframe_数据_14


如果Series的长度不同,并且具有不同的索引,那么会是什么情况呢?

python dataframe生成表格 python3 dataframe_元组_15


如果两者部分索引相同呢?

python dataframe生成表格 python3 dataframe_变量名_16


大家可以看到得到的索引将是各个Series的索引的并集,结合上面的例子,想必大家都理解了。

当我们在DataFrame的构造函数中指定列的时候,那么该值会覆盖dict的键值作为列的索引名。否则会默认将dict的键值作为列的索引名。

python dataframe生成表格 python3 dataframe_变量名_17


这里我们重新将列进行命名,在构造DataFrame的时候,在数据d中找不到以’姓名’和’年龄’为键值的数据,所以最后的结果为空。做个比较:

python dataframe生成表格 python3 dataframe_变量名_18


那么如果我们的行索引也是完全不同的呢?

python dataframe生成表格 python3 dataframe_数据_19


最终的结果好像和我想的不一样。。。再试一下,追加个Series中的索引名。

python dataframe生成表格 python3 dataframe_数据_20

额,看样子得这么说了:DataFrame构造函数中指定的行列索引优先于dict。

使用dict列表

python dataframe生成表格 python3 dataframe_数据_21


这个就不多做解释了,上面的都能看懂,这个自然不在话下了。

使用元组和元组字典

Python 的元组与列表类似,不同之处在于元组的元素不能修改。

元组使用小括号,列表使用方括号。

元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可。

python dataframe生成表格 python3 dataframe_变量名_22

python dataframe生成表格 python3 dataframe_变量名_23

python dataframe生成表格 python3 dataframe_变量名_24

替代构造函数:DataFrame.from_dict

DataFrame.from_dict采用dicts的dict或类似数组序列的dict并返回DataFrame。DataFrame除了默认情况下的orient参数外,它的操作类似于构造函数’columns’,但可以将其设置’index’为使用dict键作为行标签。

python dataframe生成表格 python3 dataframe_变量名_25

DataFrame.from_records

DataFrame.from_records获取元组列表或带有结构化dtype的ndarray。它类似于普通DataFrame构造函数,但生成的DataFrame索引可能是结构化dtype的特定字段。

python dataframe生成表格 python3 dataframe_元组_26

python dataframe生成表格 python3 dataframe_元组_27


这个有点可能有点复杂,关于dtype的的一些东西可以参考下面的链接

https://docs.scipy.org/doc/numpy/reference/arrays.dtypes.html

python dataframe生成表格 python3 dataframe_变量名_28

获取,设置和删除列的工作方式与类似的dict操作相同,这里就不在做更多的演示。

插入标量值

插入标量值得时候,它会沿着列进行填充

python dataframe生成表格 python3 dataframe_变量名_29

assign()

DataFrame有一种assign() 方法可以让您轻松创建可能从现有列派生的新列,它始终返回数据的副本,保持原始DataFrame不变。

python dataframe生成表格 python3 dataframe_元组_30

转置 DataFrane.T

python dataframe生成表格 python3 dataframe_数据_31

DataFrame列属性访问

如果DataFrame列标签是有效的Python变量名称,则可以像属性一样访问该列:

注意:仅限于列,用在行会出错的。

python dataframe生成表格 python3 dataframe_变量名_32

to_string() 将以表格形式返回DataFrame的字符串表示形式

python dataframe生成表格 python3 dataframe_数据_33

使用info()获取摘要

python dataframe生成表格 python3 dataframe_变量名_34