目录
Pandas介绍
1. 基于NumPy的工具,为解决数据分析任务而创建的,纳入了大量库和一些标准的数据模型,提供了高效的操作大数据集所需的工具。
2.基本上能使用excel或者BI工具进行数据处理,Pandas也都能实现,而且更快
3.开源社区:https://github.com/pandas-dev/pandas
使用
1. 安装
pip install pandas
import pandas as pd
2.创建一个二维表格
# 创建一个二维表格, data = {"apples": [3, 2, 1, 0], "oranges": [0, 3, 7, 2]} print(data) print(type(data)) # 转换为pandas类型,生成为一个表格类型 data = pd.DataFrame(data) print(data) print(type(data)) # 获取指定列的数据 apples = data["apples"] print(apples) # 调整排序 apples=pd.Series(apples, index=[3, 2, 1, 0]) print(apples) #调整顺序并重置index,并且去掉index:drop=True apples=apples.reset_index(drop=True) print(apples){'apples': [3, 2, 1, 0], 'oranges': [0, 3, 7, 2]}
<class 'dict'>
apples oranges
0 3 0
1 2 3
2 1 7
3 0 2
<class 'pandas.core.frame.DataFrame'>
0 3
1 2
2 1
3 0
Name: apples, dtype: int64
3 0
2 1
1 2
0 3
Name: apples, dtype: int64
0 0
1 1
2 2
3 3
Name: apples, dtype: int64
3.numpy转换pandas
#Numpy转换Pandas data=np.arange(15).reshape(3,5) print(data) #columns 设置列标题 data=pd.DataFrame(data,columns=["a","b","c","d","e"]) print(data) #DataFrame转为数组 print(data.values)[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]]
a b c d e
0 0 1 2 3 4
1 5 6 7 8 9
2 10 11 12 13 14
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]]
4.DataFrame基本操作
1.模拟数据
#模拟数据,带时间搓的价格数据 dates=pd.date_range("2021-01-01",periods=10,freq="M") print(dates) #填充数据 data=pd.DataFrame(np.random.rand(10,3),columns=["苹果","小米","华为"],index=dates) print(data)DatetimeIndex(['2021-01-31', '2021-02-28', '2021-03-31', '2021-04-30',
'2021-05-31', '2021-06-30', '2021-07-31', '2021-08-31',
'2021-09-30', '2021-10-31'],
dtype='datetime64[ns]', freq='M')
苹果 小米 华为
2021-01-31 0.684936 0.874573 0.180163
2021-02-28 0.305274 0.537829 0.084554
2021-03-31 0.834719 0.412483 0.113443
2021-04-30 0.773563 0.233272 0.286549
2021-05-31 0.833274 0.885436 0.782416
2021-06-30 0.819943 0.168424 0.334666
2021-07-31 0.340181 0.922660 0.115425
2021-08-31 0.482005 0.450389 0.614912
2021-09-30 0.852704 0.949847 0.631340
2021-10-31 0.204136 0.968614 0.860673
2.查询头部,尾部数据
#查询头部数据,尾部数据 print("查询几行数据",data.head()) #查询前三行数据 print("查询前三行数据",data.head(3)) #查询尾部数据 print("查询尾部数据",data.tail()) print("查询尾部三行数据",data.tail(3))查询几行数据 苹果 小米 华为
2021-01-31 0.808681 0.821054 0.984782
2021-02-28 0.175775 0.256881 0.230408
2021-03-31 0.554073 0.452634 0.421805
2021-04-30 0.072842 0.585244 0.654349
2021-05-31 0.739625 0.279561 0.108246
查询前三行数据 苹果 小米 华为
2021-01-31 0.808681 0.821054 0.984782
2021-02-28 0.175775 0.256881 0.230408
2021-03-31 0.554073 0.452634 0.421805
查询尾部数据 苹果 小米 华为
2021-06-30 0.432621 0.572955 0.627820
2021-07-31 0.513483 0.808681 0.049465
2021-08-31 0.350001 0.564429 0.411039
2021-09-30 0.500061 0.665173 0.758615
2021-10-31 0.799431 0.201343 0.048331
查询尾部三行数据 苹果 小米 华为
2021-08-31 0.350001 0.564429 0.411039
2021-09-30 0.500061 0.665173 0.758615
2021-10-31 0.799431 0.201343 0.048331
3.查询索引、列名
#查询索引名 print("查询索引",data.index) print("查询列明",data.columns)查询索引 DatetimeIndex(['2021-01-31', '2021-02-28', '2021-03-31', '2021-04-30',
'2021-05-31', '2021-06-30', '2021-07-31', '2021-08-31',
'2021-09-30', '2021-10-31'],
dtype='datetime64[ns]', freq='M')
查询列明 Index(['苹果', '小米', '华为'], dtype='object')
4.查看值
#查看数值(array) print("查看值 values",data.values) print("查看值 to_numpy",data.to_numpy())查看值 values [[0.85333682 0.37036395 0.19676982]
[0.05484279 0.00405347 0.19988169]
[0.0765656 0.57548524 0.17430366]
[0.46426411 0.32989775 0.828621 ]
[0.1981231 0.92109303 0.35425536]
[0.88601156 0.69827724 0.3663627 ]
[0.93461127 0.09698946 0.35694662]
[0.64737528 0.7400368 0.90297379]
[0.64475335 0.34194282 0.9076167 ]
[0.55752804 0.35570815 0.80782257]]
查看值 to_numpy [[0.85333682 0.37036395 0.19676982]
[0.05484279 0.00405347 0.19988169]
[0.0765656 0.57548524 0.17430366]
[0.46426411 0.32989775 0.828621 ]
[0.1981231 0.92109303 0.35425536]
[0.88601156 0.69827724 0.3663627 ]
[0.93461127 0.09698946 0.35694662]
[0.64737528 0.7400368 0.90297379]
[0.64475335 0.34194282 0.9076167 ]
[0.55752804 0.35570815 0.80782257]]
5.查看统计摘要
print("查看统计摘要",data.describe())查看统计摘要 苹果 小米 华为
count 10.000000 10.000000 10.000000
mean 0.507089 0.376991 0.517665
std 0.264515 0.228177 0.301951
min 0.075162 0.044740 0.100207
25% 0.363494 0.262900 0.289732
50% 0.482666 0.303799 0.501494
75% 0.724689 0.431840 0.715131
max 0.851971 0.832133 0.943922
6.查询列数据
print("查询数据苹果:",data["苹果"]) print("查询数据苹果、小米:",data[["苹果","小米"]])查询数据苹果: 2021-01-31 0.672353
2021-02-28 0.861122
2021-03-31 0.829895
2021-04-30 0.277755
2021-05-31 0.821017
2021-06-30 0.895704
2021-07-31 0.484939
2021-08-31 0.522489
2021-09-30 0.169768
2021-10-31 0.208582
Freq: M, Name: 苹果, dtype: float64
查询数据苹果、小米: 苹果 小米
2021-01-31 0.672353 0.707993
2021-02-28 0.861122 0.697652
2021-03-31 0.829895 0.120987
2021-04-30 0.277755 0.096438
2021-05-31 0.821017 0.950389
2021-06-30 0.895704 0.104517
2021-07-31 0.484939 0.199773
2021-08-31 0.522489 0.449857
2021-09-30 0.169768 0.839512
2021-10-31 0.208582 0.056595
7.查询行数据
print("查询行数据0到5行:",data.iloc[0:5]) print("查询行数据,根据行索引和列索引范围:",data.loc["2021-01-01":"2021-05-01","小米":"华为"])查询行数据0到5行: 苹果 小米 华为
2021-01-31 0.672353 0.707993 0.140318
2021-02-28 0.861122 0.697652 0.627945
2021-03-31 0.829895 0.120987 0.546427
2021-04-30 0.277755 0.096438 0.256611
2021-05-31 0.821017 0.950389 0.001913
查询行数据,根据行索引和列索引范围: 小米 华为
2021-01-31 0.707993 0.140318
2021-02-28 0.697652 0.627945
2021-03-31 0.120987 0.546427
2021-04-30 0.096438 0.256611
8.值筛选
#按值筛选 #将值保留2位小数 data=round(data,2) print("按值筛选数据:",data[data['小米']<0.5])按值筛选数据: 苹果 小米 华为
2021-03-31 0.83 0.12 0.55
2021-04-30 0.28 0.10 0.26
2021-06-30 0.90 0.10 0.38
2021-07-31 0.48 0.20 0.00
2021-08-31 0.52 0.45 0.52
2021-10-31 0.21 0.06 0.81
9.转置行转列、排序
print("转置,行转列:",data.T) print("排序,根据值:",data.sort_values(by=["苹果"],ascending=False)) print("排序,根据索引:",data.sort_index(ascending=False))转置,行转列: 2021-01-31 2021-02-28 2021-03-31 ... 2021-08-31 2021-09-30 2021-10-31
苹果 0.67 0.86 0.83 ... 0.52 0.17 0.21
小米 0.71 0.70 0.12 ... 0.45 0.84 0.06
华为 0.14 0.63 0.55 ... 0.52 0.44 0.81[3 rows x 10 columns]
排序,根据值: 苹果 小米 华为
2021-06-30 0.90 0.10 0.38
2021-02-28 0.86 0.70 0.63
2021-03-31 0.83 0.12 0.55
2021-05-31 0.82 0.95 0.00
2021-01-31 0.67 0.71 0.14
2021-08-31 0.52 0.45 0.52
2021-07-31 0.48 0.20 0.00
2021-04-30 0.28 0.10 0.26
2021-10-31 0.21 0.06 0.81
2021-09-30 0.17 0.84 0.44
排序,根据索引: 苹果 小米 华为
2021-10-31 0.21 0.06 0.81
2021-09-30 0.17 0.84 0.44
2021-08-31 0.52 0.45 0.52
2021-07-31 0.48 0.20 0.00
2021-06-30 0.90 0.10 0.38
2021-05-31 0.82 0.95 0.00
2021-04-30 0.28 0.10 0.26
2021-03-31 0.83 0.12 0.55
2021-02-28 0.86 0.70 0.63
2021-01-31 0.67 0.71 0.14
5.时间序列和resample函数
1. index坐标为日期的数据
2.通过数据导入:pandas reader 进行数据模拟可以获取从线上获取一些统计数据,如雅虎基金数据等: 官网:pandas-datareader — pandas-datareader 0.10.0 documentation
pip install pandas-datareaderimport pandas_datareader as pdr
#获取美国5年期的债券
pdr.get_data_fred("GS10")
2019-04-01 2.53
2019-05-01 2.40
2019-06-01 2.07
2019-07-01 2.06
2019-08-01 1.63
2019-09-01 1.70
2019-10-01 1.71
2019-11-01 1.81
2019-12-01 1.86
2020-01-01 1.76
2020-02-01 1.50
2020-03-01 0.87
2020-04-01 0.66
2020-05-01 0.67
2020-06-01 0.73
2020-07-01 0.62
2020-08-01 0.65
2020-09-01 0.68
2020-10-01 0.79
2020-11-01 0.87
2020-12-01 0.93
2021-01-01 1.08
2021-02-01 1.26
2021-03-01 1.61
2021-04-01 1.64
2021-05-01 1.62
2021-06-01 1.52
2021-07-01 1.32
2021-08-01 1.28
2021-09-01 1.37
2021-10-01 1.58
2021-11-01 1.56
2021-12-01 1.47
2022-01-01 1.76
2022-02-01 1.93
2022-03-01 2.13
2022-04-01 2.75
2022-05-01 2.90
2022-06-01 3.14
2022-07-01 2.90
2022-08-01 2.90
2022-09-01 3.52
2022-10-01 3.98
2022-11-01 3.89
2022-12-01 3.62
2023-01-01 3.53
2023-02-01 3.75
2023-03-01 3.66
2023-04-01 3.46
2023-05-01 3.57
2023-06-01 3.75
2023-07-01 3.90
2023-08-01 4.17
2023-09-01 4.38
2023-10-01 4.80
3.Reample函数
只针对时间序列的计数、均值、方差啊、累加、累乘、周期转换、数据验证等功能
import pandas_datareader as pdr data=pdr.get_data_fred("GS10") print("data:",data) print("typedata:",type(data)) #计数、均值、方差啊、累加、累乘 #参数rule:可指定Y:年 M:月 D:天来进行统计 print("求每个年的均值",data.resample(rule="Y").mean()) print("求每个年的数量",data.resample(rule="Y").count()) print("求每个年的sum",data.resample(rule="Y").sum()) print("求每个年的标准差",data.resample(rule="Y").std()) print("求每个年的标准差的最大值",data.resample(rule="Y").std().max()) print("求每个年的累乘",data.resample(rule="Y").prod()) #数据合并 data2=pdr.get_data_fred("GS5") data["GS5"]=data2 print("GS5数据合并:",data) data["平均值"]=(data["GS10"]+data["GS5"])/2 print("列与列之间的计算,GS10和GS5的平均值",data) #周期转换 print("周期转换,为年",data.resample(rule="Y")) print("取一年中最后一个月数据",data.resample(rule="Y").last())data: GS10
DATE
2019-04-01 2.53
2019-05-01 2.40
2019-06-01 2.07
2019-07-01 2.06
2019-08-01 1.63
2019-09-01 1.70
2019-10-01 1.71
2019-11-01 1.81
2019-12-01 1.86
2020-01-01 1.76
2020-02-01 1.50
2020-03-01 0.87
2020-04-01 0.66
2020-05-01 0.67
2020-06-01 0.73
2020-07-01 0.62
2020-08-01 0.65
2020-09-01 0.68
2020-10-01 0.79
2020-11-01 0.87
2020-12-01 0.93
2021-01-01 1.08
2021-02-01 1.26
2021-03-01 1.61
2021-04-01 1.64
2021-05-01 1.62
2021-06-01 1.52
2021-07-01 1.32
2021-08-01 1.28
2021-09-01 1.37
2021-10-01 1.58
2021-11-01 1.56
2021-12-01 1.47
2022-01-01 1.76
2022-02-01 1.93
2022-03-01 2.13
2022-04-01 2.75
2022-05-01 2.90
2022-06-01 3.14
2022-07-01 2.90
2022-08-01 2.90
2022-09-01 3.52
2022-10-01 3.98
2022-11-01 3.89
2022-12-01 3.62
2023-01-01 3.53
2023-02-01 3.75
2023-03-01 3.66
2023-04-01 3.46
2023-05-01 3.57
2023-06-01 3.75
2023-07-01 3.90
2023-08-01 4.17
2023-09-01 4.38
2023-10-01 4.80
2023-11-01 4.50
2023-12-01 4.02
2024-01-01 4.06
2024-02-01 4.21
typedata: <class 'pandas.core.frame.DataFrame'>
求每个年的均值 GS10
DATE
2019-12-31 1.974444
2020-12-31 0.894167
2021-12-31 1.442500
2022-12-31 2.951667
2023-12-31 3.957500
2024-12-31 4.135000
求每个年的数量 GS10
DATE
2019-12-31 9
2020-12-31 12
2021-12-31 12
2022-12-31 12
2023-12-31 12
2024-12-31 2
求每个年的sum GS10
DATE
2019-12-31 17.77
2020-12-31 10.73
2021-12-31 17.31
2022-12-31 35.42
2023-12-31 47.49
2024-12-31 8.27
求每个年的标准差 GS10
DATE
2019-12-31 0.318242
2020-12-31 0.362152
2021-12-31 0.178230
2022-12-31 0.734276
2023-12-31 0.425337
2024-12-31 0.106066
求每个年的标准差的最大值 GS10 0.734276
dtype: float64
求每个年的累乘 GS10
DATE
2019-12-31 4.130413e+02
2020-12-31 1.298698e-01
2021-12-31 7.420379e+01
2022-12-31 3.006006e+05
2023-12-31 1.388089e+07
2024-12-31 1.709260e+01
GS5数据合并: GS10 GS5
DATE
2019-04-01 2.53 2.33
2019-05-01 2.40 2.19
2019-06-01 2.07 1.83
2019-07-01 2.06 1.83
2019-08-01 1.63 1.49
2019-09-01 1.70 1.57
2019-10-01 1.71 1.53
2019-11-01 1.81 1.64
2019-12-01 1.86 1.68
2020-01-01 1.76 1.56
2020-02-01 1.50 1.32
2020-03-01 0.87 0.59
2020-04-01 0.66 0.39
2020-05-01 0.67 0.34
2020-06-01 0.73 0.34
2020-07-01 0.62 0.28
2020-08-01 0.65 0.27
2020-09-01 0.68 0.27
2020-10-01 0.79 0.34
2020-11-01 0.87 0.39
2020-12-01 0.93 0.39
2021-01-01 1.08 0.45
2021-02-01 1.26 0.54
2021-03-01 1.61 0.82
2021-04-01 1.64 0.86
2021-05-01 1.62 0.82
2021-06-01 1.52 0.84
2021-07-01 1.32 0.76
2021-08-01 1.28 0.77
2021-09-01 1.37 0.86
2021-10-01 1.58 1.11
2021-11-01 1.56 1.20
2021-12-01 1.47 1.23
2022-01-01 1.76 1.54
2022-02-01 1.93 1.81
2022-03-01 2.13 2.11
2022-04-01 2.75 2.78
2022-05-01 2.90 2.87
2022-06-01 3.14 3.19
2022-07-01 2.90 2.96
2022-08-01 2.90 3.03
2022-09-01 3.52 3.70
2022-10-01 3.98 4.18
2022-11-01 3.89 4.06
2022-12-01 3.62 3.76
2023-01-01 3.53 3.64
2023-02-01 3.75 3.94
2023-03-01 3.66 3.82
2023-04-01 3.46 3.54
2023-05-01 3.57 3.59
2023-06-01 3.75 3.95
2023-07-01 3.90 4.14
2023-08-01 4.17 4.31
2023-09-01 4.38 4.49
2023-10-01 4.80 4.77
2023-11-01 4.50 4.49
2023-12-01 4.02 4.00
2024-01-01 4.06 3.98
2024-02-01 4.21 4.19
列与列之间的计算,GS10和GS5的平均值 GS10 GS5 平均值
DATE
2019-04-01 2.53 2.33 2.430
2019-05-01 2.40 2.19 2.295
2019-06-01 2.07 1.83 1.950
2019-07-01 2.06 1.83 1.945
2019-08-01 1.63 1.49 1.560
2019-09-01 1.70 1.57 1.635
2019-10-01 1.71 1.53 1.620
2019-11-01 1.81 1.64 1.725
2019-12-01 1.86 1.68 1.770
2020-01-01 1.76 1.56 1.660
2020-02-01 1.50 1.32 1.410
2020-03-01 0.87 0.59 0.730
2020-04-01 0.66 0.39 0.525
2020-05-01 0.67 0.34 0.505
2020-06-01 0.73 0.34 0.535
2020-07-01 0.62 0.28 0.450
2020-08-01 0.65 0.27 0.460
2020-09-01 0.68 0.27 0.475
2020-10-01 0.79 0.34 0.565
2020-11-01 0.87 0.39 0.630
2020-12-01 0.93 0.39 0.660
2021-01-01 1.08 0.45 0.765
2021-02-01 1.26 0.54 0.900
2021-03-01 1.61 0.82 1.215
2021-04-01 1.64 0.86 1.250
2021-05-01 1.62 0.82 1.220
2021-06-01 1.52 0.84 1.180
2021-07-01 1.32 0.76 1.040
2021-08-01 1.28 0.77 1.025
2021-09-01 1.37 0.86 1.115
2021-10-01 1.58 1.11 1.345
2021-11-01 1.56 1.20 1.380
2021-12-01 1.47 1.23 1.350
2022-01-01 1.76 1.54 1.650
2022-02-01 1.93 1.81 1.870
2022-03-01 2.13 2.11 2.120
2022-04-01 2.75 2.78 2.765
2022-05-01 2.90 2.87 2.885
2022-06-01 3.14 3.19 3.165
2022-07-01 2.90 2.96 2.930
2022-08-01 2.90 3.03 2.965
2022-09-01 3.52 3.70 3.610
2022-10-01 3.98 4.18 4.080
2022-11-01 3.89 4.06 3.975
2022-12-01 3.62 3.76 3.690
2023-01-01 3.53 3.64 3.585
2023-02-01 3.75 3.94 3.845
2023-03-01 3.66 3.82 3.740
2023-04-01 3.46 3.54 3.500
2023-05-01 3.57 3.59 3.580
2023-06-01 3.75 3.95 3.850
2023-07-01 3.90 4.14 4.020
2023-08-01 4.17 4.31 4.240
2023-09-01 4.38 4.49 4.435
2023-10-01 4.80 4.77 4.785
2023-11-01 4.50 4.49 4.495
2023-12-01 4.02 4.00 4.010
2024-01-01 4.06 3.98 4.020
2024-02-01 4.21 4.19 4.200
周期转换,为年 DatetimeIndexResampler [freq=<YearEnd: month=12>, axis=0, closed=right, label=right, convention=start, origin=start_day]
取一年中最后一个月数据 GS10 GS5 平均值
DATE
2019-12-31 1.86 1.68 1.77
2020-12-31 0.93 0.39 0.66
2021-12-31 1.47 1.23 1.35
2022-12-31 3.62 3.76 3.69
2023-12-31 4.02 4.00 4.01
2024-12-31 4.21 4.19 4.20
6.plot快速可視化
1.dataframe.plot功能:绘制图表
2. 安装:pip install matplotlib
import pandas as pd import numpy as np import matplotlib.pyplot as plt import pandas_datareader as pdr d2=pdr.get_data_fred("GS10") d3=pdr.get_data_fred("GS5") d2["GS5"]=d3 print("data:",d2) print("typedata:",type(d2)) #折线图 print(d2.plot()) #则方图 print(d2.hist()) plt.show()
7.io读写csv,excel
1.csv操作
import pandas as pd import pandas_datareader as pdr d1=pdr.get_data_fred("GS10") d2=pdr.get_data_fred("GS5") d1["GS5"]=d2 #存储csv,默认项目路径下 file_name="近5年数据.csv" #d1.to_csv(file_name) #读取csv, index_col:指定索引列 r1=pd.read_csv(file_name,index_col="DATE") print(r1) #对现有数据进行追加 mode:a 追加 默认w 覆盖 header:是否追加标题 默认True d1.to_csv(file_name,mode="a",header=False)
2.excel操作
安装依赖: pip install openpyxl
import pandas as pd import pandas_datareader as pdr d1=pdr.get_data_fred("GS10") d2=pdr.get_data_fred("GS5") d1["GS5"]=d2 #存储excel,默认项目路径下 file_name="近5年数据.xlsx" #sheet_name 指定sheet excel没有追加内容mode参数 #d1.to_excel(file_name,sheet_name="a") #同时写入多个sheet with pd.ExcelWriter(file_name) as w: d1.to_excel(w,sheet_name="GS10-GS5") d2.to_excel(w,sheet_name="GS5") #追加sheet with pd.ExcelWriter(file_name,mode="a",engine="openpyxl") as w: d2.to_excel(w,sheet_name="GS51")