四、pandas系列---pandas库的数据类型操作

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


.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(接上述例子1)

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

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

注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

二、删除指定索引对象


.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

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

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

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

猜你喜欢

转载自blog.csdn.net/skyli114/article/details/77719408