MOOC《数据分析与展示》之Pandas 课堂笔记


#python常用基础包
import matplotlib.pyplot as plt
import pylab as py
import math as m
import scipy.stats as stats
import numpy as np
import pandas as pd

pandas的一维数据类型:Series

a=pd.Series([9,8,7,6])
a
0    9
1    8
2    7
3    6
dtype: int64

自定义索引

b=pd.Series([4,5,7,9],index=['a','b','c','d'])
b
a    4
b    5
c    7
d    9
dtype: int64

pandas的多维维数据类型:DataFrame

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

使用字典对象创建DataFrame

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
 onetwo
a1.09
b2.08
c3.07
dNaN6
pd.DataFrame(dt,index=['b','c'])
 onetwo
b28
c37
d1={'one':[1,2,3,4],'two':[9,8,7,6]}
d=pd.DataFrame(d1,index=['a','b','c','d'])
d
 onetwo
a19
b28
c37
d46
dl={'城市':['北京','上海','广州','深圳','沈阳'],
    '环比':[101.5,101.2,101.3,102.0,100.1],
    '同比':[120.7,127.3,119.4,140.9,101.4],
    '定基':[121.4,127.8,120.0,145.5,101.6]}
d=pd.DataFrame(dl,index=['c1','c2','c3','c4','c5'])
d
 同比城市定基环比
c1120.7北京121.4101.5
c2127.3上海127.8101.2
c3119.4广州120.0101.3
c4140.9深圳145.5102.0
c5101.4沈阳101.6100.1

获取DataFrame里的数据

d.index
Index(['c1', 'c2', 'c3', 'c4', 'c5'], dtype='object')
d.ix['c1']
同比    120.7
城市       北京
定基    121.4
环比    101.5
Name: c1, dtype: object
d['城市']
c1    北京
c2    上海
c3    广州
c4    深圳
c5    沈阳
Name: 城市, dtype: object
d.values
array([[120.7, '北京', 121.4, 101.5],
       [127.3, '上海', 127.8, 101.2],
       [119.4, '广州', 120.0, 101.3],
       [140.9, '深圳', 145.5, 102.0],
       [101.4, '沈阳', 101.6, 100.1]], dtype=object)
d['城市']['c1']
'北京'

调整列序

d=d.reindex(columns=['城市','环比','同比','定基'])
d
 城市环比同比定基
c1北京101.5120.7121.4
c2上海101.2127.3127.8
c3广州101.3119.4120.0
c4深圳102.0140.9145.5
c5沈阳100.1101.4101.6

Index增删操作

newc=d.columns.insert(4,'新增')
dd=d.reindex(columns=newc,fill_value=20)
dd
 城市环比同比定基新增
c1北京101.5120.7121.420
c2上海101.2127.3127.820
c3广州101.3119.4120.020
c4深圳102.0140.9145.520
c5沈阳100.1101.4101.620
nc=d.columns.delete(2)
ni=d.index.insert(5,'c0').delete(2)
nd=d.reindex(index=ni,columns=nc,method='bfill')
nd
 城市环比定基
c1北京101.5121.4
c2上海101.2127.8
c4深圳102.0145.5
c5沈阳100.1101.6
c0北京101.5121.4

drop()直接删除索引对象(axis默认为0)

nd.drop('c1',axis=0)
 城市环比定基
c2上海101.2127.8
c4深圳102.0145.5
c5沈阳100.1101.6
c0北京101.5121.4
nd.drop('城市',axis=1)
 环比定基
c1101.5121.4
c2101.2127.8
c4102.0145.5
c5100.1101.6
c0101.5121.4

数据类型的算数运算

#Index不同值的二维DataFrame间的运算
a=pd.DataFrame(np.arange(12).reshape(3,4))
b=pd.DataFrame(np.arange(20).reshape(4,5))
a+b
 01234
00.02.04.06.0NaN
19.011.013.015.0NaN
218.020.022.024.0NaN
3NaNNaNNaNNaNNaN
a.mul(b,fill_value=1)
 01234
00.01.04.09.04.0
120.030.042.056.09.0
280.099.0120.0143.014.0
315.016.017.018.019.0
#DataFrame和Series间的运算
c=pd.Series(np.arange(4))
b+c
 01234
00.02.04.06.0NaN
15.07.09.011.0NaN
210.012.014.016.0NaN
315.017.019.021.0NaN
b.sub(c,axis=0)
 01234
001234
145678
289101112
31213141516

比较运算(只能同Index值的DataFrame,或广播运算)

bb=b+c
b>=bb
 01234
0TrueFalseFalseFalseFalse
1TrueFalseFalseFalseFalse
2TrueFalseFalseFalseFalse
3TrueFalseFalseFalseFalse

数据的排序

sort_index()

b=pd.DataFrame(np.arange(20).reshape(4,5),index=['c','a','d','b'])
b
 01234
c01234
a56789
d1011121314
b1516171819
b.sort_index(axis=1,ascending=0)
 43210
c43210
a98765
d1413121110
b1918171615

sort_values

b.sort_values(3,axis=0,ascending=False)
 01234
b1516171819
d1011121314
a56789
c01234

基本统计分析

describe()

c=pd.Series([9,8,7,6],index=['a','b','c','d'])
c.describe()
count    4.000000
mean     7.500000
std      1.290994
min      6.000000
25%      6.750000
50%      7.500000
75%      8.250000
max      9.000000
dtype: float64
c.describe()['mean']
7.5
b.describe()
 01234
count4.0000004.0000004.0000004.0000004.000000
mean7.5000008.5000009.50000010.50000011.500000
std6.4549726.4549726.4549726.4549726.454972
min0.0000001.0000002.0000003.0000004.000000
25%3.7500004.7500005.7500006.7500007.750000
50%7.5000008.5000009.50000010.50000011.500000
75%11.25000012.25000013.25000014.25000015.250000
max15.00000016.00000017.00000018.00000019.000000
b.describe()[0]['max']
15.0

累计统计分析

b.cumsum(axis=1)
 01234
c013610
a511182635
d1021334660
b1531486685
b.rolling(3).sum()
 01234
cNaNNaNNaNNaNNaN
aNaNNaNNaNNaNNaN
d15.018.021.024.027.0
b30.033.036.039.042.0

相关性实例:房价增幅与M2增幅相关性

hprice=pd.Series([3.04,22.93,12.75,22.6,12.33],index=['2008','2009','2010','2011','2012'])
m2=pd.Series([8.18,18.38,9.13,7.82,6.69],index=['2008','2009','2010','2011','2012'])
hprice.plot(marker='o')
m2.plot(marker='*')
plt.xlabel('年份',fontproperties='SimHei',fontsize=15)
plt.ylabel(r'增幅比例%',fontproperties='SimHei',fontsize=15)
plt.show()
hprice.corr(m2)
0.5239439145220387
a = pd.Series([9, 8, 7, 6], ['a', 'b', 'c', 'd'])
a.index.dtype
dtype('O')

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值