您好,欢迎访问一九零五行业门户网

Python 数据处理库 pandas 入门

pandas是一个python语言的软件包,在我们使用python语言进行机器学习编程的时候,这是一个非常常用的基础编程库。本文是对python 数据处理库 pandas 入门教程,非常不错,感兴趣的朋友一起看看吧
pandas是一个python语言的软件包,在我们使用python语言进行机器学习编程的时候,这是一个非常常用的基础编程库。本文是对它的一个入门教程。
pandas提供了快速,灵活和富有表现力的数据结构,目的是使“关系”或“标记”数据的工作既简单又直观。它旨在成为在python中进行实际数据分析的高级构建块。
入门介绍
pandas适合于许多不同类型的数据,包括:
具有异构类型列的表格数据,例如sql表格或excel数据
有序和无序(不一定是固定频率)时间序列数据。
具有行列标签的任意矩阵数据(均匀类型或不同类型)
任何其他形式的观测/统计数据集。
由于这是一个python语言的软件包,因此需要你的机器上首先需要具备python语言的环境。关于这一点,请自行在网络上搜索获取方法。
关于如何获取pandas请参阅官网上的说明:pandas installation。
通常情况下,我们可以通过pip来执行安装:
sudo pip3 install pandas
或者通过conda 来安装pandas:
conda install pandas
目前(2018年2月)pandas的最新版本是v0.22.0(发布时间:2017年12月29日)。
我已经将本文的源码和测试数据放到github上:pandas_tutorial ,读者可以前往获取。
另外,pandas常常和numpy一起使用,本文中的源码中也会用到numpy。
建议读者先对numpy有一定的熟悉再来学习pandas,我之前也写过一个numpy的基础教程,参见这里:python 机器学习库 numpy 教程
核心数据结构
pandas最核心的就是series和dataframe两个数据结构。
这两种类型的数据结构对比如下:
dataframe可以看做是series的容器,即:一个dataframe中可以包含若干个series。
注:在0.20.0版本之前,还有一个三维的数据结构,名称为panel。这也是pandas库取名的原因:pan-da-s。但这种数据结构由于很少被使用到,因此已经被废弃了。
series
由于series是一维结构的数据,我们可以直接通过数组来创建这种数据,像这样:
# data_structure.py import pandas as pd import numpy as np series1 = pd.series([1, 2, 3, 4]) print("series1:\n{}\n".format(series1))
这段代码输出如下:
series1: 0 1 1 2 2 3 3 4 dtype: int64
这段输出说明如下:
输出的最后一行是series中数据的类型,这里的数据都是int64类型的。
数据在第二列输出,第一列是数据的索引,在pandas中称之为index。
我们可以分别打印出series中的数据和索引:
# data_structure.py print("series1.values: {}\n".format(series1.values)) print("series1.index: {}\n".format(series1.index))
这两行代码输出如下:
series1.values: [1 2 3 4] series1.index: rangeindex(start=0, stop=4, step=1)
如果不指定(像上面这样),索引是[1, n-1]的形式。不过我们也可以在创建series的时候指定索引。索引未必一定需要是整数,可以是任何类型的数据,例如字符串。例如我们以七个字母来映射七个音符。索引的目的是可以通过它来获取对应的数据,例如下面这样:
# data_structure.py series2 = pd.series([1, 2, 3, 4, 5, 6, 7], index=["c", "d", "e", "f", "g", "a", "b"]) print("series2:\n{}\n".format(series2)) print("e is {}\n".format(series2["e"]))
这段代码输出如下:
series2: c 1 d 2 e 3 f 4 g 5 a 6 b 7 dtype: int64 e is 3 dataframe
下面我们来看一下dataframe的创建。我们可以通过numpy的接口来创建一个4x4的矩阵,以此来创建一个dataframe,像这样:
# data_structure.py df1 = pd.dataframe(np.arange(16).reshape(4,4)) print("df1:\n{}\n".format(df1))
这段代码输出如下:
df1: 0 1 2 3 0 0 1 2 3 1 4 5 6 7 2 8 9 10 11 3 12 13 14 15
从这个输出我们可以看到,默认的索引和列名都是[0, n-1]的形式。
我们可以在创建dataframe的时候指定列名和索引,像这样:
# data_structure.py df2 = pd.dataframe(np.arange(16).reshape(4,4), columns=["column1", "column2", "column3", "column4"], index=["a", "b", "c", "d"]) print("df2:\n{}\n".format(df2))
这段代码输出如下:
df2: column1 column2 column3 column4 a 0 1 2 3 b 4 5 6 7 c 8 9 10 11 d 12 13 14 15
我们也可以直接指定列数据来创建dataframe:
# data_structure.py df3 = pd.datafrme({"note" : ["c", "d", "e", "f", "g", "a", "b"], "weekday": ["mon", "tue", "wed", "thu", "fri", "sat", "sun"]}) print("df3:\n{}\n".format(df3))
这段代码输出如下:
df3: note weekday 0 c mon 1 d tue 2 e wed 3 f thu 4 g fri 5 a sat 6 b sun
请注意:
dataframe的不同列可以是不同的数据类型
如果以series数组来创建dataframe,每个series将成为一行,而不是一列
例如:
# data_structure.py noteseries = pd.series(["c", "d", "e", "f", "g", "a", "b"], index=[1, 2, 3, 4, 5, 6, 7]) weekdayseries = pd.series(["mon", "tue", "wed", "thu", "fri", "sat", "sun"], index=[1, 2, 3, 4, 5, 6, 7]) df4 = pd.dataframe([noteseries, weekdayseries]) print("df4:\n{}\n".format(df4))
df4的输出如下:
df4: 1 2 3 4 5 6 7 0 c d e f g a b 1 mon tue wed thu fri sat sun
我们可以通过下面的形式给dataframe添加或者删除列数据:
# data_structure.py df3["no."] = pd.series([1, 2, 3, 4, 5, 6, 7]) print("df3:\n{}\n".format(df3)) del df3["weekday"] print("df3:\n{}\n".format(df3))
这段代码输出如下:
df3: note weekday no. 0 c mon 1 1 d tue 2 2 e wed 3 3 f thu 4 4 g fri 5 5 a sat 6 6 b sun 7 df3: note no. 0 c 1 1 d 2 2 e 3 3 f 4 4 g 5 5 a 6 6 b 7
index对象与数据访问
pandas的index对象包含了描述轴的元数据信息。当创建series或者dataframe的时候,标签的数组或者序列会被转换成index。可以通过下面的方式获取到dataframe的列和行的index对象:
# data_structure.py print("df3.columns\n{}\n".format(df3.columns)) print("df3.index\n{}\n".format(df3.index))
这两行代码输出如下:
df3.columns index(['note', 'no.'], dtype='object') df3.index rangeindex(start=0, stop=7, step=1)
请注意:
index并非集合,因此其中可以包含重复的数据
index对象的值是不可以改变,因此可以通过它安全的访问数据
dataframe提供了下面两个操作符来访问其中的数据:
loc:通过行和列的索引来访问数据
iloc:通过行和列的下标来访问数据
例如这样:
# data_structure.py print("note c, d is:\n{}\n".format(df3.loc[[0, 1], "note"])) print("note c, d is:\n{}\n".format(df3.iloc[[0, 1], 0]))
第一行代码访问了行索引为0和1,列索引为“note”的元素。第二行代码访问了行下标为0和1(对于df3来说,行索引和行下标刚好是一样的,所以这里都是0和1,但它们却是不同的含义),列下标为0的元素。
这两行代码输出如下:
note c, d is: 0 c 1 d name: note, dtype: object note c, d is: 0 c 1 d name: note, dtype: object
文件操作
pandas库提供了一系列的read_函数来读取各种格式的文件,它们如下所示:
read_csv
read_table
read_fwf
read_clipboard
read_excel
read_hdf
read_html
read_json
read_msgpack
read_pickle
read_sas
read_sql
read_stata
read_feather
读取excel文件
注:要读取excel文件,还需要安装另外一个库:xlrd
通过pip可以这样完成安装:
sudo pip3 install xlrd
安装完之后可以通过pip查看这个库的信息:
$ pip3 show xlrd name: xlrd version: 1.1.0 summary: library for developers to extract data from microsoft excel (tm) spreadsheet files home-page: http://www.python-excel.org/ author: john machin author-email: sjmachin@lexicon.net license: bsd location: /library/frameworks/python.framework/versions/3.6/lib/python3.6/site-packages requires:
接下来我们看一个读取excel的简单的例子:
# file_operation.py import pandas as pd import numpy as np df1 = pd.read_excel("data/test.xlsx") print("df1:\n{}\n".format(df1))
这个excel的内容如下:
df1: c mon 0 d tue 1 e wed 2 f thu 3 g fri 4 a sat 5 b sun
注:本文的代码和数据文件可以通过文章开头提到的github仓库获取。
读取csv文件
下面,我们再来看读取csv文件的例子。
第一个csv文件内容如下:
$ cat test1.csv c,mon d,tue e,wed f,thu g,fri a,sat
读取的方式也很简单:
# file_operation.py df2 = pd.read_csv("data/test1.csv") print("df2:\n{}\n".format(df2))
我们再来看第2个例子,这个文件的内容如下:
$ cat test2.csv c|mon d|tue e|wed f|thu g|fri a|sat
严格的来说,这并不是一个csv文件了,因为它的数据并不是通过逗号分隔的。在这种情况下,我们可以通过指定分隔符的方式来读取这个文件,像这样:
# file_operation.py df3 = pd.read_csv("data/test2.csv", sep="|") print("df3:\n{}\n".format(df3))
实际上,read_csv支持非常多的参数用来调整读取的参数,如下表所示:
参数说明
path 文件路径
sep或者delimiter 字段分隔符
header 列名的行数,默认是0(第一行)
index_col 列号或名称用作结果中的行索引
names 结果的列名称列表
skiprows 从起始位置跳过的行数
na_values 代替na的值序列
comment 以行结尾分隔注释的字符
parse_dates 尝试将数据解析为datetime。默认为false
keep_date_col 如果将列连接到解析日期,保留连接的列。默认为false。
converters 列的转换器
dayfirst 当解析可以造成歧义的日期时,以内部形式存储。默认为false
data_parser 用来解析日期的函数
nrows 从文件开始读取的行数
iterator 返回一个textparser对象,用于读取部分内容
chunksize 指定读取块的大小
skip_footer 文件末尾需要忽略的行数
verbose 输出各种解析输出的信息
encoding 文件编码
squeeze 如果解析的数据只包含一列,则返回一个series
thousands 千数量的分隔符
详细的read_csv函数说明请参见这里:pandas.read_csv
处理无效值
现实世界并非完美,我们读取到的数据常常会带有一些无效值。如果没有处理好这些无效值,将对程序造成很大的干扰。
对待无效值,主要有两种处理方法:直接忽略这些无效值;或者将无效值替换成有效值。
下面我先创建一个包含无效值的数据结构。然后通过pandas.isna函数来确认哪些值是无效的:
# process_na.py import pandas as pd import numpy as np df = pd.dataframe([[1.0, np.nan, 3.0, 4.0], [5.0, np.nan, np.nan, 8.0], [9.0, np.nan, np.nan, 12.0], [13.0, np.nan, 15.0, 16.0]]) print("df:\n{}\n".format(df)); print("df:\n{}\n".format(pd.isna(df)));****
这段代码输出如下:
df: 0 1 2 3 0 1.0 nan 3.0 4.0 1 5.0 nan nan 8.0 2 9.0 nan nan 12.0 3 13.0 nan 15.0 16.0 df: 0 1 2 3 0 false true false false 1 false true true false 2 false true true false 3 false true false false
忽略无效值
我们可以通过pandas.dataframe.dropna函数抛弃无效值:
# process_na.py print("df.dropna():\n{}\n".format(df.dropna()));
注:dropna默认不会改变原先的数据结构,而是返回了一个新的数据结构。如果想要直接更改数据本身,可以在调用这个函数的时候传递参数 inplace = true。
对于原先的结构,当无效值全部被抛弃之后,将不再是一个有效的dataframe,因此这行代码输出如下:
df.dropna(): empty dataframe columns: [0, 1, 2, 3] index: []
我们也可以选择抛弃整列都是无效值的那一列:
# process_na.py print("df.dropna(axis=1, how='all'):\n{}\n".format(df.dropna(axis=1, how='all')));
注:axis=1表示列的轴。how可以取值'any'或者'all',默认是前者。
这行代码输出如下:
df.dropna(axis=1, how='all'): 0 2 3 0 1.0 3.0 4.0 1 5.0 nan 8.0 2 9.0 nan 12.0 3 13.0 15.0 16.0
替换无效值
我们也可以通过fillna函数将无效值替换成为有效值。像这样:
# process_na.py print("df.fillna(1):\n{}\n".format(df.fillna(1)));
这段代码输出如下:
df.fillna(1): 0 1 2 3 0 1.0 1.0 3.0 4.0 1 5.0 1.0 1.0 8.0 2 9.0 1.0 1.0 12.0 3 13.0 1.0 15.0 16.0
将无效值全部替换成同样的数据可能意义不大,因此我们可以指定不同的数据来进行填充。为了便于操作,在填充之前,我们可以先通过rename方法修改行和列的名称:
# process_na.py df.rename(index={0: 'index1', 1: 'index2', 2: 'index3', 3: 'index4'}, columns={0: 'col1', 1: 'col2', 2: 'col3', 3: 'col4'}, inplace=true); df.fillna(value={'col2': 2}, inplace=true) df.fillna(value={'col3': 7}, inplace=true) print("df:\n{}\n".format(df));
这段代码输出如下:
df: col1 col2 col3 col4 index1 1.0 2.0 3.0 4.0 index2 5.0 2.0 7.0 8.0 index3 9.0 2.0 7.0 12.0 index4 13.0 2.0 15.0 16.0
处理字符串
数据中常常牵涉到字符串的处理,接下来我们就看看pandas对于字符串操作。
series的str字段包含了一系列的函数用来处理字符串。并且,这些函数会自动处理无效值。
下面是一些实例,在第一组数据中,我们故意设置了一些包含空格字符串:
# process_string.py import pandas as pd s1 = pd.series([' 1', '2 ', ' 3 ', '4', '5']); print("s1.str.rstrip():\n{}\n".format(s1.str.lstrip())) print("s1.str.strip():\n{}\n".format(s1.str.strip())) print("s1.str.isdigit():\n{}\n".format(s1.str.isdigit()))
在这个实例中我们看到了对于字符串strip的处理以及判断字符串本身是否是数字,这段代码输出如下:
s1.str.rstrip(): 0 1 1 2 2 3 3 4 4 5 dtype: object s1.str.strip(): 0 1 1 2 2 3 3 4 4 5 dtype: object s1.str.isdigit(): 0 false 1 false 2 false 3 true 4 true dtype: bool
下面是另外一些示例,展示了对于字符串大写,小写以及字符串长度的处理:
# process_string.py s2 = pd.series(['stairway to heaven', 'eruption', 'freebird', 'comfortably numb', 'all along the watchtower']) print("s2.str.lower():\n{}\n".format(s2.str.lower())) print("s2.str.upper():\n{}\n".format(s2.str.upper())) print("s2.str.len():\n{}\n".format(s2.str.len()))
该段代码输出如下:
s2.str.lower(): 0 stairway to heaven 1 eruption 2 freebird 3 comfortably numb 4 all along the watchtower dtype: object s2.str.upper(): 0 stairway to heaven 1 eruption 2 freebird 3 comfortably numb 4 all along the watchtower dtype: object s2.str.len(): 0 18 1 8 2 8 3 16 4 24 dtype: int64
相关推荐:
python数据分析库pandas基本操作方法_python
python之numpy和pandas的使用介绍
以上就是python 数据处理库 pandas 入门的详细内容。
其它类似信息

推荐信息