pandas is a Python language software package. When we use Python language for machine learning programming, this is a very commonly used basic programming library. This article is an introductory tutorial on the Python data processing library pandas. It is very good. Friends who are interested should take a look.
pandas is a Python language software package. When we use Python language for machine learning programming, this It is a very commonly used basic programming library. This article is an introductory tutorial to it.
pandas provides fast, flexible and expressive data structures designed to make working with "relational" or "tagged" data simple and intuitive. It is intended to be a high-level building block for practical data analysis in Python.
Getting Started
pandas is suitable for many different types of data, including:
Tabular data with heterogeneous type columns, such as SQL tables or Excel data
Ordered and unordered (not necessarily fixed frequency) time series data.
Arbitrary matrix data (uniform type or different types) with row and column labels
Any other form of observation/statistical data set.
Since this is a Python language software package, you need to have a Python language environment on your machine first. . Regarding this, please search the Internet for how to obtain it yourself.
For how to obtain pandas, please refer to the instructions on the official website: pandas Installation.
Normally, we can perform installation through pip:
1 |
|
or through conda Install pandas:
1 |
|
Currently (February 2018) the latest version of pandas is v0.22.0 (release time: December 29, 2017).
I have put the source code and test data of this article on Github: pandas_tutorial, readers can get it.
In addition, pandas is often used together with NumPy, and NumPy is also used in the source code in this article.
It is recommended that readers have some familiarity with NumPy before learning pandas. I have also written a basic tutorial on NumPy before, see here: Python Machine Learning Library NumPy Tutorial
Core Data Structure
The core of pandas are the two data structures Series
and DataFrame
.
The comparison of these two types of data structures is as follows:
DataFrame can be regarded as a container of Series, that is: a DataFrame can contain several Series .
Note: Before version 0.20.0, there was a three-dimensional data structure named Panel. This is also the reason why the pandas library is named: pan-da-s. However, this data structure has been abandoned because it is rarely used.
Series
Since Series is a one-dimensional structured data, we can create this kind of data directly through an array , like this:
1 2 3 4 5 |
|
The output of this code is as follows:
1 2 3 4 5 6 |
|
This paragraph The output description is as follows:
The last line of output is the type of data in the Series, and the data here are all int64 types.
The data is output in the second column, and the first column is the index of the data, which is called Index in pandas.
We can print out the data and index in the Series respectively:
1 2 3 |
|
The output of these two lines of code is as follows:
1 2 |
|
If not specified (like above), the index is [1, N- 1] form. However, we can also specify the index when creating the Series. The index does not necessarily need to be an integer, it can be any type of data, such as a string. For example, we use seven letters to map seven musical notes. The purpose of the index is to obtain the corresponding data, such as the following:
1 2 3 4 5 |
|
The output of this code is as follows:
1 2 3 4 5 6 7 8 9 10 11 |
|
Let’s take a look at the creation of DataFrame. We can create a 4x4 matrix through the NumPy interface to create a DataFrame, like this:
1 2 3 |
|
The output of this code is as follows:
1 2 3 4 5 6 |
|
We can see from this output that the default index and column names are in the form [0, N-1].
We can specify column names and indexes when creating a DataFrame, like this:
1 2 3 4 5 |
|
The output of this code is as follows:
1 2 3 4 5 6 |
|
We can also directly specify column data to create a DataFrame:
1 2 3 4 5 |
|
The output of this code is as follows :
1 2 3 4 5 6 7 8 9 |
|
Please note:
Different columns of DataFrame can be different data types
If you create a DataFrame with a Series array, each Series will become a row instead of a column
For example:
1 2 3 4 5 6 7 |
|
df4的输出如下:
1 2 3 4 |
|
我们可以通过下面的形式给DataFrame添加或者删除列数据:
1 2 3 4 5 |
|
这段代码输出如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
Index对象与数据访问
pandas的Index对象包含了描述轴的元数据信息。当创建Series或者DataFrame的时候,标签的数组或者序列会被转换成Index。可以通过下面的方式获取到DataFrame的列和行的Index对象:
1 2 3 |
|
这两行代码输出如下:
1 2 3 4 |
|
请注意:
Index并非集合,因此其中可以包含重复的数据
Index对象的值是不可以改变,因此可以通过它安全的访问数据
DataFrame提供了下面两个操作符来访问其中的数据:
loc:通过行和列的索引来访问数据
iloc:通过行和列的下标来访问数据
例如这样:
1 2 3 |
|
第一行代码访问了行索引为0和1,列索引为“note”的元素。第二行代码访问了行下标为0和1(对于df3来说,行索引和行下标刚好是一样的,所以这里都是0和1,但它们却是不同的含义),列下标为0的元素。
这两行代码输出如下:
1 2 3 4 5 6 7 8 9 |
|
文件操作
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可以这样完成安装:
1 |
|
安装完之后可以通过pip查看这个库的信息:
1 2 3 4 5 6 7 8 9 10 |
|
接下来我们看一个读取Excel的简单的例子:
1 2 3 4 5 |
|
这个Excel的内容如下:
1 2 3 4 5 6 7 8 |
|
注:本文的代码和数据文件可以通过文章开头提到的Github仓库获取。
读取CSV文件
下面,我们再来看读取CSV文件的例子。
第一个CSV文件内容如下:
1 2 3 4 5 6 7 |
|
读取的方式也很简单:
1 2 3 |
|
我们再来看第2个例子,这个文件的内容如下:
1 2 3 4 5 6 7 |
|
严格的来说,这并不是一个CSV文件了,因为它的数据并不是通过逗号分隔的。在这种情况下,我们可以通过指定分隔符的方式来读取这个文件,像这样:
1 2 3 |
|
实际上,read_csv支持非常多的参数用来调整读取的参数,如下表所示:
Parameters | Description |
---|---|
NA
| |
parse_dates | Attempts to parse the data into datetime . Defaults to False |
keep_date_col | If concatenating a column to a parsed date, keep the concatenated column. Default is False . |
converters | Converters for columns |
dayfirst | When parsing dates that can cause ambiguity , stored in internal form.The default is False |
data_parser | Function used to parse dates |
nrows | The number of lines to read from the beginning of the file |
iterator | Returns a TextParser object for reading part of the content |
chunksize | Specify the size of the read chunk |
skip_footer | The number of lines to be ignored at the end of the file |
verbose | Output various parsing output information |
encoding | File encoding |
squeeze | If the parsed data contains only one column, a Series |
thousands | thousands delimiter is returned |
详细的read_csv函数说明请参见这里:pandas.read_csv
处理无效值
现实世界并非完美,我们读取到的数据常常会带有一些无效值。如果没有处理好这些无效值,将对程序造成很大的干扰。
对待无效值,主要有两种处理方法:直接忽略这些无效值;或者将无效值替换成有效值。
下面我先创建一个包含无效值的数据结构。然后通过pandas.isna函数来确认哪些值是无效的:
1 2 3 4 5 6 7 8 9 |
|
这段代码输出如下:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
忽略无效值
我们可以通过pandas.DataFrame.dropna函数抛弃无效值:
1 2 |
|
注:dropna默认不会改变原先的数据结构,而是返回了一个新的数据结构。如果想要直接更改数据本身,可以在调用这个函数的时候传递参数 inplace = True。
对于原先的结构,当无效值全部被抛弃之后,将不再是一个有效的DataFrame,因此这行代码输出如下:
1 2 3 4 |
|
我们也可以选择抛弃整列都是无效值的那一列:
1 2 |
|
注:axis=1表示列的轴。how可以取值'any'或者'all',默认是前者。
这行代码输出如下:
1 2 3 4 5 6 |
|
替换无效值
我们也可以通过fillna函数将无效值替换成为有效值。像这样:
1 2 |
|
这段代码输出如下:
1 2 3 4 5 6 |
|
将无效值全部替换成同样的数据可能意义不大,因此我们可以指定不同的数据来进行填充。为了便于操作,在填充之前,我们可以先通过rename方法修改行和列的名称:
1 2 3 4 5 6 7 8 |
|
这段代码输出如下:
1 2 3 4 5 6 |
|
处理字符串
数据中常常牵涉到字符串的处理,接下来我们就看看pandas对于字符串操作。
Series的str字段包含了一系列的函数用来处理字符串。并且,这些函数会自动处理无效值。
下面是一些实例,在第一组数据中,我们故意设置了一些包含空格字符串:
1 2 3 4 5 6 |
|
在这个实例中我们看到了对于字符串strip的处理以及判断字符串本身是否是数字,这段代码输出如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
下面是另外一些示例,展示了对于字符串大写,小写以及字符串长度的处理:
1 2 3 4 5 6 |
|
该段代码输出如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
相关推荐:
Python数据分析库pandas基本操作方法_python
The above is the detailed content of Getting started with Python data processing library pandas. For more information, please follow other related articles on the PHP Chinese website!