pandas模块

pandas模块简介

pandas是python数据分析的核心模块。它主要提供了五大功能:

  1. 支持文件存取操作,支持数据库(sql)、html、json、pickle、csv(txt、excel)、sas、stata、hdf等。
  2. 支持增删改查、切片、高阶函数、分组聚合等单表操作,以及和dict、list的互相转换。
  3. 支持多表拼接合并操作。
  4. 支持简单的绘图操作。
  5. 支持简单的统计分析操作。

Series数据结构

Series是一种类似于一维数组的对象,由一组数据和一组与之相关的数据标签(索引)组成。

Series比较像列表(数组)和字典的结合体

import numpy as np
import pandas as pd

Series创建方式

  • 第一种: 直接传入一个列表,此时由于没有指定数据索引,则会自动创建一个0~N-1(N为数据的长度)的整型索引,可以通过索引进行取值
df = pd.Series([i for i in range(4, 8)])
df
0    4
1    5
2    6
3    7
dtype: int64
df[1]
5
df.values
array([4, 5, 6, 7], dtype=int64)
  • 第二种:传入一个列表,自定义索引列表(索引列表长度需要和数据的长度一致),此时就可以通过自定义的索引进行取值, 但还是可以通过默认索引进行取值
df1 = pd.Series([2,3,5,7,9], index=['a','c', 'b','e','f'])
df1
a    2
c    3
b    5
e    7
f    9
dtype: int64
df1[2]
5
df1['b']
5
df1.index
Index(['a', 'c', 'b', 'e', 'f'], dtype='object')
  • 第三种: 传入一个字典,相当于第二种方式
df2 = pd.Series({'b': 2, 'f': 5})
df2
b    2
f    5
dtype: int64
df2[0]
2
df2['f']
5
  • 第四种: 创建一个值都是0的数组
pd.Series(0, index=['a','b'])
a    0
b    0
dtype: int64

Series缺失数据处理

方法 详解
dropna() 过滤掉值为NaN的行
fillna() 填充缺失数据
isnull() 返回布尔数组,缺失值对应为True
notnull() 返回布尔数组,缺失值对应为False
df = pd.Series([1, 2, 3, 4, np.nan], index=['a', 'b', 'c', 'd', 'e'])
print(df)
a    1.0
b    2.0
c    3.0
d    4.0
e    NaN
dtype: float64
print(df.dropna())  # 不会改变原先的数组
a    1.0
b    2.0
c    3.0
d    4.0
dtype: float64
df1 = df.copy()
df1.dropna(inplace=True)  # inplace参数默认为False,当设为True的时候,则会改变原先的数组
df1
a    1.0
b    2.0
c    3.0
d    4.0
dtype: float64
df.fillna(0)
a    1.0
b    2.0
c    3.0
d    4.0
e    0.0
dtype: float64
df.isna()
a    False
b    False
c    False
d    False
e     True
dtype: bool
df.notnull()
a     True
b     True
c     True
d     True
e    False
dtype: bool

Series特性

  • 从ndarray创建Series:Series(arr)
  • 与标量(数字):sr * 2
  • 两个Series运算
  • 通用函数:np.ads(sr)
  • 布尔值过滤:sr[sr>0]
  • 统计函数:mean()、sum()、cumsum()

支持字典的特性

  • 从字典创建Series:Series(dic),
  • In运算:'a'in sr、for x in sr
  • 键索引:sr['a'],sr[['a','b','d']]
  • 键切片:sr['a':'c']
  • 其他函数:get('a',default=0)等

整数索引

pandas当中的整数索引对象可能会让初次接触它的人很懵逼,
接下来通过代码演示:

df = pd.Series(np.arange(10))
df1 = df[3:].copy()
df1
3    3
4    4
5    5
6    6
7    7
8    8
9    9
dtype: int32
df1[1]  # 报错, 因为pandas当中使用整数索引取值是优先以标签解释的,而不是下标

此时有如下解决办法:

  1. loc属性: 以标签解释
  2. iloc属性: 以下标解释
df1.loc[3]
3
df1.iloc[0]
3

Series数据对齐

pandas在运算时,会按索引进行对齐然后计算。如果存在不同的索引,则结果的索引是两个操作数索引的并集。

sr1 = pd.Series([12,23,34], index=['c','a','d'])
sr1
c    12
a    23
d    34
dtype: int64
sr2 = pd.Series([11,20,10], index=['d','c','a',])
sr2
d    11
c    20
a    10
dtype: int64
sr1 + sr2  # 可以通过这种索引对齐直接将两个Series对象进行运算
a    33
c    32
d    45
dtype: int64
sr3 = pd.Series([11,20,10,14], index=['d','c','a','b'])
sr3
d    11
c    20
a    10
b    14
dtype: int64
sr1 + sr3  #  sr1 和 sr3的索引不一致,所以最终的运行会发现b索引对应的值无法运算,就返回了NaN,一个缺失值
a    33.0
b     NaN
c    32.0
d    45.0
dtype: float64

此时可以将两个Series对象相加时将缺失值设为0

sr1.add(sr3, fill_value=0)# 将缺失值设为0,所以最后算出来b索引对应的结果为14
a    33.0
b    14.0
c    32.0
d    45.0
dtype: float64

DataFram

DataFrame是一个表格型的数据结构,含有一组有序的列。
DataFrame可以被看做是由Series组成的字典,并且共用一个索引。

DataFram创建方式

  • 利用包含等长度列表或Numpy数组的字典来形成DataFrame
pd.DataFrame({'a': pd.Series([1,2,3,4]),'b':pd.Series([5,6,7,8])})
a b
0 1 5
1 2 6
2 3 7
3 4 8
  • 通过columns参数指定列参数
pd.DataFrame(np.random.randint(1,100,(3,4),dtype=int), columns=['c1','c2','c3','c4'])
c1 c2 c3 c4
0 43 72 62 87
1 77 91 42 98
2 22 76 62 63
  • 通过index指定行参数
df = pd.DataFrame(np.random.randint(1,100,(3,4),dtype=int),index=['one', 'tow', 'three'] ,columns=['c1','c3','c2','c4'])
df
c1 c3 c2 c4
one 37 85 3 22
tow 45 7 36 49
three 62 15 28 79

DataFram常用属性和方法

属性/方法 作用
dtypes 查看数据类型
index 获取行索引
columns 获取列索引
transpose 转置,也可用T来操作
values 获取值索引
describe() 获取快速统计
sort_index(axis) 排序,可按行(axis=0)或列(axis=1)index排序输出
sort_values(by) 按数据值来排序
df.dtypes
c1    int32
c3    int32
c2    int32
c4    int32
dtype: object
df.index
Index(['one', 'tow', 'three'], dtype='object')
df.values
array([[24, 51, 24,  6],
       [32,  9, 44, 57],
       [ 3, 27,  1, 84]])
df.columns
Index(['c1', 'c3', 'c2', 'c4'], dtype='object')
df.T
one tow three
c1 24 32 3
c3 51 9 27
c2 24 44 1
c4 6 57 84
df.describe()
c1 c3 c2 c4
count 3.000000 3.000000 3.000000 3.000000
mean 19.666667 29.000000 23.000000 49.000000
std 14.977761 21.071308 21.517435 39.610605
min 3.000000 9.000000 1.000000 6.000000
25% 13.500000 18.000000 12.500000 31.500000
50% 24.000000 27.000000 24.000000 57.000000
75% 28.000000 39.000000 34.000000 70.500000
max 32.000000 51.000000 44.000000 84.000000
df.sort_index(axis=0)  # 按照行索引进行排序
c1 c3 c2 c4
one 24 51 24 6
three 3 27 1 84
tow 32 9 44 57
df.sort_index(axis=1) # 按照列索引进行排序
c1 c2 c3 c4
one 24 24 51 6
tow 32 44 9 57
three 3 1 27 84
df.sort_values(by='c3', ascending=False)  # 默认按照列的值进行排序
c1 c3 c2 c4
one 24 51 24 6
three 3 27 1 84
tow 32 9 44 57
df.sort_values(by='three',axis=1) # 按照行的值进行排序
c2 c1 c3 c4
one 24 24 51 6
tow 44 32 9 57
three 1 3 27 84

DataFram的索引和切片

通过columns取值

df
c1 c3 c2 c4
one 24 51 24 6
tow 32 9 44 57
three 3 27 1 84
df[['c1','c3']]  # 取多列数据需要传一个列表类型
c1 c3
one 24 51
tow 32 9
three 3 27

loc通过行标签进行取值

  • 通过自定义的行标签进行取值
df.loc[:'one']
c1 c3 c2 c4
one 24 51 24 6
  • 通过索引取值
df[1:3]
c1 c3 c2 c4
tow 32 9 44 57
three 3 27 1 84

iloc(类似于numpy数组取值)

df
c1 c3 c2 c4
one 24 51 24 6
tow 32 9 44 57
three 3 27 1 84
df.iloc[0, 0]
24
df.iloc[:2, 1:4]
c3 c2 c4
one 51 24 6
tow 9 44 57

使用逻辑判断进行取值

df[df['c1'] >20]
c1 c3 c2 c4
one 24 51 24 6
tow 32 9 44 57
df[(df['c1'] > 20) & (df['c2'] <30)]
c1 c3 c2 c4
one 24 51 24 6

DataFram值替换

df
c1 c3 c2 c4
one 37 85 3 22
tow 45 7 36 49
three 62 15 28 79
df1 = df.copy()
df1[df1 < 20] = 100
df1
c1 c3 c2 c4
one 37 85 100 22
tow 45 100 36 49
three 62 100 28 79

产生时间对象数组: date_range

参数:

参数 详解
start 开始时间
end 结束时间
periods 时间长度
freq 时间频率,默认为'D',可选H(our),W(eek),B(usiness),S(emi-)M(onth),(min)T(es), S(econd), A(year),…
dates = pd.date_range('20190101', periods=3, freq='M')
dates
DatetimeIndex(['2019-01-31', '2019-02-28', '2019-03-31'], dtype='datetime64[ns]', freq='M')
df.index = dates
df
c1 c3 c2 c4
2019-01-31 37 85 3 22
2019-02-28 45 7 36 49
2019-03-31 62 15 28 79

数据分组和聚合

在数据分析当中,我们有时需要将数据拆分,然后在每一个特定的组里进行运算,这些操作通常也是数据分析工作中的重要环节。

分组(groupby)

pandas对象(无论Series、DataFrame还是其他的什么)当中的数据会根据提供的一个或者多个键被拆分为多组,拆分操作是在对象的特定轴上执行的。就比如DataFrame可以在他的行上或者列上进行分组,然后将一个函数应用到各个分组上并产生一个新的值。最后将所有的执行结果合并到最终的结果对象中。

分组键的形式:

  • 列表或者数组,长度与待分组的轴一样
  • 表示DataFrame某个列名的值。
  • 字典或Series,给出待分组轴上的值与分组名之间的对应关系
  • 函数,用于处理轴索引或者索引中的各个标签吗

后三种只是快捷方式,最终仍然是为了产生一组用于拆分对象的值。
首先,通过一个很简单的DataFrame数组尝试一下:

df = pd.DataFrame({'key1':['x','x','y','y','x'],                               
            'key2':['one','two','one','two','one'],
            'data1':np.random.randn(5),
            'data2':np.random.randn(5)})
df
key1 key2 data1 data2
0 x one 0.506897 -1.189281
1 x two -1.448441 -1.658427
2 y one -0.665272 -1.708576
3 y two -1.466032 -1.705750
4 x one 3.127327 -1.591700
# 访问data1,并根据key1调用groupby:
f1 = df['data1'].groupby(df['key1'])
f1.groups
{'x': Int64Index([0, 1, 4], dtype='int64'),
 'y': Int64Index([2, 3], dtype='int64')}

上述运行是没有进行任何计算的,但是我们想要的中间数据已经拿到了,接下来,就可以调用groupby进行任何计算

# 调用mean函数求出平均值
f1.mean()
key1
x    0.728594
y   -1.065652
Name: data1, dtype: float64

以上数据经过分组键(一个Series数组)进行了聚合,产生了一个新的Series,索引就是key1列中的唯一值。这些索引的名称就为key1。接下来就尝试一次将多个数组的列表传进来

f2 = df['data1'].groupby([df['key1'],df['key2']])
f2.mean()
key1  key2
x     one     1.817112
      two    -1.448441
y     one    -0.665272
      two    -1.466032
Name: data1, dtype: float64

传入多个数据之后会发现,得到的数据具有一个层次化的索引,key1对应的xy;key2对应的one wo.

# 通过unstack方法就可以让索引不堆叠在一起了

f2.mean().unstack()
key2 one two
key1
x 1.817112 -1.448441
y -0.665272 -1.466032

补充:

  1. 分组键可以是任意长度的数组
  2. 分组时,对于不是数组数据的列会从结果中排除,例如key1、key2这样的列
  3. GroupBy的size方法,返回一个含有分组大小的Series
f1.size()
key1
x    3
y    2
Name: data1, dtype: int64

聚合

聚合是指任何能够从数组产生标量值的数据转换过程。刚才上面的操作会发现使用GroupBy并不会直接得到一个显性的结果,而是一个中间数据,可以通过执行类似mean、count、min等计算得出结果,常见的还有一些:

函数名 描述
sum 非NA值的和
median 非NA值的算术中位数
std、var 无偏(分母为n-1)标准差和方差
prod 非NA值的积
first、last 第一个和最后一个非NA值

自定义聚合函数

不仅可以使用这些常用的聚合运算,还可以自己自定义。使用自定义的聚合函数,需要将其传入aggregate或者agg方法当中

def peak_to_peak(arr):
    return arr.max() - arr.min()

f1.aggregate(peak_to_peak)
key1
x    4.575767
y    0.800759
Name: data1, dtype: float64
f1.agg(['mean','std'])
mean std
key1
x 0.728594 2.295926
y -1.065652 0.566222

最终得到的列就会以相应的函数命名生成一个DataFrame数组

apply

GroupBy当中自由度最高的方法就是apply,它会将待处理的对象拆分为多个片段,然后各个片段分别调用传入的函数,最后将它们组合到一起。

df.apply( ['func', 'axis=0', 'broadcast=None', 'raw=False', 'reduce=None', 'result_type=None', 'args=()', '**kwds'])

  • func:传入一个自定义函数
  • axis:函数传入参数当axis=1就会把一行数据作为Series的数据

案例:

url="https://baike.baidu.com/item/NBA%E6%80%BB%E5%86%A0%E5%86%9B/2173192?fr=aladdin"
nba_champions=pd.read_html(url)  # 获取数据
a1 = nba_champions[0]    # 取出决赛名单
a1.columns = a1.loc[0]  # 使用第一行的数据替换默认的横向索引
a1.drop(0,inplace=True)  # 将第一行的数据删除
a1.head()
年份 比赛日期 冠军 总比分 亚军 FMVP
1 1947 4.16-4.22 费城勇士队 4-1 芝加哥牡鹿队
2 1948 4.10-4.21 巴尔的摩子弹队 4-2 费城勇士队
3 1949 4.4-4.13 明尼阿波利斯湖人队 4-2 华盛顿国会队
4 1950 4.8-4.23 明尼阿波利斯湖人队 4-2 塞拉库斯民族队
5 1951 4.7-4.21 罗切斯特皇家队 4-3 纽约尼克斯队
# 取各个球队获取总冠军的次数的前10名

a1.groupby('冠军').size().sort_values(ascending=False).head(10)
冠军
波士顿凯尔特人队     17
洛杉矶湖人队       11
芝加哥公牛队        6
圣安东尼奥马刺队      5
明尼阿波利斯湖人队     5
金州勇士队         4
迈阿密热火队        3
底特律活塞队        3
休斯顿火箭队        2
纽约尼克斯队        2
dtype: int64

其他常用方法

pandas常用方法(适用Series和DataFrame)

  • mean(axis=0,skipna=False)
  • sum(axis=1)
  • sort_index(axis, …, ascending) # 按行或列索引排序
  • sort_values(by, axis, ascending) # 按值排序
  • apply(func, axis=0) # 将自定义函数应用在各行或者各列上,func可返回标量或者Series
  • applymap(func) # 将函数应用在DataFrame各个元素上
  • map(func) # 将函数应用在Series各个元素上

合并数据

  • pd.concat: 合并数据,合并行(axis=1),合并列(axis=0)
  • obj.append: 只能合并列
df1 = pd.DataFrame(np.zeros((3, 4)))
df1
0 1 2 3
0 0.0 0.0 0.0 0.0
1 0.0 0.0 0.0 0.0
2 0.0 0.0 0.0 0.0
df2 = df2 = pd.DataFrame(np.ones((3, 4)))
df2
0 1 2 3
0 1.0 1.0 1.0 1.0
1 1.0 1.0 1.0 1.0
2 1.0 1.0 1.0 1.0
pd.concat((df1, df2),axis=1)
0 1 2 3 0 1 2 3
0 0.0 0.0 0.0 0.0 1.0 1.0 1.0 1.0
1 0.0 0.0 0.0 0.0 1.0 1.0 1.0 1.0
2 0.0 0.0 0.0 0.0 1.0 1.0 1.0 1.0
df1.append(df2)
0 1 2 3
0 0.0 0.0 0.0 0.0
1 0.0 0.0 0.0 0.0
2 0.0 0.0 0.0 0.0
0 1.0 1.0 1.0 1.0
1 1.0 1.0 1.0 1.0
2 1.0 1.0 1.0 1.0

导入导出数据

使用df = pd.read_excel(filename)读取文件,使用df.to_excel(filename)保存文件。

读取文件导入数据

读取文件导入数据函数主要参数:

参数 详解
sep 指定分隔符,可用正则表达式如's+'
header=None 指定文件无行名
name 指定列名
index_col 指定某列作为索引
skip_row 指定跳过某些行
na_values 指定某些字符串表示缺失值
parse_dates 指定某些列是否被解析为日期,布尔值或列表

写入文件导出数据

写入文件函数的主要参数:

参数 详解
sep 分隔符
na_rep 指定缺失值转换的字符串,默认为空字符串
header=False 不保存列名
index=False 不保存行索引
cols 指定输出的列,传入列表

读取CSV文件并处理数据

from io import StringIO

test_data = '''
5.1,,1.4,0.2
4.9,3.0,1.4,0.2
4.7,3.2,,0.2
7.0,3.2,4.7,1.4
6.4,3.2,4.5,1.5
6.9,3.1,4.9,
,,,
'''

test_data = StringIO(test_data)
df = pd.read_csv(test_data, header=None)
df.columns = ['A', 'B', 'C', 'D']
df.index = [1,2,3,4,5,6,7]
df
A B C D
1 5.1 NaN 1.4 0.2
2 4.9 3.0 1.4 0.2
3 4.7 3.2 NaN 0.2
4 7.0 3.2 4.7 1.4
5 6.4 3.2 4.5 1.5
6 6.9 3.1 4.9 NaN
7 NaN NaN NaN NaN

处理丢失数据

# 通过在isnull()方法后使用sum()方法即可获得该数据集某个特征含有多少个缺失值

df.isnull().sum()
A    1
B    2
C    2
D    2
dtype: int64
# axis=0删除有NaN值的行

df.dropna(axis=0)
A B C D
2 4.9 3.0 1.4 0.2
4 7.0 3.2 4.7 1.4
5 6.4 3.2 4.5 1.5
# 删除全为NaN值得行或列
df.dropna(how='all')
A B C D
1 5.1 NaN 1.4 0.2
2 4.9 3.0 1.4 0.2
3 4.7 3.2 NaN 0.2
4 7.0 3.2 4.7 1.4
5 6.4 3.2 4.5 1.5
6 6.9 3.1 4.9 NaN
# 删除行不为4个值的
df.dropna(thresh=4)
A B C D
2 4.9 3.0 1.4 0.2
4 7.0 3.2 4.7 1.4
5 6.4 3.2 4.5 1.5
# 删除B中有NaN值的行
df.dropna(subset=['B'])
A B C D
2 4.9 3.0 1.4 0.2
3 4.7 3.2 NaN 0.2
4 7.0 3.2 4.7 1.4
5 6.4 3.2 4.5 1.5
6 6.9 3.1 4.9 NaN
# 填充nan值
df.fillna(value=0)
A B C D
1 5.1 0.0 1.4 0.2
2 4.9 3.0 1.4 0.2
3 4.7 3.2 0.0 0.2
4 7.0 3.2 4.7 1.4
5 6.4 3.2 4.5 1.5
6 6.9 3.1 4.9 0.0
7 0.0 0.0 0.0 0.0

读取json文件

strtext = '[{"ttery":"min","issue":"20130801-3391","code":"8,4,5,2,9","code1":"297734529","code2":null,"time":1013395466000},
{"ttery":"min","issue":"20130801-3390","code":"7,8,2,1,2","code1":"298058212","code2":null,"time":1013395406000},
{"ttery":"min","issue":"20130801-3389","code":"5,9,1,2,9","code1":"298329129","code2":null,"time":1013395346000},
{"ttery":"min","issue":"20130801-3388","code":"3,8,7,3,3","code1":"298588733","code2":null,"time":1013395286000},
{"ttery":"min","issue":"20130801-3387","code":"0,8,5,2,7","code1":"298818527","code2":null,"time":1013395226000}]'

df = pd.read_json(strtext, orient='records')
df
code code1 code2 issue time ttery
0 8,4,5,2,9 297734529 NaN 20130801-3391 1013395466000 min
1 7,8,2,1,2 298058212 NaN 20130801-3390 1013395406000 min
2 5,9,1,2,9 298329129 NaN 20130801-3389 1013395346000 min
3 3,8,7,3,3 298588733 NaN 20130801-3388 1013395286000 min
4 0,8,5,2,7 298818527 NaN 20130801-3387 1013395226000 min

orient参数的五种形式

orient是表明预期的json字符串格式。orient的设置有以下五个值:

  1. 'split' : dict like {index -> [index], columns -> [columns], data -> [values]}

    这种就是有索引,有列字段,和数据矩阵构成的json格式。key名称只能是index,columns和data。

s = '{"index":[1,2,3],"columns":["a","b"],"data":[[1,3],[2,8],[3,9]]}'
df = pd.read_json(s, orient='split')
df
a b
1 1 3
2 2 8
3 3 9
  1. 'records' : list like [{column -> value}, ... , {column -> value}]

    这种就是成员为字典的列表。如我今天要处理的json数据示例所见。构成是列字段为键,值为键值,每一个字典成员就构成了dataframe的一行数据。

strtext = '[{"ttery":"min","issue":"20130801-3391","code":"8,4,5,2,9","code1":"297734529","code2":null,"time":1013395466000},
{"ttery":"min","issue":"20130801-3390","code":"7,8,2,1,2","code1":"298058212","code2":null,"time":1013395406000}]'

df = pd.read_json(strtext, orient='records')
df
code code1 code2 issue time ttery
0 8,4,5,2,9 297734529 NaN 20130801-3391 1013395466000 min
1 7,8,2,1,2 298058212 NaN 20130801-3390 1013395406000 min
  1. 'index' : dict like {index -> {column -> value}}

    以索引为key,以列字段构成的字典为键值。如:

s = '{"0":{"a":1,"b":2},"1":{"a":9,"b":11}}'
df = pd.read_json(s, orient='index')
df
a b
0 1 2
1 9 11
  1. 'columns' : dict like {column -> {index -> value}}

    这种处理的就是以列为键,对应一个值字典的对象。这个字典对象以索引为键,以值为键值构成的json字符串。如下图所示:

s = '{"a":{"0":1,"1":9},"b":{"0":2,"1":11}}'
df = pd.read_json(s, orient='columns')
df
a b
0 1 2
1 9 11
  1. 'values' : just the values array。

    values这种我们就很常见了。就是一个嵌套的列表。里面的成员也是列表,2层的。

s = '[["a",1],["b",2]]'
df = pd.read_json(s, orient='values')
df
0 1
0 a 1
1 b 2
原文地址:https://www.cnblogs.com/Hades123/p/11738909.html