pandas系列—pandas库的DataFrame类型

一、DataFrame概述


1. DataFrame类型由共用相同索引的一组列组成。 
2. DataFrame是一个表格型的数据类型,每列值类型可以不同。 
3. DataFrame常用于表达二维数据,也可表达多维数据。 
4. DataFrame既有行索引(index),也有列索引(column)

二、DataFrame类型的创建

DataFrame类型可由以下类型创建: 
1. 二维ndarray对象 
2. 由一维ndarray、列表、字典、元组或Series构成的字典 
3. Series类型 
4. 其他的DataFrame类型

2.1 从二维ndarray对象创建

import pandas as pd
import numpy as np

d = pd.DataFrame(np.arange(10).reshape(2,5))

d
Out[58]: 
   0  1  2  3  4
0  0  1  2  3  4
1  5  6  7  8  9
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

2.2 从二维ndarray对象字典创建

import pandas as pd

dt = {'one':pd.Series([1,2,3],index=['a','b','c']),
      'two':pd.Series([9,8,7,6],index=['a','b','c','d'])}
d = pd.DataFrame(dt)

d
Out[69]: 
   one  two
a  1.0    9
b  2.0    8
c  3.0    7
d  NaN    6

pd.DataFrame(dt,index=['b','c','d'],columns=['two','three'])
Out[71]: 
   two three
b    8   NaN
c    7   NaN
d    6   NaN
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

2.3 从列表类型的字典创建

import pandas as pd

dl = {'one':[1,2,3,4],
      'two':[9,8,7,6]}
f = pd.DataFrame(dl,index=['a','b','c','d'])

f
Out[75]: 
   one  two
a    1    9
b    2    8
c    3    7
d    4    6
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

再来看一个例子:

import pandas as pd

dl = {'城市':['北京','上海','广州','深圳','沈阳'],
      '环比':[101.5,101.2,101.3,102.0,100.1],
      '同比':[120.7,127.3,119.4,140.0,101.4],
      '定基':[121.4,127.8,120.0,145.5,101.6]
      }

g = pd.DataFrame(dl,index=['c1','c2','c3','c4','c5'])

g.index
Out[75]: Index(['c1', 'c2', 'c3', 'c4', 'c5'], dtype='object')

g.columns
Out[76]: Index(['同比', '城市', '定基', '环比'], dtype='object')

g.values
Out[77]: 
array([[120.7, '北京', 121.4, 101.5],
       [127.3, '上海', 127.8, 101.2],
       [119.4, '广州', 120.0, 101.3],
       [140.0, '深圳', 145.5, 102.0],
       [101.4, '沈阳', 101.6, 100.1]], dtype=object)

g['同比']
Out[78]: 
c1    120.7
c2    127.3
c3    119.4
c4    140.0
c5    101.4
Name: 同比, dtype: float64

g.ix['c2']
__main__:1: 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#deprecate_ix
Out[79]: 
同比    127.3
城市       上海
定基    127.8
环比    101.2
Name: c2, dtype: object

g['同比']['c2']
Out[80]: 127.3

 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51

注:DataFrame是二维带“标签”数组

pandas系列—pandas库的数据类型操作

[TOC] 

如何改变Series和DataFrame对象: 
1. 增加或重排:重新索引 
2. 删除:drop 

一、重新索引

1.1 重新索引的方法


.reindex( )能够改变或重排Series和DataFrame索引 

例子1:

 import pandas as pd

dl = {'城市':['北京','上海','广州','深圳','沈阳'],
      '环比':[101.5,101.2,101.3,102.0,100.1],
      '同比':[120.7,127.3,119.4,140.0,101.4],
      '定基':[121.4,127.8,120.0,145.5,101.6]
      }

d = pd.DataFrame(dl,index=['c1','c2','c3','c4','c5'])

d
Out[4]: 
       同比  城市     定基     环比
c1  120.7  北京  121.4  101.5
c2  127.3  上海  127.8  101.2
c3  119.4  广州  120.0  101.3
c4  140.0  深圳  145.5  102.0
c5  101.4  沈阳  101.6  100.1

d = d.reindex(index=['c5','c4','c3','c2','c1'])

d
Out[6]: 
       同比  城市     定基     环比
c5  101.4  沈阳  101.6  100.1
c4  140.0  深圳  145.5  102.0
c3  119.4  广州  120.0  101.3
c2  127.3  上海  127.8  101.2
c1  120.7  北京  121.4  101.5

d = d.reindex(columns=['城市','同比','环比','定基'])

d
Out[8]: 
    城市     同比     环比     定基
c5  沈阳  101.4  100.1  101.6
c4  深圳  140.0  102.0  145.5
c3  广州  119.4  101.3  120.0
c2  上海  127.3  101.2  127.8
c1  北京  120.7  101.5  121.4

 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41


.reindex(index=None,columns=None,…)的参数: 

参数 说明
index,columns 新的行列自定义索引
fill_value 重新索引,用于填充缺失位置的值
method 填充方法,ffill当前值向前填充,bfill向后填充
limit 最大填充量
copy 默认True,生成新的对象,False时,新旧相等,不复制

例子2(接上述例子1):

newc = d.columns.insert(4,'新增')
newd = d.reindex(columns=newc,fill_value=200)

newd
Out[13]: 
    城市     同比     环比     定基   新增
c5  沈阳  101.4  100.1  101.6  200
c4  深圳  140.0  102.0  145.5  200
c3  广州  119.4  101.3  120.0  200
c2  上海  127.3  101.2  127.8  200
c1  北京  120.7  101.5  121.4  200
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

 1.2 索引类型

例子3(接上述例子1)

d.index
Out[14]: Index(['c5', 'c4', 'c3', 'c2', 'c1'], dtype='object')

d.columns
Out[15]: Index(['城市', '同比', '环比', '定基'], dtype='object')
 
  • 1
  • 2
  • 3
  • 4
  • 5

注1:Series和DataFrame的索引是Index类型 
注2:Index对象时不可修改类型
 
索引类型的常用方法:

方法 说明
.append(idx) 连接另一个Index对象,产生新的Index对象
.diff(idx) 计算差集,产生新的Index对象
.intersection(idx) 计算交集
.union(idx) 计算并集
.delete(loc) 删除loc位置处的元素
.insert(loc,e) 在loc位置增加一个元素e

例子4:

import pandas as pd

dl = {'城市':['北京','上海','广州','深圳','沈阳'],
      '环比':[101.5,101.2,101.3,102.0,100.1],
      '同比':[120.7,127.3,119.4,140.0,101.4],
      '定基':[121.4,127.8,120.0,145.5,101.6]
      }

d=pd.DataFrame(dl,index=['c1','c2','c3','c4','c5'])

d
Out[27]: 
       同比  城市     定基     环比
c1  120.7  北京  121.4  101.5
c2  127.3  上海  127.8  101.2
c3  119.4  广州  120.0  101.3
c4  140.0  深圳  145.5  102.0
c5  101.4  沈阳  101.6  100.1

nc = d.columns.delete(2)
ni = d.index.insert(5,'c0')
nd = d.reindex(index=ni,columns=nc,method='ffill')

nd
Out[31]: 
       同比   城市     环比
c1  120.7   北京  101.5
c2  127.3   上海  101.2
c3  119.4   广州  101.3
c4  140.0   深圳  102.0
c5  101.4   沈阳  100.1
c0    NaN  NaN    NaN

 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

二、删除指定索引对象


.drop( )能删除Series和DataFrame指定行或列索引
 
例子:

import pandas as pd

a = pd.Series([9,8,7,6],index=['a','b','c','d'])

a
Out[33]: 
a    9
b    8
c    7
d    6
dtype: int64

a.drop(['b','c'])
Out[34]: 
a    9
d    6
dtype: int64

 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

再来看一下上面那个城市的例子:

d.drop('c5')
Out[35]: 
       同比  城市     定基     环比
c1  120.7  北京  121.4  101.5
c2  127.3  上海  127.8  101.2
c3  119.4  广州  120.0  101.3
c4  140.0  深圳  145.5  102.0

d.drop('同比',axis=1)
Out[36]: 
    城市     定基     环比
c1  北京  121.4  101.5
c2  上海  127.8  101.2
c3  广州  120.0  101.3
c4  深圳  145.5  102.0
c5  沈阳  101.6  100.1
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

参考资料:北京理工大学嵩天老师教学视频