您的位置:首页 > 编程语言 > Python开发

python之pandas学习

2017-02-05 17:11 369 查看
Python中的pandas模块进行数据分析。

接下来pandas介绍中将学习到如下8块内容:

1、数据结构简介:DataFrame和Series

2、数据索引index

3、利用pandas查询数据

4、利用pandas的DataFrames进行统计分析

5、利用pandas实现SQL操作

6、利用pandas进行缺失值的处理

7、利用pandas实现Excel的数据透视表功能

8、多层索引的使用

一、数据结构介绍

在pandas中有两类非常重要的数据结构,即序列Series和数据框DataFrame。Series类似于numpy中的一维数组,除了通吃一维数组可用的函数或方法,而且其可通过索引标签的方式获取数据,还具有索引的自动对齐功能;DataFrame类似于numpy中的二维数组,同样可以通用numpy数组的函数和方法,而且还具有其他灵活应用,后续会介绍到。

1、Series的创建

序列的创建主要有三种方式:

1)通过一维数组创建序列

import numpy as np, pandas as pd
arr1 = np.arange(10)
print(arr1)
print(type(arr1))
s1 = pd.Series(arr1)
print(s1)
print(type(s1))


实验结果:

[0 1 2 3 4 5 6 7 8 9]
<class 'numpy.ndarray'>
0    0
1    1
2    2
3    3
4    4
5    5
6    6
7    7
8    8
9    9
dtype: int32
<class 'pandas.core.series.Series'>


2)通过字典的方式创建序列

dic1 = {'a':10,'b':20,'c':30,'d':40,'e':50}
print(dic1)
print(type(dic1))
s2 = pd.Series(dic1)
print(s2)
print(type(s2))


实验结果:

{'c': 30, 'b': 20, 'e': 50, 'a': 10, 'd': 40}
<class 'dict'>
a    10
b    20
c    30
d    40
e    50
dtype: int64
<class 'pandas.core.series.Series'>


3)通过DataFrame中的某一行或某一列创建序列

这部分内容我们放在后面讲,因为下面就开始将DataFrame的创建。

2、DataFrame的创建

数据框的创建主要有三种方式:

1)通过二维数组创建数据框

import numpy as np, pandas as pd
arr2 = np.array(np.arange(12)).reshape(4,3)
print(arr2)
print(type(arr2))
df1 = pd.DataFrame(arr2)
print(df1)
print(type(df1))


实验结果:

[[ 0  1  2]
[ 3  4  5]
[ 6  7  8]
[ 9 10 11]]
<class 'numpy.ndarray'>
0   1   2
0  0   1   2
1  3   4   5
2  6   7   8
3  9  10  11
<class 'pandas.core.frame.DataFrame'>


2)通过字典的方式创建数据框

以下以两种字典来创建数据框,一个是字典列表,一个是嵌套字典。

dic2 = {'a':[1,2,3,4],'b':[5,6,7,8],
'c':[9,10,11,12],'d':[13,14,15,16]}
print(dic2)
print(type(dic2))
df2 = pd.DataFrame(dic2)
print(df2)
print(type(df2))
dic3 = {'one':{'a':1,'b':2,'c':3,'d':4},
'two':{'a':5,'b':6,'c':7,'d':8},
'three':{'a':9,'b':10,'c':11,'d':12}}
print(dic3)
print(type(dic3))
df3 = pd.DataFrame(dic3)
print(df3)
print(type(df3))


实验结果:

{'d': [13, 14, 15, 16], 'b': [5, 6, 7, 8], 'a': [1, 2, 3, 4], 'c': [9, 10, 11, 12]}
<class 'dict'>
a  b   c   d
0  1  5   9  13
1  2  6  10  14
2  3  7  11  15
3  4  8  12  16
<class 'pandas.core.frame.DataFrame'>
{'two': {'d': 8, 'b': 6, 'a': 5, 'c': 7}, 'one': {'d': 4, 'b': 2, 'a': 1, 'c': 3}, 'three': {'d': 12, 'b': 10, 'a': 9, 'c': 11}}
<class 'dict'>
one  three  two
a    1      9    5
b    2     10    6
c    3     11    7
d    4     12    8
<class 'pandas.core.frame.DataFrame'>


3)通过数据框的方式创建数据框

dic3 = {'one':{'a':1,'b':2,'c':3,'d':4},
'two':{'a':5,'b':6,'c':7,'d':8},
'three':{'a':9,'b':10,'c':11,'d':12}}
print(dic3)
print(type(dic3))
df3 = pd.DataFrame(dic3)
# print(df3)
# print(type(df3))
df4 = df3[['one','three']]
print(df4)
print(type(df4))
s3 = df3['one']
print(s3)
print(type(s3))


实验结果:

{'one': {'d': 4, 'b': 2, 'a': 1, 'c': 3}, 'three': {'d': 12, 'b': 10, 'a': 9, 'c': 11}, 'two': {'d': 8, 'b': 6, 'a': 5, 'c': 7}}
<class 'dict'>
one  three
a    1      9
b    2     10
c    3     11
d    4     12
<class 'pandas.core.frame.DataFrame'>
a    1
b    2
c    3
d    4
Name: one, dtype: int64
<class 'pandas.core.series.Series'>


pandas模块为我们提供了非常多的描述性统计分析的指标函数,如总和、均值、最小值、最大值等,我们来具体看看这些函数:

首先随机生成三组数据

np.random.seed(1234)
d1 = pd.Series(2*np.random.normal(size = 10)+3)
print(d1)
d2 = np.random.f(2,4,size = 10)
print(d2)
d3 = np.random.randint(1,100,size = 10)
print(d3)
print(d1.count()) #非空元素计算
print(d1.min()) #最小值
print(d1.max()) #最大值
print(d1.idxmin()) #最小值的位置,类似于R中的which.min函数
print(d1.idxmax()) #最大值的位置,类似于R中的which.max函数
print(d1.quantile(0.1)) #10%分位数
print(d1.sum()) #求和
print(d1.mean()) #均值
print(d1.median()) #中位数
print(d1.mode()) #众数
print(d1.var()) #方差
print(d1.std()) #标准差
print(d1.mad()) #平均绝对偏差
print(d1.skew()) #偏度
print(d1.kurt()) #峰度
print(d1.describe()) #一次性输出多个描述性统计指标


实验结果:

0    3.942870
1    0.618049
2    5.865414
3    2.374696
4    1.558823
5    4.774326
6    4.719177
7    1.726953
8    3.031393
9   -1.485370
dtype: float64
[ 2.95903083  0.32784914  2.27321231  0.05147861  9.10291941  0.15691116
0.99021894  1.84169938  0.32196418  0.04276792]
[57 71 57 87 45 91 84 48 50 19]
10
-1.48536990837
5.86541393685
9
2
0.4077067586912829
27.1263301511
2.71263301511
2.703044476022716
Series([], dtype: float64)
4.91771912101
2.21759309185
1.75400292821
-0.453758801844
-0.116260760058
count    10.000000
mean      2.712633
std       2.217593
min      -1.485370
25%       1.600855
50%       2.703044
75%       4.525100
max       5.865414
dtype: float64
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: