Python For Data Analysis -- Pandas

简介:

首先pandas的作者就是这本书的作者 
对于Numpy,我们处理的对象是矩阵 
pandas是基于numpy进行封装的,pandas的处理对象是二维表(tabular, spreadsheet-like),和矩阵的区别就是,二维表是有元数据的 
用这些元数据作为index更方便,而Numpy只有整形的index,但本质是一样的,所以大部分操作是共通的

大家碰到最多的二维表应用,关系型数据库中的表,有列名和行号,这些就是元数据 
当然你可以用抽象的矩阵来对这些二维表做统计,但使用pandas会更方便

 

Introduction to pandas Data Structures

Series

A Series is a one-dimensional array-like object containing an array of data (of any NumPy data type) and an associated array of data labels, called its index. 
简单的理解,就是字典,或一维表;不显式指定index时,会自动添加 0 through N - 1的整数作为index

image

image

这里可以简单的替换index,生成新的series,

image

大家想想,对于Numpy而言,没有显式的指定index,但也是可以通过整形的index取到数据的,这里的index其实本质上和numpy的整形index是一样的 
所以对于Numpy的操作,也同样适用于pandas

image

image

同时,上面说了series其实就是字典,所以也可以用python字典来初始化

image

 

DataFrame

A DataFrame represents a tabular, spreadsheet-like data structure containing an ordered collection of columns, each of which can be a different value type (numeric, string, boolean, etc.).

如果接触过R,应该对DataFrame很熟悉,其实pandas就从某种程度上模拟出R的一些功能 
所以如果用python也可以像R一样方便的做统计,那何必要再去用R

上面Series是字典或一维表, 
DataFrame是二维表,也可以看作是series的字典

image

image

指定了列名,行名是自动生成的

同时也可以指定行名,这里增加了debt列,但是没有数据,所以是NaN

image

可以为debt,赋值

image

取行,用ix

image

也可以用嵌套字典来创建Dataframe,其实是series的字典,series本身就是字典,所以就是嵌套的字典

image

image

可以像numpy矩阵一样,转置

image

 

Essential Functionality

下面看看到底pandas在这些数据结构上提供了哪些方便的functions

Reindexing

A critical method on pandas objects is reindex, which means to create a new object with the data conformed to a new index.

其实就是更改indexing

image

增加e,并默认填上0

image

还可以通过method参数,来指定填充方式

image

可以选择向前或向后填充

image

对于二维表,可以在index和columns上同时进行reindex

image

image

image

reindex的参数,

image

 

Dropping entries from an axis

用axis指定维度,对于二维表,行是0,列是1

image

 

Indexing, selection, and filtering

基本和Numpy差不多

image

 

Arithmetic and data alignment

数据对齐和自动填充是pandas比较方便的一点

In [136]: df1 = DataFrame(np.arange(12.).reshape((3, 4)), columns=list('abcd')) 
In [137]: df2 = DataFrame(np.arange(20.).reshape((4, 5)), columns=list('abcde'))

image

可以看到默认情况下,只有两个df都有的情况下,才会相加,否则为NaN 
我觉得大部分情况,应该是希望有一个就加一个,即把没有的初始化为0

image

除了add,还支持

image

 

Function application and mapping

1. Element-wise:NumPy ufuncs (element-wise array methods) work fine with pandas objects:

image

另一种element-wise,使用applymap

image

 

2. 可以将func apply到每一行或每一列

image

比较复杂的case

image

image

 

3.对于某个行或列,即series进行map

image

 

Summarizing and Computing Descriptive Statistics

提供很多类似R的统计函数,

image

image

image

image

提供类似R中的descirbe,很方便

image

对非数值型,执行describe

image

汇总表,

image

 

Correlation and Covariance,相关系数和协方差

image

对MSFT和IBM之间求相关系数和协方差

image

也可以求出相关系数矩阵和协方差矩阵

image

 

Unique Values, Value Counts, and Membership

In [217]: obj = Series(['c', 'a', 'd', 'a', 'a', 'b', 'b', 'c', 'c'])

In [218]: uniques = obj.unique() 
In [219]: uniques 
Out[219]: array([c, a, d, b], dtype=object)

In [220]: obj.value_counts() 
Out[220]: 
c 3 
a 3 
b 2 
d 1

image

 

Handling Missing Data

提供一些用于处理missing data的工具函数

image

其中fillna复杂些,

image

image

image

 

Hierarchical Indexing

Hierarchical indexing is an important feature of pandas enabling you to have multiple (two or more) index levels on an axis. Somewhat abstractly, it provides a way for you to work with higher dimensional data in a lower dimensional form.

可以使用多层分级的index,其实本质等同于增加一维,所以相当于用低维来模拟高维数据

image

image

image

并且是支持,通过unstack和stack来还原多维数据的

image

image

 

Pandas还提供其他功能,尤其是ETL功能,方便数据处理

比如和各种文件读入和写出的功能

cleaning, transform(基于map), merge(join)……


本文章摘自博客园,原文发布日期:2014-08-12

目录
相关文章
|
5天前
|
数据采集 SQL 数据处理
Python中的Pandas库:数据处理与分析的利器
Python中的Pandas库:数据处理与分析的利器
17 0
|
6天前
|
数据采集 数据挖掘 数据处理
Python数据分析实战:使用Pandas处理Excel文件
Python数据分析实战:使用Pandas处理Excel文件
76 0
|
6天前
|
数据采集 数据可视化 数据处理
Python中的高效数据处理:Pandas库详解
Python中的高效数据处理:Pandas库详解
27 2
|
6天前
|
数据采集 SQL 数据可视化
使用Python和Pandas库进行数据分析的入门指南
使用Python和Pandas库进行数据分析的入门指南
69 0
|
6天前
|
数据采集 SQL 存储
使用Python和Pandas进行数据分析
使用Python和Pandas进行数据分析
20 0
|
8天前
|
存储 分布式计算 数据处理
使用Python和Pandas处理大型数据集的高效策略
随着大数据时代的到来,处理大型数据集已成为数据分析师和数据科学家的日常任务。本文旨在探讨如何使用Python的Pandas库高效地处理大型数据集。不同于常规的数据处理教程,本文将重点介绍数据子集化、内存优化、并行处理和数据压缩等高级策略,帮助读者在资源受限的环境中快速且准确地分析大量数据。
|
8天前
|
数据采集 数据挖掘 Serverless
利用Python和Pandas库优化数据清洗流程
在数据分析项目中,数据清洗是至关重要的一步。传统的数据清洗方法往往繁琐且易出错。本文将介绍如何利用Python编程语言中的Pandas库,通过其强大的数据处理能力,实现高效、自动化的数据清洗流程。我们将探讨Pandas库在数据清洗中的应用,包括缺失值处理、重复值识别、数据类型转换等,并通过一个实际案例展示如何利用Pandas优化数据清洗流程,提升数据质量。
|
11天前
|
Python
python相关库的安装:pandas,numpy,matplotlib,statsmodels
python相关库的安装:pandas,numpy,matplotlib,statsmodels
|
13天前
|
数据采集 数据可视化 数据处理
利用Python和Pandas库实现高效的数据处理与分析
在大数据和人工智能时代,数据处理与分析已成为不可或缺的一环。Python作为一门强大的编程语言,结合Pandas库,为数据科学家和开发者提供了高效、灵活的数据处理工具。本文将介绍Pandas库的基本功能、优势,并通过实际案例展示如何使用Pandas进行数据清洗、转换、聚合等操作,以及如何利用Pandas进行数据可视化,旨在帮助读者深入理解并掌握Pandas在数据处理与分析中的应用。
|
14天前
|
架构师 数据挖掘 Python
最全pandas库(Python),2024年最新阿里云架构师面试
最全pandas库(Python),2024年最新阿里云架构师面试
最全pandas库(Python),2024年最新阿里云架构师面试