数据分析与数据科学必备技能之——Pandas使用(一)

本次主要总结归纳了Pandas常用函数方法,分两节归纳概括,内容后期有待补充。
首先介绍前7节内容,之后会介绍8节至15节内容

import pandas as pd 
import numpy as np
import matplotlib as plt
import math

1. Pandas数据结构

数据结构 维数 说明
Series 1 序列/系列;是具有均匀数据、尺寸大小不变、数据可变的一维数组结构
DataFrame 2 数据框/数据帧;是具有异构数据、大小可变、数据可变的二维数组结构,即数据以行和列的表格方式排列
Panel 3 面板;具有异构数据、大小可变、数据可变的三维数据结构

2. Pandas创建对象

2.1 pd.Series对象创建

创建函数 参数 描述
pd.Series( data, index, dtype, copy) data
index
dtype
copy
数据采取各种形式,如:ndarray,list,constants,dict;
索引值必须是唯一的和散列的,与数据的长度相同。如果没有索引被传递默认np.arange(n);
dtype用于数据类型。如果没有,将推断数据类型;
复制数据,默认为false;
# 创建简单的序列
# 创建一个空的系列
pd.Series()
# 从ndarray创建一个系列
data=np.array(['a','s','d','f'])
pd.Series(data)
pd.Series(data,index=[101,102,103,104])
# 从字典创建一个系列
data = {'a' : 0., 'b' : 1., 'c' : 2.}
pd.Series(data)
pd.Series(data,index=['b','c','d','a'])
# 从标量创建一个系列
pd.Series(5, index=['a','b','c','d'])

Series([], dtype: float64)
0 a
1 s
2 d
3 f
dtype: object

101 a
102 s
103 d
104 f
dtype: object

a 0.0
b 1.0
c 2.0
dtype: float64

b 1.0
c 2.0
d NaN
a 0.0
dtype: float64

a 5
b 5
c 5
d 5
dtype: int64

# 访问序列中数据(与访问numpy中元素一致)
s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])
s[0] # 等价于 s['a']
s[:3] # 等价于 s[['a','b','c']]
s[3:]
s[1:3]

1

a 1
b 2
c 3
dtype: int64

d 4
e 5
dtype: int64

b 2
c 3
dtype: int64

2.2 pd.DataFrame对象创建

创建函数 参数 描述
pd.pandas(data, index, columns, dtype, copy) data
index
columns
dtype
copy
数据采取各种形式,如:ndarray,series,map,lists,dict,constant和另一个DataFrame;
对于行标签,要用于结果帧的索引是可选缺省值np.arrange(n),如果没有传递索引值;
对于列标签,可选的默认语法是 - np.arange(n)。 如果没有传递索引值;
每列的数据类型;
如果默认值为False,则此命令(或任何它)用于复制数据;
如果默认值为False,则此命令(或任何它)用于复制数据;
# 创建简单的数据框
# 创建一个空的DataFrame
pd.DataFrame()
# 从列表创建DataFrame
data = [1,2,3]
pd.DataFrame(data)
data = [['Alex',10],['Bob',12],['Clarke',13]]
pd.DataFrame(data,columns=['Name','Age'],dtype=float)
0
0 1
1 2
2 3
Name Age
0 Alex 10.0
1 Bob 12.0
2 Clarke 13.0
# 从ndarrays/Lists的字典来创建DataFrame
data = {'Name':['Tom', 'Jack', 'Steve', 'Ricky'],'Age':[28,34,29,42]}
pd.DataFrame(data)
pd.DataFrame(data, index=['rank1','rank2','rank3','rank4'])
Age Name
0 28 Tom
1 34 Jack
2 29 Steve
3 42 Ricky
Age Name
rank1 28 Tom
rank2 34 Jack
rank3 29 Steve
rank4 42 Ricky
# 从字典列表创建数据帧DataFrame
data = [{'a': 1, 'b': 2},{'a': 5, 'b': 10, 'c': 20}]
pd.DataFrame(data,index=['first', 'second'])
pd.DataFrame(data, index=['first', 'second'], columns=list('ab'))
pd.DataFrame(data, index=['first', 'second'], columns=['a', 'b1']) # columns可用两种方式表示
a b c
first 1 2 NaN
second 5 10 20.0
a b
first 1 2
second 5 10
a b1
first 1 NaN
second 5 NaN
# 从系列的字典来创建DataFrame
d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']),
      'two' : pd.Series([1, 2, 3, 4], index=list('abcd'))} # index可用两种方式表示
pd.DataFrame(d)
one two
a 1.0 1
b 2.0 2
c 3.0 3
d NaN 4

2.2.1 数据框数据的索引与选取

选择对象 方法
行列 df[]
区域 df.loc[ ],df.iloc[ ],df.ix[ ]
单元格 df.at[ ],df.iat[ ]
选择方法 说明
loc[ ] 根据index行标签或column列名称选取
iloc[ ] 基于行/列的position(行数列数)
at[ ] 根据指定行index及列label,快速定位DataFrame的元素
iat[ ] 与at类似,不同的是根据position来定位的
ix[ ] 为loc与iloc的混合体,既支持label也支持position
行列选取方法 维度 行列 操作类型
df[ ] 一维 行维度
列维度
整数切片、标签切片、<布尔数组>
标签索引、标签列表、Callable
# df[]的例子
df = pd.DataFrame(np.random.randn(6,4), index=list('abcdef'), columns=list('ABCD'))
df[:3] #整数切片 
# df[0]、df['a']报错,不能为整数索引,必须为整数切片
df['a':'c'] #标签切片 
# df[['a','c']]错误,不能为标签列表,必须为标签切片
df[[True,True,True,False,False,False]] # 前三行(布尔数组长度等于行数)
df[df['A']>0] # A列值大于0的行
df[(df['A']>0) & (df['C']>0)] # A列值大于0,并且C列大于0的行
A B C D
a -0.897015 -1.883345 -0.589268 1.215273
b 0.639703 0.982318 -0.785517 0.708336
c -0.402066 0.054401 -0.511191 1.703571
A B C D
a -0.897015 -1.883345 -0.589268 1.215273
b 0.639703 0.982318 -0.785517 0.708336
c -0.402066 0.054401 -0.511191 1.703571
A B C D
a -0.897015 -1.883345 -0.589268 1.215273
b 0.639703 0.982318 -0.785517 0.708336
c -0.402066 0.054401 -0.511191 1.703571
A B C D
b 0.639703 0.982318 -0.785517 0.708336
e 1.199448 0.555940 1.222145 -1.744408
f 0.623923 -0.388683 0.273779 0.468964
A B C D
e 1.199448 0.555940 1.222145 -1.744408
f 0.623923 -0.388683 0.273779 0.468964
df['A'] # 标签索引,等价于 df.A,
# df[0:3,'A'] / df['a':'c','A']错误,不能为切片,必须为索引
df[['A','C']] #标签列表
df[lambda df: df.columns[0]] # Callable

a -0.897015
b 0.639703
c -0.402066
d -1.296602
e 1.199448
f 0.623923
Name: A, dtype: float64

A C
a -0.897015 -0.589268
b 0.639703 -0.785517
c -0.402066 -0.511191
d -1.296602 0.488359
e 1.199448 1.222145
f 0.623923 0.273779

a -0.897015
b 0.639703
c -0.402066
d -1.296602
e 1.199448
f 0.623923
Name: A, dtype: float64

标签区域选取方法 维度 行列 操作类型
df.loc[] 二维 行维度
列维度
标签索引、标签切片、标签列表、<布尔数组>、Callable
标签索引、标签切片、标签列表、<布尔数组>、Callable
# df.loc[]的例子
df.loc['a', :] # 行标签索引,等价于 df.loc['a']\df.iloc[0]
df.loc['a':'d', :] # 行标签切片 等价于df.loc['a':'b']
df.loc[['a','b','c'], :] # 行标签列表
df.loc[[True,True,True,False,False,False], :] # 前三行(布尔数组长度等于行数)
df.loc[df.A<0, :] # A列大于0的列
df.loc[df.loc[:,'A']>0, :]
df.loc[df.iloc[:,0]>0, :]
df.loc[lambda _df: _df.A > 0, :]

A -0.897015
B -1.883345
C -0.589268
D 1.215273
Name: a, dtype: float64

A B C D
a -0.897015 -1.883345 -0.589268 1.215273
b 0.639703 0.982318 -0.785517 0.708336
c -0.402066 0.054401 -0.511191 1.703571
d -1.296602 -0.446351 0.488359 1.219049
A B C D
a -0.897015 -1.883345 -0.589268 1.215273
b 0.639703 0.982318 -0.785517 0.708336
c -0.402066 0.054401 -0.511191 1.703571
A B C D
a -0.897015 -1.883345 -0.589268 1.215273
b 0.639703 0.982318 -0.785517 0.708336
c -0.402066 0.054401 -0.511191 1.703571
A B C D
a -0.897015 -1.883345 -0.589268 1.215273
c -0.402066 0.054401 -0.511191 1.703571
d -1.296602 -0.446351 0.488359 1.219049
A B C D
b 0.639703 0.982318 -0.785517 0.708336
e 1.199448 0.555940 1.222145 -1.744408
f 0.623923 -0.388683 0.273779 0.468964
A B C D
b 0.639703 0.982318 -0.785517 0.708336
e 1.199448 0.555940 1.222145 -1.744408
f 0.623923 -0.388683 0.273779 0.468964
A B C D
b 0.639703 0.982318 -0.785517 0.708336
e 1.199448 0.555940 1.222145 -1.744408
f 0.623923 -0.388683 0.273779 0.468964
df.loc[:, 'A'] # 列标签索引
df.loc[:, 'A':'C'] #列切片
df.loc[:, ['A','B','C']]  #列列表
df.loc[:, [True,True,True,False]] # 前三列(布尔数组长度等于行数)
df.loc[:, df.loc['a']>0] # a行大于0的列
df.loc[:, df.iloc[0]>0] # 0行大于0的列
df.loc[:, lambda _df: ['A', 'B']] #Callable

a -0.897015
b 0.639703
c -0.402066
d -1.296602
e 1.199448
f 0.623923
Name: A, dtype: float64

A B C
a -0.897015 -1.883345 -0.589268
b 0.639703 0.982318 -0.785517
c -0.402066 0.054401 -0.511191
d -1.296602 -0.446351 0.488359
e 1.199448 0.555940 1.222145
f 0.623923 -0.388683 0.273779
A B C
a -0.897015 -1.883345 -0.589268
b 0.639703 0.982318 -0.785517
c -0.402066 0.054401 -0.511191
d -1.296602 -0.446351 0.488359
e 1.199448 0.555940 1.222145
f 0.623923 -0.388683 0.273779
A B C
a -0.897015 -1.883345 -0.589268
b 0.639703 0.982318 -0.785517
c -0.402066 0.054401 -0.511191
d -1.296602 -0.446351 0.488359
e 1.199448 0.555940 1.222145
f 0.623923 -0.388683 0.273779
D
a 1.215273
b 0.708336
c 1.703571
d 1.219049
e -1.744408
f 0.468964
D
a 1.215273
b 0.708336
c 1.703571
d 1.219049
e -1.744408
f 0.468964
A B
a -0.897015 -1.883345
b 0.639703 0.982318
c -0.402066 0.054401
d -1.296602 -0.446351
e 1.199448 0.555940
f 0.623923 -0.388683
df.A.loc[lambda s: s > 0] # 定位到标量元素
df.loc[['a','d'], ['A','B']] # 行列标签列表
df.loc['a':'c', 'A':'C'] # 行列标签切片
df.loc['a':'c', ['A','B']] # 混合
df.loc['a','A'] # 定位到标量元素

b 0.639703
e 1.199448
f 0.623923
Name: A, dtype: float64

A B
a -0.897015 -1.883345
d -1.296602 -0.446351
A B C
a -0.897015 -1.883345 -0.589268
b 0.639703 0.982318 -0.785517
c -0.402066 0.054401 -0.511191
A B
a -0.897015 -1.883345
b 0.639703 0.982318
c -0.402066 0.054401

-0.8970154528725585

整数选取方法 维度 行列 操作类型
df.iloc[] 二维 行维度
列维度
整数索引、整数切片、整数列表、<布尔数组>
整数索引、整数切片、整数列表、<布尔数组>、Callable
# df.iloc[]的例子
df.iloc[3, :] # 行整数索引 等价于df.iloc[3]
df.iloc[0:3, :] # 行整数切片 等价于df.iloc[0:3]
df.iloc[[0,2,4], :] # 行列表
df.iloc[[True,True,True,False,False,False], :] # 前三行(布尔数组长度等于行数)
#df.iloc[df['A']>0, :] #× 为什么不行呢?想不通!
#df.iloc[df.loc[:,'A']>0, :] #×
#df.iloc[df.iloc[:,0]>0, :] #×
df.iloc[lambda _df: [0, 1], :]

A -1.296602
B -0.446351
C 0.488359
D 1.219049
Name: d, dtype: float64

A B C D
a -0.897015 -1.883345 -0.589268 1.215273
b 0.639703 0.982318 -0.785517 0.708336
c -0.402066 0.054401 -0.511191 1.703571
A B C D
a -0.897015 -1.883345 -0.589268 1.215273
c -0.402066 0.054401 -0.511191 1.703571
e 1.199448 0.555940 1.222145 -1.744408
A B C D
a -0.897015 -1.883345 -0.589268 1.215273
b 0.639703 0.982318 -0.785517 0.708336
c -0.402066 0.054401 -0.511191 1.703571
A B C D
a -0.897015 -1.883345 -0.589268 1.215273
b 0.639703 0.982318 -0.785517 0.708336
df.iloc[:, 1] # 列索引
df.iloc[:, 0:3] # 列切片
df.iloc[:, [0,1,2]] # 列列表
df.iloc[:, [True,True,True,False]] # 前三列(布尔数组长度等于行数)
#df.iloc[:, df.loc['a']>0] #× why
#df.iloc[:, df.iloc[0]>0] #× why
df.iloc[:, lambda _df: [0, 1]]

a -1.883345
b 0.982318
c 0.054401
d -0.446351
e 0.555940
f -0.388683
Name: B, dtype: float64

A B C
a -0.897015 -1.883345 -0.589268
b 0.639703 0.982318 -0.785517
c -0.402066 0.054401 -0.511191
d -1.296602 -0.446351 0.488359
e 1.199448 0.555940 1.222145
f 0.623923 -0.388683 0.273779
A B C
a -0.897015 -1.883345 -0.589268
b 0.639703 0.982318 -0.785517
c -0.402066 0.054401 -0.511191
d -1.296602 -0.446351 0.488359
e 1.199448 0.555940 1.222145
f 0.623923 -0.388683 0.273779
A B C
a -0.897015 -1.883345 -0.589268
b 0.639703 0.982318 -0.785517
c -0.402066 0.054401 -0.511191
d -1.296602 -0.446351 0.488359
e 1.199448 0.555940 1.222145
f 0.623923 -0.388683 0.273779
A B
a -0.897015 -1.883345
b 0.639703 0.982318
c -0.402066 0.054401
d -1.296602 -0.446351
e 1.199448 0.555940
f 0.623923 -0.388683
df.iloc[[0,1], [0,1,2]] # 行列索引列表
df.iloc[1:3, 0:3] # 行列索引切片
df.iloc[[0,1], 0:3] # 混合
df.iloc[1,3] #定位到标量值
A B C
a -0.897015 -1.883345 -0.589268
b 0.639703 0.982318 -0.785517
A B C
b 0.639703 0.982318 -0.785517
c -0.402066 0.054401 -0.511191
A B C
a -0.897015 -1.883345 -0.589268
b 0.639703 0.982318 -0.785517

0.7083361725183475

混合选取方法 维度 行列 操作类型
df.ix[ ] 二维 行维度
列维度
标签/整数索引、标签/整数切片、标签/整数列表、<布尔数组>、Callable
标签/整数索引、标签/整数切片、标签/整数列表、<布尔数组>、Callable
# df.ix[]的例子
df.ix[0, :] # 行整数索引
df.ix[0:3, :] # 行整数切片
df.ix[[0,1,2], :] # 行整数列表

D:\Program Files\anaconda3\lib\site-packages\ipykernel_launcher.py:2: DeprecationWarning:
.ix is deprecated. Please use
.loc for label based indexing or
.iloc for positional indexing

See the documentation here:
http://pandas.pydata.org/pandas-docs/stable/indexing.html#ix-indexer-is-deprecated

A -0.897015
B -1.883345
C -0.589268
D 1.215273
Name: a, dtype: float64

A B C D
a -0.897015 -1.883345 -0.589268 1.215273
b 0.639703 0.982318 -0.785517 0.708336
c -0.402066 0.054401 -0.511191 1.703571
A B C D
a -0.897015 -1.883345 -0.589268 1.215273
b 0.639703 0.982318 -0.785517 0.708336
c -0.402066 0.054401 -0.511191 1.703571
df.ix['a', :] # 行标签
df.ix['a':'c', :] # 行标签切片
df.ix[['a','b','c'], :] # 行标签列表

A -0.897015
B -1.883345
C -0.589268
D 1.215273
Name: a, dtype: float64

A B C D
a -0.897015 -1.883345 -0.589268 1.215273
b 0.639703 0.982318 -0.785517 0.708336
c -0.402066 0.054401 -0.511191 1.703571
A B C D
a -0.897015 -1.883345 -0.589268 1.215273
b 0.639703 0.982318 -0.785517 0.708336
c -0.402066 0.054401 -0.511191 1.703571
df.ix[:, 0] # 列索引
df.ix[:, 0:3]
df.ix[:, [0,1,2]]

a -0.897015
b 0.639703
c -0.402066
d -1.296602
e 1.199448
f 0.623923
Name: A, dtype: float64

A B C
a -0.897015 -1.883345 -0.589268
b 0.639703 0.982318 -0.785517
c -0.402066 0.054401 -0.511191
d -1.296602 -0.446351 0.488359
e 1.199448 0.555940 1.222145
f 0.623923 -0.388683 0.273779
A B C
a -0.897015 -1.883345 -0.589268
b 0.639703 0.982318 -0.785517
c -0.402066 0.054401 -0.511191
d -1.296602 -0.446351 0.488359
e 1.199448 0.555940 1.222145
f 0.623923 -0.388683 0.273779
df.ix[:, 'A'] #列标签
df.ix[:, 'A':'C']
df.ix[:, ['A','B','C']]

a -0.897015
b 0.639703
c -0.402066
d -1.296602
e 1.199448
f 0.623923
Name: A, dtype: float64

A B C
a -0.897015 -1.883345 -0.589268
b 0.639703 0.982318 -0.785517
c -0.402066 0.054401 -0.511191
d -1.296602 -0.446351 0.488359
e 1.199448 0.555940 1.222145
f 0.623923 -0.388683 0.273779
A B C
a -0.897015 -1.883345 -0.589268
b 0.639703 0.982318 -0.785517
c -0.402066 0.054401 -0.511191
d -1.296602 -0.446351 0.488359
e 1.199448 0.555940 1.222145
f 0.623923 -0.388683 0.273779
df.ix[[0,1,2], 'A':'B'] # 行整数列表,列标签切片
df.ix[0:3, ['A','B','C']] # 行整数切片,列标签列表
df.ix['a':'d', 0:3] # 行标签切片,列整数切片
df.ix[['a','b','c'], 'A':'B'] # 行标签列表,列标签切片
A B
a -0.897015 -1.883345
b 0.639703 0.982318
c -0.402066 0.054401
A B C
a -0.897015 -1.883345 -0.589268
b 0.639703 0.982318 -0.785517
c -0.402066 0.054401 -0.511191
A B C
a -0.897015 -1.883345 -0.589268
b 0.639703 0.982318 -0.785517
c -0.402066 0.054401 -0.511191
d -1.296602 -0.446351 0.488359
A B
a -0.897015 -1.883345
b 0.639703 0.982318
c -0.402066 0.054401
单元格选取方法 维度 行列 操作类型
df.at[] 精确定位单元格 行维度
列维度
标签索引
标签索引
# df.at[]的例子
df.at['a', 'A']
-0.8970154528725585
单元格选取方法 维度 行列 操作类型
df.iat[] 精确定位单元格 行维度
列维度
整数索引
整数索引
# df.iat[]的例子
df.iat[0, 0]

-0.8970154528725585

2.3 pd.Panel对象创建

创建函数 参数 描述
pd.Panel(data, items, major_axis, minor_axis, dtype, copy) data
items
major_axis
major_axis
dtype
copy
数据采取各种形式,如:ndarray,series,map,lists,dict,constant和另一个数据帧(DataFrame);
axis=0;
axis=1;
axis=2;
每列的数据类型;
复制数据,默认 - false;
# 创建面板
# 从3D ndarray创建
data = np.random.rand(2,4,5)
pd.Panel(data)
# 从DataFrame对象的dict创建面板
data = {'Item1' : pd.DataFrame(np.random.randn(4, 3)), 
        'Item2' : pd.DataFrame(np.random.randn(4, 2))}
data
pd.Panel(data)

<class ‘pandas.core.panel.Panel’>
Dimensions: 2 (items) x 4 (major_axis) x 5 (minor_axis)
Items axis: 0 to 1
Major_axis axis: 0 to 3
Minor_axis axis: 0 to 4

{‘Item1’: 0 1 2
0 -0.434674 -1.516325 -1.090963
1 1.343774 -0.143883 -0.213383
2 0.349254 1.666228 -0.654386
3 -1.416395 -0.913377 -0.475773, ‘Item2’: 0 1
0 0.643990 0.304934
1 -0.456729 0.210168
2 -1.620677 -0.479168
3 1.265655 -1.812357}

<class ‘pandas.core.panel.Panel’>
Dimensions: 2 (items) x 4 (major_axis) x 3 (minor_axis)
Items axis: Item1 to Item2
Major_axis axis: 0 to 3
Minor_axis axis: 0 to 2

3. Pandas基本属性

属性或方法 描述
dtypes 返回对象的数据类型(dtype)
shape 返回数据框的大小
head() 返回前n行
tail() 返回最后n行
values 将返回底层数据作为ndarray返回
index 返回行索引
columns 返回列名称
axes 返回行轴标签列表。
empty 如果系列为空,则返回True
ndim 返回底层数据的维数,默认定义:1
size 返回基础数据中的元素数
df.T 将数据框转置
sort_index 按行索引排序
sort_values(by=‘listA’) 按实际值排序,按listA列值排序,其他数据参照此列变化
# pandas相关属性示例
# df = pd.DataFrame({'A':np.random.randn(24)
#                   ,'B': ['A', 'B', 'C'] * 8
#                   ,'C': ['Female', 'Male', 'Male', 'Male']*6}
#                   , index=pd.date_range('20170101', periods=24))
d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Minsu','Jack']),
   'Age':pd.Series([25,26,25,23,30,29,23]),
   'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])}
df=pd.DataFrame(d)
df.dtypes
df.shape
df.head() # df.head(7)
df.tail()

Age int64
Name object
Rating float64
dtype: object

(7, 3)
Age Name Rating
0 25 Tom 4.23
1 26 James 3.24
2 25 Ricky 3.98
3 23 Vin 2.56
4 30 Steve 3.20
Age Name Rating
2 25 Ricky 3.98
3 23 Vin 2.56
4 30 Steve 3.20
5 29 Minsu 4.60
6 23 Jack 3.80
df.values
df.index
df.columns
df.axes
df.empty

array([[25, ‘Tom’, 4.23],
[26, ‘James’, 3.24],
[25, ‘Ricky’, 3.98],
[23, ‘Vin’, 2.56],
[30, ‘Steve’, 3.2],
[29, ‘Minsu’, 4.6],
[23, ‘Jack’, 3.8]], dtype=object)

RangeIndex(start=0, stop=7, step=1)

Index([‘Age’, ‘Name’, ‘Rating’], dtype=‘object’)

[RangeIndex(start=0, stop=7, step=1),
Index([‘Age’, ‘Name’, ‘Rating’], dtype=‘object’)]

False

df.ndim
df.size
df[['Age','Rating']].describe
df.T
df.sort_index # 默认升序
df.sort_index(ascending=False) # 降序
# df.sort_index(axis=1) # 按列排序
df.sort_values(by=['Age','Rating']) # 按值排序

2

21

<bound method NDFrame.describe of Age Rating
0 25 4.23
1 26 3.24
2 25 3.98
3 23 2.56
4 30 3.20
5 29 4.60
6 23 3.80>

0 1 2 3 4 5 6
Age 25 26 25 23 30 29 23
Name Tom James Ricky Vin Steve Minsu Jack
Rating 4.23 3.24 3.98 2.56 3.2 4.6 3.8

<bound method DataFrame.sort_index of Age Name Rating
0 25 Tom 4.23
1 26 James 3.24
2 25 Ricky 3.98
3 23 Vin 2.56
4 30 Steve 3.20
5 29 Minsu 4.60
6 23 Jack 3.80>

Age Name Rating
6 23 Jack 3.80
5 29 Minsu 4.60
4 30 Steve 3.20
3 23 Vin 2.56
2 25 Ricky 3.98
1 26 James 3.24
0 25 Tom 4.23
Age Name Rating
3 23 Vin 2.56
6 23 Jack 3.80
2 25 Ricky 3.98
0 25 Tom 4.23
1 26 James 3.24
5 29 Minsu 4.60
4 30 Steve 3.20

4. Pandas描述性统计

函数 描述
count() 非空观测数量
sum() 所有值之和
mean() 所有值的平均值
median() 所有值的中位数
mode() 值的模值
std() 值的标准偏差
min() 所有值中的最小值
max() 所有值中的最大值
abs() 绝对值
prod() 数组元素的乘积
cumsum() 累计总和
cumprod() 累计乘积
函数 描述 参数说明
describe ( include= ) 对数据的描述性统计信息,注意数据需要是数据类型(float,int) include默认number 用于汇总数字列;
object 汇总字符串列;
all 将所有列汇总在一起
# 描述性统计函数示例
d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Minsu','Jack',
     'Lee','David','Gasper','Betina','Andres']),
     'Age':pd.Series([25,26,25,23,30,29,23,34,40,30,51,46]),
     'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8,3.78,2.98,4.80,4.10,3.65])}
df=pd.DataFrame(d)
df.count()
df.sum() # 默认axis=0
df.sum(axis=1)

Age 12
Name 12
Rating 12
dtype: int64

Age 382
Name TomJamesRickyVinSteveMinsuJackLeeDavidGasperBe…
Rating 44.92
dtype: object

0 29.23
1 29.24
2 28.98
3 25.56
4 33.20
5 33.60
6 26.80
7 37.78
8 42.98
9 34.80
10 55.10
11 49.65
dtype: float64

df.mean()
df.median()

Age 31.833333
Rating 3.743333
dtype: float64

Age 29.50
Rating 3.79
dtype: float64

df.mode()
df.std()
df.max()
Age Name Rating
0 23.0 Andres 2.56
1 25.0 Betina 2.98
2 30.0 David 3.20
3 NaN Gasper 3.24
4 NaN Jack 3.65
5 NaN James 3.78
6 NaN Lee 3.80
7 NaN Minsu 3.98
8 NaN Ricky 4.10
9 NaN Steve 4.23
10 NaN Tom 4.60
11 NaN Vin 4.80

Age 9.232682
Rating 0.661628
dtype: float64

Age 51
Name Vin
Rating 4.8
dtype: object

df.prod()
df.cumsum()
df[['Age','Rating']].cumprod() # abs/cumprod必须针对数值型

Age 7.158408e+17
Rating 6.320128e+06
dtype: float64

Age Name Rating
0 25 Tom 4.23
1 51 TomJames 7.47
2 76 TomJamesRicky 11.45
3 99 TomJamesRickyVin 14.01
4 129 TomJamesRickyVinSteve 17.21
5 158 TomJamesRickyVinSteveMinsu 21.81
6 181 TomJamesRickyVinSteveMinsuJack 25.61
7 215 TomJamesRickyVinSteveMinsuJackLee 29.39
8 255 TomJamesRickyVinSteveMinsuJackLeeDavid 32.37
9 285 TomJamesRickyVinSteveMinsuJackLeeDavidGasper 37.17
10 336 TomJamesRickyVinSteveMinsuJackLeeDavidGasperBe... 41.27
11 382 TomJamesRickyVinSteveMinsuJackLeeDavidGasperBe... 44.92
Age Rating
0 2.500000e+01 4.230000e+00
1 6.500000e+02 1.370520e+01
2 1.625000e+04 5.454670e+01
3 3.737500e+05 1.396395e+02
4 1.121250e+07 4.468465e+02
5 3.251625e+08 2.055494e+03
6 7.478738e+09 7.810877e+03
7 2.542771e+11 2.952512e+04
8 1.017108e+13 8.798485e+04
9 3.051325e+14 4.223273e+05
10 1.556176e+16 1.731542e+06
11 7.158408e+17 6.320128e+06
df.describe()
df.describe(include=['object'])
df.describe(include='all')
Age Rating
count 12.000000 12.000000
mean 31.833333 3.743333
std 9.232682 0.661628
min 23.000000 2.560000
25% 25.000000 3.230000
50% 29.500000 3.790000
75% 35.500000 4.132500
max 51.000000 4.800000
Name
count 12
unique 12
top Lee
freq 1
Age Name Rating
count 12.000000 12 12.000000
unique NaN 12 NaN
top NaN Lee NaN
freq NaN 1 NaN
mean 31.833333 NaN 3.743333
std 9.232682 NaN 0.661628
min 23.000000 NaN 2.560000
25% 25.000000 NaN 3.230000
50% 29.500000 NaN 3.790000
75% 35.500000 NaN 4.132500
max 51.000000 NaN 4.800000

5. Pandas统计函数

函数 描述
pct_change() 函数将每个元素与其前一个元素进行比较,并计算变化百分比
cov ( ) cov用来计算序列对象之间的协方差,NA将被自动排除
corr ( ) 相关性显示了任何两个数值(系列)之间的线性关系,计算pearson(默认),spearman和kendall之间的相关性
rank ( ) 数据排名为元素数组中的每个元素生成排名
# 统计函数示例
df = pd.DataFrame(np.random.randn(4, 2))
df.pct_change() # 行df.pct_change(axis=1)
0 1
0 NaN NaN
1 -1.599659 0.390988
2 1.791461 -0.938851
3 -2.746943 4.785401
s1 = pd.Series(np.random.randn(10))
s2 = pd.Series(np.random.randn(10))
s1.cov(s2)
df = pd.DataFrame(np.random.randn(10, 5), columns=['a', 'b', 'c', 'd', 'e'])
df['a'].cov(df['b'])
df.cov() # cov()计算所有列之间的协方差值

-0.12117368605285425
0.02248553586668352

a b c d e
a 0.201676 0.022486 -0.053950 0.249407 -0.171799
b 0.022486 0.370753 0.072438 0.362195 -0.123009
c -0.053950 0.072438 1.018886 0.151529 0.325987
d 0.249407 0.362195 0.151529 1.893329 -0.448767
e -0.171799 -0.123009 0.325987 -0.448767 0.951633
df['a'].corr(df['b'])
df.corr()  # 计算所有列之间的相关系数

0.08223070280618396

a b c d e
a 1.000000 0.082231 -0.119014 0.403617 -0.392157
b 0.082231 1.000000 0.117859 0.432302 -0.207091
c -0.119014 0.117859 1.000000 0.109098 0.331056
d 0.403617 0.432302 0.109098 1.000000 -0.334328
e -0.392157 -0.207091 0.331056 -0.334328 1.000000
df.rank()
a b c d e
0 3.0 9.0 2.0 6.0 5.0
1 7.0 10.0 9.0 9.0 9.0
2 1.0 3.0 4.0 2.0 4.0
3 4.0 7.0 10.0 7.0 6.0
4 10.0 4.0 8.0 4.0 2.0
5 8.0 5.0 3.0 1.0 7.0
6 9.0 6.0 1.0 10.0 1.0
7 6.0 2.0 7.0 5.0 8.0
8 2.0 1.0 6.0 3.0 10.0
9 5.0 8.0 5.0 8.0 3.0

6. Pandas字符串和文本函数

函数 函数作用
lower() 将Series/Index中的字符串转换为小写。
upper() 将Series/Index中的字符串转换为大写。
len() 计算字符串长度。
strip() 帮助从两侧的系列/索引中的每个字符串中删除空格(包括换行符)。
split(’ ') 用给定的模式拆分每个字符串。
cat(sep=’ ') 使用给定的分隔符连接系列/索引元素。
get_dummies() 返回具有单热编码值的数据帧(DataFrame)。
contains(pattern) 如果元素中包含子字符串,则返回每个元素的布尔值True,否则为False。
replace(a,b) 将值a替换为值b。
repeat(value) 重复每个元素指定的次数。
count(pattern) 返回模式中每个元素的出现总数。
startswith(pattern) 如果系列/索引中的元素以模式开始,则返回true。
endswith(pattern) 如果系列/索引中的元素以模式结束,则返回true。
find(pattern) 返回模式第一次出现的位置。
findall(pattern) 返回模式的所有出现的列表。
swapcase 变换字母大小写。
islower() 检查系列/索引中每个字符串中的所有字符是否小写,返回布尔值
isupper() 检查系列/索引中每个字符串中的所有字符是否大写,返回布尔值
isnumeric() 检查系列/索引中每个字符串中的所有字符是否为数字,返回布尔值。
# 字符串和文本函数示例
d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Minsu','Jack']),
     'Age':pd.Series([25,26,25,23,30,29,23]),
     'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])}
df=pd.DataFrame(d)
df['Name'].str.lower()
df['Name'].str.upper()
df['Name'].str.len()

0 tom
1 james
2 ricky
3 vin
4 steve
5 minsu
6 jack
Name: Name, dtype: object

0 TOM
1 JAMES
2 RICKY
3 VIN
4 STEVE
5 MINSU
6 JACK
Name: Name, dtype: object

0 3
1 5
2 5
3 3
4 5
5 5
6 4
Name: Name, dtype: int64

(df.sort_values(by='Age'))['Name'].str.cat(sep='->')
df['Name'].str.contains('')
df['Name'].str.replace('a','#')

‘Vin->Jack->Tom->Ricky->James->Minsu->Steve’

0 True
1 True
2 True
3 True
4 True
5 True
6 True
Name: Name, dtype: bool

0 Tom
1 J#mes
2 Ricky
3 Vin
4 Steve
5 Minsu
6 J#ck
Name: Name, dtype: object

df['Name'].str.repeat(2)
df['Name'].str.count('m')
df['Name'].str.startswith('T')

0 TomTom
1 JamesJames
2 RickyRicky
3 VinVin
4 SteveSteve
5 MinsuMinsu
6 JackJack
Name: Name, dtype: object

0 1
1 1
2 0
3 0
4 0
5 0
6 0
Name: Name, dtype: int64

0 True
1 False
2 False
3 False
4 False
5 False
6 False
Name: Name, dtype: bool

df['Name'].str.find('m')
df['Name'].str.findall('m')
df['Name'].str.swapcase()

0 2
1 2
2 -1
3 -1
4 -1
5 -1
6 -1
Name: Name, dtype: int64

0 [m]
1 [m]
2 []
3 []
4 []
5 []
6 []
Name: Name, dtype: object

0 tOM
1 jAMES
2 rICKY
3 vIN
4 sTEVE
5 mINSU
6 jACK
Name: Name, dtype: object

df['Name'].str.isupper()
df['Name'].str.isnumeric()

0 False
1 False
2 False
3 False
4 False
5 False
6 False
Name: Name, dtype: bool

0 False
1 False
2 False
3 False
4 False
5 False
6 False
Name: Name, dtype: bool

7. Pandas窗口函数与聚合函数

函数 函数作用
df.rolling( window=n ) 指定window=n参数并在其上应用适当的统计函数,前n-1个元素有空值,第n个元素的值将是n,n-1….0个元素的统计函数值
df.expanding( min_periods=n ) 指定min_periods=n参数并在其上应用适当的统计函数,与rolling函数功能相似
df.aggregate( np.sum ) 在整个数据框上应用聚合\单个列上应用聚合\多列上应用聚合\单个列上应用多个函数\多列上应用多个函数
# 窗口函数
df = pd.DataFrame(np.random.randn(10, 4),index = pd.date_range('1/1/2020', periods=10),columns = ['A', 'B', 'C', 'D'])
df.rolling(window=4).mean()
A B C D
2020-01-01 NaN NaN NaN NaN
2020-01-02 NaN NaN NaN NaN
2020-01-03 NaN NaN NaN NaN
2020-01-04 0.072847 0.625421 -0.321718 -0.286368
2020-01-05 -0.111957 0.589124 -0.955854 -0.709480
2020-01-06 -0.785411 1.143256 -0.521317 -0.302849
2020-01-07 -0.662634 0.507758 -0.407731 -0.527692
2020-01-08 0.115185 0.596837 -0.579141 -0.239076
2020-01-09 0.245764 0.085004 -0.170510 0.295653
2020-01-10 0.627896 -0.273199 -0.122880 0.188302
df.expanding(min_periods=3).mean()
A B C D
2020-01-01 NaN NaN NaN NaN
2020-01-02 NaN NaN NaN NaN
2020-01-03 0.879951 0.437257 -0.767225 -0.045709
2020-01-04 0.072847 0.625421 -0.321718 -0.286368
2020-01-05 0.269547 0.644470 -0.466433 -0.504114
2020-01-06 -0.051696 0.680689 -0.491277 -0.303636
2020-01-07 -0.001526 0.477544 -0.561800 -0.321128
2020-01-08 0.094016 0.611129 -0.450430 -0.262722
2020-01-09 0.258977 0.395819 -0.334912 -0.148662
2020-01-10 0.220141 0.299134 -0.343918 -0.106861
# 聚合函数
df1=df.rolling(window=3,min_periods=1)
df1.aggregate(np.sum) #在整个数据框上应用聚合
A B C D
2020-01-01 1.795565 0.865853 1.491254 0.317351
2020-01-02 2.831468 -0.488887 -0.862395 -0.610418
2020-01-03 2.639853 1.311772 -2.301674 -0.137128
2020-01-04 -1.504176 1.635833 -2.778127 -1.462824
2020-01-05 -1.483733 3.711238 -1.469768 -1.910150
2020-01-06 -2.950030 2.772365 -0.645988 -1.684687
2020-01-07 -0.302073 0.841119 -2.645727 -1.102421
2020-01-08 -0.595606 1.666682 -1.271275 0.418790
2020-01-09 2.640971 -0.521768 -0.066542 0.483859
2020-01-10 2.212092 -0.351465 0.493420 1.179287
df1['A'].aggregate(np.sum) # 在数据框的单个列上应用聚合
df1[['A','C']].aggregate(np.sum) # 在DataFrame的多列上应用聚合

2020-01-01 1.795565
2020-01-02 2.831468
2020-01-03 2.639853
2020-01-04 -1.504176
2020-01-05 -1.483733
2020-01-06 -2.950030
2020-01-07 -0.302073
2020-01-08 -0.595606
2020-01-09 2.640971
2020-01-10 2.212092
Freq: D, Name: A, dtype: float64

A C
2020-01-01 1.795565 1.491254
2020-01-02 2.831468 -0.862395
2020-01-03 2.639853 -2.301674
2020-01-04 -1.504176 -2.778127
2020-01-05 -1.483733 -1.469768
2020-01-06 -2.950030 -0.645988
2020-01-07 -0.302073 -2.645727
2020-01-08 -0.595606 -1.271275
2020-01-09 2.640971 -0.066542
2020-01-10 2.212092 0.493420
df1['A'].aggregate([np.sum,np.mean]) # 在DataFrame的单个列上应用多个函数
df1[['A','C']].aggregate([np.sum,np.mean]) # 在DataFrame的多列上应用多个函数 
sum mean
2020-01-01 1.795565 1.795565
2020-01-02 2.831468 1.415734
2020-01-03 2.639853 0.879951
2020-01-04 -1.504176 -0.501392
2020-01-05 -1.483733 -0.494578
2020-01-06 -2.950030 -0.983343
2020-01-07 -0.302073 -0.100691
2020-01-08 -0.595606 -0.198535
2020-01-09 2.640971 0.880324
2020-01-10 2.212092 0.737364
A C
sum mean sum mean
2020-01-01 1.795565 1.795565 1.491254 1.491254
2020-01-02 2.831468 1.415734 -0.862395 -0.431197
2020-01-03 2.639853 0.879951 -2.301674 -0.767225
2020-01-04 -1.504176 -0.501392 -2.778127 -0.926042
2020-01-05 -1.483733 -0.494578 -1.469768 -0.489923
2020-01-06 -2.950030 -0.983343 -0.645988 -0.215329
2020-01-07 -0.302073 -0.100691 -2.645727 -0.881909
2020-01-08 -0.595606 -0.198535 -1.271275 -0.423758
2020-01-09 2.640971 0.880324 -0.066542 -0.022181
2020-01-10 2.212092 0.737364 0.493420 0.164473
df1.aggregate({'A' : np.sum,'B' : np.mean}) # 将不同的函数应用于DataFrame的不同列
A B
2020-01-01 1.795565 0.865853
2020-01-02 2.831468 -0.244444
2020-01-03 2.639853 0.437257
2020-01-04 -1.504176 0.545278
2020-01-05 -1.483733 1.237079
2020-01-06 -2.950030 0.924122
2020-01-07 -0.302073 0.280373
2020-01-08 -0.595606 0.555561
2020-01-09 2.640971 -0.173923
2020-01-10 2.212092 -0.117155

猜你喜欢

转载自blog.csdn.net/KEEP_GIONG/article/details/80137281