Python进阶第三方库之Pandas

了解NumpyPandas的不同
说明PandasSeriesDataframe两种结构的区别
了解PandasMultiIndexpanel结构
应用Pandas实现基本数据操作
应用Pandas实现数据的合并
应用crosstabpivot_table实现交叉表与透视表
应用groupby和聚合函数实现数据的分组与聚合
了解Pandasplot画图功能
应用Pandas实现数据的读取和存储

Pandas介绍

2008WesMcKinney开发出的库
专门用于数据挖掘的开源python
Numpy为基础,借力Numpy模块在计算方面性能高的优势
基于matplotlib,能够简便的画图
独特的数据结构

为什么使用Pandas

Numpy已经能够帮助我们处理数据,能够结合matplotlib解决部分数据展示等问题,那么pandas学习的目的在什么地方呢?
增强图表可读性
回忆我们在numpy当中创建学生成绩表样式:
返回结果:
array([[92, 55, 78, 50, 50],
[71, 76, 50, 48, 96],
[45, 84, 78, 51, 68],
[81, 91, 56, 54, 76],
[86, 66, 77, 67, 95],
[46, 86, 56, 61, 99],
[46, 95, 44, 46, 56],
[80, 50, 45, 65, 57],
[41, 93, 90, 41, 97],
[65, 83, 57, 57, 40]])
如果数据展示为这样,可读性就会更友好:
便捷的数据处理能力
读取文件方便
封装了MatplotlibNumpy的画图和计算

Pandas数据结构

Series

Series是一个类似于一维数组的数据结构,它能够保存任何类型的数据,比如整数、字符串、浮点数等,主要由一组数据和与之相关的索引两部分构成

Series的创建

# 导入pandas
import pandas as pd
pd.Series(data=None, index=None, dtype=None)
参数:
data:传入的数据,可以是ndarraylist
index:索引,必须是唯一的,且与数据的长度相等。如果没有传入索引参数,则默认会自动创建一个从0-N的整数索引。
dtype:数据的类型
通过已有数据创建
指定内容,默认索引
pd.Series(np.arange(10))
# 运行结果
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
dtype: int64

指定索引

pd.Series([6.7,5.6,3,10,2], index=[1,2,3,4,5])
# 运行结果
1 6.7
2 5.6
3 3.0
4 10.0
5 2.0
dtype: float64

通过字典数据创建

color_count = pd.Series({'red':100, 'blue':200, 'green': 500, 'yellow':1000})
color_count
# 运行结果
blue 200
green 500
red 100
yellow 1000
dtype: int64

Series的属性

index
color_count.index
# 结果
Index(['blue', 'green', 'red', 'yellow'], dtype='object')
values
color_count.values
# 结果
array([ 200, 500, 100, 1000])
也可以使用索引来获取数据:
color_count[2]
# 结果
100

DataFrame

DataFrame是一个类似于二维数组或表格(excel)的对象,既有行索引,又有列索引
行索引,表名不同行,横向索引,叫index0轴,axis=0
列索引,表名不同列,纵向索引,叫columns1轴,axis=1

DataFrame的创建

# 导入pandas
import pandas as pd
pd.DataFrame(data=None, index=None, columns=None)
参数:
index:行标签。如果没有传入索引参数,则默认会自动创建一个从0-N的整数索引。
columns:列标签。如果没有传入索引参数,则默认会自动创建一个从0-N的整数索引。
通过已有数据创建
pd.DataFrame(np.random.randn(2,3))
# 生成10名同学,5门功课的数据
score = np.random.randint(40, 100, (10, 5))
# 结果
array([[92, 55, 78, 50, 50],
[71, 76, 50, 48, 96],
[45, 84, 78, 51, 68],
[81, 91, 56, 54, 76],
[86, 66, 77, 67, 95],
[46, 86, 56, 61, 99],
[46, 95, 44, 46, 56],
[80, 50, 45, 65, 57],
[41, 93, 90, 41, 97],
[65, 83, 57, 57, 40]])
# 使用Pandas中的数据结构
score_df = pd.DataFrame(score)

Python进阶第三方库之Pandas_第1张图片

增加行、列索引

# 构造行索引序列
subjects = ["语文", "数学", "英语", "政治", "体育"]
# 构造列索引序列
stu = ['同学' + str(i) for i in range(score_df.shape[0])]
# 添加行索引
data = pd.DataFrame(score, columns=subjects, index=stu)

DataFrame的属性

shape
data.shape
# 结果
(10, 5)
index
DataFrame的行索引列表
data.index
# 结果
Index(['同学0', '同学1', '同学2', '同学3', '同学4', '同学5', '同学6', '同学7', '同学8', '同学9'], dtype='object')
columns
DataFrame的列索引列表
data.columns
# 结果
Index(['语文', '数学', '英语', '政治', '体育'], dtype='object')
values
直接获取其中array的值
data.values
array([[92, 55, 78, 50, 50],
[71, 76, 50, 48, 96],
[45, 84, 78, 51, 68],
[81, 91, 56, 54, 76],
[86, 66, 77, 67, 95],
[46, 86, 56, 61, 99],
[46, 95, 44, 46, 56],
[80, 50, 45, 65, 57],
[41, 93, 90, 41, 97],
[65, 83, 57, 57, 40]])
T
转置
data.T

DatatFrame索引的设置

修改行列索引值

stu = ["学生_" + str(i) for i in range(score_df.shape[0])]
# 必须整体全部修改
data.index = stu
注意:以下修改方式是错误的
# 错误修改方式
data.index[3] = '学生_3'

重设索引

reset_index(drop=False)
设置新的下标索引
drop:默认为False,不删除原来索引,如果为True,删除原来的索引值
# 重置索引,drop=False
data.reset_index()
# 重置索引,drop=True
data.reset_index(drop=True)

以某列值设置为新的索引

set_index(keys, drop=True)
keys : 列索引名成或者列索引名称的列表
drop : boolean, default True.当做新的索引,删除原来的列
df = pd.DataFrame({'month': [1, 4, 7, 10],
'year': [2012, 2014, 2013, 2014],
'sale':[55, 40, 84, 31]})
        month         sale         year
0         1               55           2012
1         4               40           2014
2         7               84           2013
3         10             31           2014
以月份设置新的索引
df.set_index('month')
sale year
month
1 55 2012
4 40 2014
7 84 2013
10 31 2014

你可能感兴趣的:(python,pandas,开发语言)