Pandas
-
- 一 pandas介绍
-
- 1.1DataFrame基本操作
- 1.2 Panel
- 1.3 Series
- 二 pandas基本操作
-
- 2.1索引
- 2.2赋值操作
- 2.3排序操作
- 三 pandas运算
-
- 3.1 算术运算
- 3.2 逻辑运算
- 3.3 统计运算
- 3.4 自定义运算
- 四 pandas画图
- 五 pandas读取文件
-
- 5.1CSV
- 5.2HDF5
- 5.3 JSON
一 pandas介绍
numpy+matplotlib=pandas
panel+data=analysis
优点:
1 便捷的数据处理能力
2 读取文件方便
3 封装了numpy和matplotlib的计算和绘图
三大数据结构:
DateFrame
Panel
Series
1.1DataFrame基本操作
1.1.1结构:
既有行索引又有列索引的二维数组
1.1.2 属性:
shape 形状
index 行索引
columns 列索引
values 值
T 转置
1.1.3 方法:
head(行数) 返回头几行 可以指定显示几行
tail(行数) 返回后几行 可以指定显示几行
import numpy as np
import pandas as pd
#创建10支股票五天的涨跌幅
stock=np.random.normal(0,2,(10,5))
#输出numpy数组
print("numpy显示结果\n"+str(stock))
#创建行索引和列索引数组
indexs=['股票{}'.format(_) for _ in range(1,11)]
colum=["2020-7-{}".format(_) for _ in range(1,6)]
#创建pands数组,指定列索引和行索引
date=pd.DataFrame(stock,index=indexs,columns=colum)
#显示
print("pandas显示结果\n"+str(date))
'''属性'''
#输出形状、行索引、列索引、值、转置
print("形状\n")
print(date.shape)
print("行索引\n")
print(date.index)
print("列索引\n")
print(date.columns)
print("值\n")
print(date.values)
print("转置\n")
print(date.T)
'''方法'''
#获取前几行和后几行
print("默认前五行\n")
print(date.head()) #默认前五行
print("前两行\n")
print(date.head(2))#指定前2行输出
print("默认后五行\n")
print(date.tail())#默认后五行
print("后两行\n")
print(date.tail(2))#指定后两行
D:\pwork\项目二\venv\Scripts\python.exe D:/pwork/项目二/大萨达.py
numpy显示结果
[[ 1.55249408e+00 -2.42446911e+00 -2.06819827e+00 4.17191555e+00
3.05398551e-01]
[ 1.52863883e+00 -4.65139469e-01 -2.58682155e+00 -2.95366664e-03
-3.45655647e-01]
[ 1.67958568e+00 -2.33522926e+00 3.40632104e+00 -5.42360592e+00
-1.13525287e+00]
[ 2.90552103e-01 -2.81693963e+00 -1.23042142e+00 -1.43188518e-01
-2.95087590e+00]
[ 6.71777465e-01 5.70084215e-01 1.13689321e+00 4.36867796e+00
2.26641032e+00]
[-7.00525842e-01 -1.88854273e+00 -1.66895195e+00 1.31804539e+00
-3.45916794e+00]
[ 3.20964923e+00 2.11335194e+00 -1.41114454e+00 2.33933567e-01
-1.74785560e+00]
[ 2.42366447e+00 -3.92334146e+00 2.31240295e+00 1.32739384e+00
9.20478217e-01]
[ 1.13802932e+00 1.41010670e-01 5.88956894e-01 -7.52806500e-01
-1.29353749e+00]
[-8.09834094e-01 -1.31031802e+00 -9.16474423e-02 1.84540315e+00
-8.87270286e-01]]
pandas显示结果
2020-7-1 2020-7-2 2020-7-3 2020-7-4 2020-7-5
股票1 1.552494 -2.424469 -2.068198 4.171916 0.305399
股票2 1.528639 -0.465139 -2.586822 -0.002954 -0.345656
股票3 1.679586 -2.335229 3.406321 -5.423606 -1.135253
股票4 0.290552 -2.816940 -1.230421 -0.143189 -2.950876
股票5 0.671777 0.570084 1.136893 4.368678 2.266410
股票6 -0.700526 -1.888543 -1.668952 1.318045 -3.459168
股票7 3.209649 2.113352 -1.411145 0.233934 -1.747856
股票8 2.423664 -3.923341 2.312403 1.327394 0.920478
股票9 1.138029 0.141011 0.588957 -0.752807 -1.293537
股票10 -0.809834 -1.310318 -0.091647 1.845403 -0.887270
形状
(10, 5)
行索引
Index(['股票1', '股票2', '股票3', '股票4', '股票5', '股票6', '股票7', '股票8', '股票9', '股票10'], dtype='object')
列索引
Index(['2020-7-1', '2020-7-2', '2020-7-3', '2020-7-4', '2020-7-5'], dtype='object')
值
[[ 1.55249408e+00 -2.42446911e+00 -2.06819827e+00 4.17191555e+00
3.05398551e-01]
[ 1.52863883e+00 -4.65139469e-01 -2.58682155e+00 -2.95366664e-03
-3.45655647e-01]
[ 1.67958568e+00 -2.33522926e+00 3.40632104e+00 -5.42360592e+00
-1.13525287e+00]
[ 2.90552103e-01 -2.81693963e+00 -1.23042142e+00 -1.43188518e-01
-2.95087590e+00]
[ 6.71777465e-01 5.70084215e-01 1.13689321e+00 4.36867796e+00
2.26641032e+00]
[-7.00525842e-01 -1.88854273e+00 -1.66895195e+00 1.31804539e+00
-3.45916794e+00]
[ 3.20964923e+00 2.11335194e+00 -1.41114454e+00 2.33933567e-01
-1.74785560e+00]
[ 2.42366447e+00 -3.92334146e+00 2.31240295e+00 1.32739384e+00
9.20478217e-01]
[ 1.13802932e+00 1.41010670e-01 5.88956894e-01 -7.52806500e-01
-1.29353749e+00]
[-8.09834094e-01 -1.31031802e+00 -9.16474423e-02 1.84540315e+00
-8.87270286e-01]]
转置
股票1 股票2 股票3 ... 股票8 股票9 股票10
2020-7-1 1.552494 1.528639 1.679586 ... 2.423664 1.138029 -0.809834
2020-7-2 -2.424469 -0.465139 -2.335229 ... -3.923341 0.141011 -1.310318
2020-7-3 -2.068198 -2.586822 3.406321 ... 2.312403 0.588957 -0.091647
2020-7-4 4.171916 -0.002954 -5.423606 ... 1.327394 -0.752807 1.845403
2020-7-5 0.305399 -0.345656 -1.135253 ... 0.920478 -1.293537 -0.887270
[5 rows x 10 columns]
默认前五行
2020-7-1 2020-7-2 2020-7-3 2020-7-4 2020-7-5
股票1 1.552494 -2.424469 -2.068198 4.171916 0.305399
股票2 1.528639 -0.465139 -2.586822 -0.002954 -0.345656
股票3 1.679586 -2.335229 3.406321 -5.423606 -1.135253
股票4 0.290552 -2.816940 -1.230421 -0.143189 -2.950876
股票5 0.671777 0.570084 1.136893 4.368678 2.266410
前两行
2020-7-1 2020-7-2 2020-7-3 2020-7-4 2020-7-5
股票1 1.552494 -2.424469 -2.068198 4.171916 0.305399
股票2 1.528639 -0.465139 -2.586822 -0.002954 -0.345656
默认后五行
2020-7-1 2020-7-2 2020-7-3 2020-7-4 2020-7-5
股票6 -0.700526 -1.888543 -1.668952 1.318045 -3.459168
股票7 3.209649 2.113352 -1.411145 0.233934 -1.747856
股票8 2.423664 -3.923341 2.312403 1.327394 0.920478
股票9 1.138029 0.141011 0.588957 -0.752807 -1.293537
股票10 -0.809834 -1.310318 -0.091647 1.845403 -0.887270
后两行
2020-7-1 2020-7-2 2020-7-3 2020-7-4 2020-7-5
股票9 1.138029 0.141011 0.588957 -0.752807 -1.293537
股票10 -0.809834 -1.310318 -0.091647 1.845403 -0.887270
进程已结束,退出代码 0
1.1.4 修改行列索引值
获取索引
date.index[]
修改索引只能整体修改
date.index=新数组
1.1.5重设索引
date.reset_index()
将会多一列
date.reset_index(drop=true)
索引值会变成原始状态
1.1.6设置新索引
以某一列数据作为行索引
import pandas as pd
#也可通过字典来设置dateframe
date=pd.DataFrame({'month':[1,2,3,7],
'year':[2012,2014,2016,2018],
'sale':[55,43,67,88]})
#输出原始数组
print(date)
#输出设置后的数组,drop表示是否扔掉初始索引
date1=date.set_index('month',drop=True)
print(date1)
date2=date.set_index('month',drop=False)
print(date2)
D:\pwork\项目二\venv\Scripts\python.exe D:/pwork/项目二/jk.py
month year sale
0 1 2012 55
1 2 2014 43
2 3 2016 67
3 7 2018 88
year sale
month
1 2012 55
2 2014 43
3 2016 67
7 2018 88
month year sale
month
1 1 2012 55
2 2 2014 43
3 3 2016 67
7 7 2018 88
进程已结束,退出代码 0
也可以设置两列作为索引
需要传入一个列表
1.1.7 多级索引 MultiIndex
index属性:
names:levels的名称
levels:每个leves的元组值
date.index.names
date.index.levels
1.2 Panel
三维数组数据结构
dateFrame的容器
1.2.1 创建方法
1.2.2 查看方法
从items:p[“A”]
从major:p.major_xs(‘2013-01-01’)
从minor:p.minor_xs( ’ first ’ )
1.3 Series
带索引的一维数组
获取一维数组
sr=date.iloc[1,:]
import pandas as pd
#也可通过字典来设置dateframe
date=pd.DataFrame({'month':[1,2,3,7],
'year':[2012,2014,2016,2018],
'sale':[55,43,67,88]})
#输出原始数组
print(date)
#获取第2行数据
sr=date.iloc[1,:]
print(sr)
D:\pwork\项目二\venv\Scripts\python.exe D:/pwork/项目二/jk.py
month year sale
0 1 2012 55
1 2 2014 43
2 3 2016 67
3 7 2018 88
month 2
year 2014
sale 43
Name: 1, dtype: int64
进程已结束,退出代码 0
属性
sr.index
sr.values
1.3.1 创建Series
np.arange()创建一维数组
index指定索引
创建Seies
也可以通过字典创建
import pandas as pd
import numpy as np
sr=pd.Series(np.arange(8),index=['a','b','c','d','e','f','g','h'])
print(sr)
a 0
b 1
c 2
d 3
e 4
f 5
g 6
h 7
dtype: int32
二 pandas基本操作
先准备好数组
先读取csv文件
csv文件内容如下
import pandas as pd
#设置编码方式gbk,读取文件practice.csv
r=pd.read_csv('practice.csv',encoding='gbk')
#设置时间为行索引
r1=r.set_index('时间')
print(r1)
读取结果
D:\pwork\项目二\venv\Scripts\python.exe D:/pwork/项目二/14.56.py
总消费 总收入 食品 网购 通讯 玉兰卡充值 交通 其他
时间
2020.6.01 73.9 0 21.7 52.2 0 0 0 0
2020.6.02 0.0 0 0 0.0 0 0 0 0
2020.6.03 0.0 0 0 0.0 0 0 0 0
2020.6.04 143.0 100 123 0.0 20 0 0 0
2020.6.05 0.0 0 0 0.0 0 0 0 0
2020.6.06 2.0 0 0 0.0 0 0 0 2
2020.6.07 5.0 0 0 0.0 0 0 0 0
2020.6.08 21.0 0 21 0.0 0 0 0 0
2020.6.09 30.4 200 30.4 0.0 0 0 0 0
2020.6.10 18.0 0 15 0.0 0 0 0 3
2020.6.11 31.5 0 16.5 0.0 0 0 15 0
2020.6.12 0.0 0 0 0.0 0 0 0 0
2020.6.13 293.8 0 #REF! 9.9 0 0 239 0
2020.6.14 77.7 0 77.7 0.0 0 0 0 0
2020.6.15 50.0 0 50 0.0 0 0 0 0
2020.6.16 6.0 0 6 0.0 0 0 0 0
2020.6.17 15.7 0 15.7 0.0 0 0 0 0
2020.6.18 0.0 0 0 0.0 0 0 0 0
2020.6.19 10.0 0 0 0.0 0 0 0 10
2020.6.20 54.0 0 41 0.0 0 0 0 13
2020.6.21 66.0 100 34 0.0 22 0 10 0
2020.6.22 49.0 0 29 0.0 20 0 0 0
2020.6.23 129.0 500 29 0.0 0 100 0 0
2020.6.24 249.8 0 29 220.8 0 0 0 0
2020.6.25 29.0 0 0 0.0 0 0 0 0
2020.6.26 0.0 0 0 0.0 0 0 0 0
2020.6.27 0.0 0 0 0.0 0 0 0 0
2020.6.28 0.0 0 0 0.0 0 0 0 0
2020.6.29 0.0 0 0 0.0 0 0 0 0
2020.6.30 218.0 0 12 0.0 0 200 0 6
进程已结束,退出代码 0
2.1索引
1 直接索引
先列后行
import pandas as pd
#设置编码方式gbk,读取文件practice.csv
r=pd.read_csv('practice.csv',encoding='gbk')
#设置时间为行索引
r1=r.set_index('时间')
print(r1['总消费']['2020.6.01'])
D:\pwork\项目二\venv\Scripts\python.exe D:/pwork/项目二/14.56.py
73.9
进程已结束,退出代码 0
2 按名字索引
可以不先列后行
import pandas as pd
#设置编码方式gbk,读取文件practice.csv
r=pd.read_csv('practice.csv',encoding='gbk')
#设置时间为行索引
r1=r.set_index('时间')
print(r1.loc['2020.6.01']['总消费'])
D:\pwork\项目二\venv\Scripts\python.exe D:/pwork/项目二/14.56.py
73.9
进程已结束,退出代码 0
3 按数字索引
可以进行切片,类似numpy
import pandas as pd
#设置编码方式gbk,读取文件practice.csv
r=pd.read_csv('practice.csv',encoding='gbk')
#设置时间为行索引
r1=r.set_index('时间')
print(r1.iloc[0,0])
D:\pwork\项目二\venv\Scripts\python.exe D:/pwork/项目二/14.56.py
73.9
进程已结束,退出代码 0
4 组合索引
似乎不支持了
import pandas as pd
#设置编码方式gbk,读取文件practice.csv
r=pd.read_csv('practice.csv',encoding='gbk')
#设置时间为行索引
r1=r.set_index('时间')
print(r1.ix[0:4,['总消费','总收入','食品']])
Traceback (most recent call last):
File "D:/pwork/项目二/14.56.py", line 7, in <module>
print(r1.ix[0:4,['总消费','总收入','食品']])
File "D:\pwork\项目二\venv\lib\site-packages\pandas\core\generic.py", line 5274, in __getattr__
return object.__getattribute__(self, name)
AttributeError: 'DataFrame' object has no attribute 'ix'
2.2赋值操作
利用索引赋值
import pandas as pd
#设置编码方式gbk,读取文件practice.csv
r=pd.read_csv('practice.csv',encoding='gbk')
#设置时间为行索引
r1=r.set_index('时间')
r1['总消费']=100
print(r1['总消费'])
时间
2020.6.01 100
2020.6.02 100
2020.6.03 100
2020.6.04 100
2020.6.05 100
2020.6.06 100
2020.6.07 100
2020.6.08 100
2020.6.09 100
2020.6.10 100
2020.6.11 100
2020.6.12 100
2020.6.13 100
2020.6.14 100
2020.6.15 100
2020.6.16 100
2020.6.17 100
2020.6.18 100
2020.6.19 100
2020.6.20 100
2020.6.21 100
2020.6.22 100
2020.6.23 100
2020.6.24 100
2020.6.25 100
2020.6.26 100
2020.6.27 100
2020.6.28 100
2020.6.29 100
2020.6.30 100
Name: 总消费, dtype: int64
2.3排序操作
DataFrame
Series 可以省略 by= ‘ ’
第一个参数为列索引 第二个参数表示是否升序排列
r1.sort_value(by='',ascending=True)
r1.sort_value(by='',ascending=False)
按照总消费升序排列
import pandas as pd
#设置编码方式gbk,读取文件practice.csv
r=pd.read_csv('practice.csv',encoding='gbk')
#设置时间为行索引
r1=r.set_index('时间')
r2=r1.sort_values(by='总消费',ascending=True)
print(r2)
总消费 总收入 食品 网购 通讯 玉兰卡充值 交通 其他
时间
2020.6.12 0.0 0 0 0.0 0 0 0 0
2020.6.02 0.0 0 0 0.0 0 0 0 0
2020.6.03 0.0 0 0 0.0 0 0 0 0
2020.6.28 0.0 0 0 0.0 0 0 0 0
2020.6.05 0.0 0 0 0.0 0 0 0 0
2020.6.18 0.0 0 0 0.0 0 0 0 0
2020.6.27 0.0 0 0 0.0 0 0 0 0
2020.6.26 0.0 0 0 0.0 0 0 0 0
2020.6.29 0.0 0 0 0.0 0 0 0 0
2020.6.06 2.0 0 0 0.0 0 0 0 2
2020.6.07 5.0 0 0 0.0 0 0 0 0
2020.6.16 6.0 0 6 0.0 0 0 0 0
2020.6.19 10.0 0 0 0.0 0 0 0 10
2020.6.17 15.7 0 15.7 0.0 0 0 0 0
2020.6.10 18.0 0 15 0.0 0 0 0 3
2020.6.08 21.0 0 21 0.0 0 0 0 0
2020.6.25 29.0 0 0 0.0 0 0 0 0
2020.6.09 30.4 200 30.4 0.0 0 0 0 0
2020.6.11 31.5 0 16.5 0.0 0 0 15 0
2020.6.22 49.0 0 29 0.0 20 0 0 0
2020.6.15 50.0 0 50 0.0 0 0 0 0
2020.6.20 54.0 0 41 0.0 0 0 0 13
2020.6.21 66.0 100 34 0.0 22 0 10 0
2020.6.01 73.9 0 21.7 52.2 0 0 0 0
2020.6.14 77.7 0 77.7 0.0 0 0 0 0
2020.6.23 129.0 500 29 0.0 0 100 0 0
2020.6.04 143.0 100 123 0.0 20 0 0 0
2020.6.30 218.0 0 12 0.0 0 200 0 6
2020.6.24 249.8 0 29 220.8 0 0 0 0
2020.6.13 293.8 0 #REF! 9.9 0 0 239 0
三 pandas运算
3.1 算术运算
Series操作:
r1['']+10 r1[''].add(10)
r1['']-10 r1[''].sub(10)
r1['']*10 r1[''].mul(10)
r1['']/10 r1[''].div(10)
DataFrame操作
r1+10 r1.add(10)
r1-10 r1.sub(10)
r1*10 r1.mul(10)
r1/10 r1.div(10)
+ - * /
import pandas as pd
#设置编码方式gbk,读取文件practice.csv
r=pd.read_csv('practice.csv',encoding='gbk')
#设置时间为行索引
r1=r.set_index('时间')
r2=r1["总消费"]+100
print(r2)
D:\pwork\项目二\venv\Scripts\python.exe D:/pwork/项目二/14.56.py
时间
2020.6.01 173.9
2020.6.02 100.0
2020.6.03 100.0
2020.6.04 243.0
2020.6.05 100.0
2020.6.06 102.0
2020.6.07 105.0
2020.6.08 121.0
2020.6.09 130.4
2020.6.10 118.0
2020.6.11 131.5
2020.6.12 100.0
2020.6.13 393.8
2020.6.14 177.7
2020.6.15 150.0
2020.6.16 106.0
2020.6.17 115.7
2020.6.18 100.0
2020.6.19 110.0
2020.6.20 154.0
2020.6.21 166.0
2020.6.22 149.0
2020.6.23 229.0
2020.6.24 349.8
2020.6.25 129.0
2020.6.26 100.0
2020.6.27 100.0
2020.6.28 100.0
2020.6.29 100.0
2020.6.30 318.0
Name: 总消费, dtype: float64
3.2 逻辑运算
运算符
例子 + - * / add sub mul div
运算函数
r1.query(“ 条件 ”)
r1.isin(列表)
一 运算符
< > | &
r1['总消费']>2 返回布尔值
r1['总消费']>2 & r1['总消费']<10 返回布尔值
筛选数据
可以利用布尔索引值进行操作
r1[r1['总消费']>2 & r1['总消费']<10] 将返回满足条件的值
例子:
import pandas as pd
#设置编码方式gbk,读取文件practice.csv
r=pd.read_csv('practice.csv',encoding='gbk')
#设置时间为行索引
r1=r.set_index('时间')
print(r1[(r1['总消费']>0) & (r1['总消费']<10)] )
D:\pwork\项目二\venv\Scripts\python.exe D:/pwork/项目二/14.56.py
总消费 总收入 食品 网购 通讯 玉兰卡充值 交通 其他
时间
2020.6.06 2.0 0 0 0.0 0 0 0 2
2020.6.07 5.0 0 0 0.0 0 0 0 0
2020.6.16 6.0 0 6 0.0 0 0 0 0
进程已结束,退出代码 0
二 运算函数
r1.query(“ 条件 ”)
例子
import pandas as pd
#设置编码方式gbk,读取文件practice.csv
r=pd.read_csv('practice.csv',encoding='gbk')
#设置时间为行索引
r1=r.set_index('时间')
r2=r1.query("总消费> 0 & 总消费<10")
print(r2)
D:\pwork\项目二\venv\Scripts\python.exe D:/pwork/项目二/14.56.py
总消费 总收入 食品 网购 通讯 玉兰卡充值 交通 其他
时间
2020.6.06 2.0 0 0 0.0 0 0 0 2
2020.6.07 5.0 0 0 0.0 0 0 0 0
2020.6.16 6.0 0 6 0.0 0 0 0 0
进程已结束,退出代码 0
r1.isin()
判断某些值是否在指定集合里
例子:
import pandas as pd
#设置编码方式gbk,读取文件practice.csv
r=pd.read_csv('practice.csv',encoding='gbk')
#设置时间为行索引
r1=r.set_index('时间')
r2=r1[r1['总消费'].isin([12,23,4,54,67,1,2,3,5,6])]
print(r2)
D:\pwork\项目二\venv\Scripts\python.exe D:/pwork/项目二/14.56.py
总消费 总收入 食品 网购 通讯 玉兰卡充值 交通 其他
时间
2020.6.06 2.0 0 0 0.0 0 0 0 2
2020.6.07 5.0 0 0 0.0 0 0 0 0
2020.6.16 6.0 0 6 0.0 0 0 0 0
2020.6.20 54.0 0 41 0.0 0 0 0 13
进程已结束,退出代码 0
3.3 统计运算
1 统计数据 describe()
返回这一组数的一些统计学数据
count mean std max min 25% 50% 75%
import pandas as pd
#设置编码方式gbk,读取文件practice.csv
r=pd.read_csv('practice.csv',encoding='gbk')
#设置时间为行索引
r1=r.set_index('时间')
print(r1.describe())
D:\pwork\项目二\venv\Scripts\python.exe D:/pwork/项目二/14.56.py
总消费 总收入 网购 ... 玉兰卡充值 交通 其他
count 30.000000 30.000000 30.000000 ... 30.00000 30.000000 30.000000
mean 52.426667 30.000000 9.430000 ... 10.00000 8.800000 1.133333
std 78.450779 98.785731 41.066701 ... 40.25779 43.598007 3.104317
min 0.000000 0.000000 0.000000 ... 0.00000 0.000000 0.000000
25% 0.000000 0.000000 0.000000 ... 0.00000 0.000000 0.000000
50% 19.500000 0.000000 0.000000 ... 0.00000 0.000000 0.000000
75% 63.000000 0.000000 0.000000 ... 0.00000 0.000000 0.000000
max 293.800000 500.000000 220.800000 ... 200.00000 239.000000 13.000000
[8 rows x 7 columns]
进程已结束,退出代码 0
也可直接调用这些统计学数据
r1['总消费'].max() 、 r1.max()
返回索引值
r1.idxmax()
r1.idxmin()
r1['总消费'].idxmax()
2 累计统计 cumsum()
依次计算累加值
import pandas as pd
#设置编码方式gbk,读取文件practice.csv
r=pd.read_csv('practice.csv',encoding='gbk')
#设置时间为行索引
r1=r.set_index('时间')
print(r1['总消费'].cumsum())
D:\pwork\项目二\venv\Scripts\python.exe D:/pwork/项目二/14.56.py
时间
2020.6.01 73.9
2020.6.02 73.9
2020.6.03 73.9
2020.6.04 216.9
2020.6.05 216.9
2020.6.06 218.9
2020.6.07 223.9
2020.6.08 244.9
2020.6.09 275.3
2020.6.10 293.3
2020.6.11 324.8
2020.6.12 324.8
2020.6.13 618.6
2020.6.14 696.3
2020.6.15 746.3
2020.6.16 752.3
2020.6.17 768.0
2020.6.18 768.0
2020.6.19 778.0
2020.6.20 832.0
2020.6.21 898.0
2020.6.22 947.0
2020.6.23 1076.0
2020.6.24 1325.8
2020.6.25 1354.8
2020.6.26 1354.8
2020.6.27 1354.8
2020.6.28 1354.8
2020.6.29 1354.8
2020.6.30 1572.8
Name: 总消费, dtype: float64
进程已结束,退出代码 0
3.4 自定义运算
r1.apply(func ,axis=0)
func=自定义函数
axis=0 默认按列
举个例子 lambda
似乎有问题 ,以后再来修改
import pandas as pd
#设置编码方式gbk,读取文件practice.csv
r=pd.read_csv('practice.csv',encoding='gbk')
#设置时间为行索引
r1=r.set_index('时间')
print(r1.apply( b=lambda x: x.max()-x.min() ,axis=0))
四 pandas画图
x指定x轴坐标,标签
y指定y坐标,标签
xy共同决定绘制的点或折线
king指定绘图类型
这三行必须添加,指定字体
plt.rcParams[‘font.sans-serif’]=[‘SimHei’]#显示中文标签
plt.rcParams[‘axes.unicode_minus’]=False
plt.show()
import matplotlib.pyplot as plt
import pandas as pd
#设置编码方式gbk,读取文件practice.csv
r=pd.read_csv('practice.csv',encoding='gbk')
#设置时间为行索引
r1=r.set_index('时间')
r.plot(x='时间',y='总消费',kind='line')
plt.rcParams['font.sans-serif']=['SimHei']#显示中文标签
plt.rcParams['axes.unicode_minus']=False
plt.show()
运行结果:
五 pandas读取文件
5.1CSV
如果第一行直接就是数据,表格将把第一行作为列索引
此时可使用names=【‘’,‘’,‘’,‘’,‘’】设置列索引
usecols设置要提取的字段
import pandas as pd
#设置编码方式gbk,读取文件practice.csv
r=pd.read_csv('practice.csv',usecols=['时间','总消费'],encoding='gbk')
r1=r.set_index('时间')
print(r1)
D:\pwork\项目二\venv\Scripts\python.exe D:/pwork/项目二/14.56.py
总消费
时间
2020.6.01 73.9
2020.6.02 0.0
2020.6.03 0.0
2020.6.04 143.0
2020.6.05 0.0
2020.6.06 2.0
2020.6.07 5.0
2020.6.08 21.0
2020.6.09 30.4
2020.6.10 18.0
2020.6.11 31.5
2020.6.12 0.0
2020.6.13 293.8
2020.6.14 77.7
2020.6.15 50.0
2020.6.16 6.0
2020.6.17 15.7
2020.6.18 0.0
2020.6.19 10.0
2020.6.20 54.0
2020.6.21 66.0
2020.6.22 49.0
2020.6.23 129.0
2020.6.24 249.8
2020.6.25 29.0
2020.6.26 0.0
2020.6.27 0.0
2020.6.28 0.0
2020.6.29 0.0
2020.6.30 218.0
进程已结束,退出代码 0
dataframe.to_csv(‘文件路径’)
#把r1列表前10行读入123.csv文件,
#columns=['','','']设置要保存的列
#index=True or False 设置是否保留索引
# mode='w' 覆盖重写
# mode='a' 在文件尾部追加
# header =True or False 是否写进列索引值
# header =['','','',''] 设置列索引值
r1[0:10].to_csv('123.csv',columns=['',''])
5.2HDF5
二进制文件无法直接查看
可以存储三维数组
通过指定key,读取不同维度的表
读取和存储必须指定key值
5.3 JSON
需要注意
orient=’records‘
lines=True 按行处理
需要注意
orient=’records‘
lines=True 按行处理