您的位置:首页 > 编程语言 > Python开发

数据加载、存储与文件格式 利用Python进行数据分析 第6章

2017-05-04 18:03 856 查看

数据加载、存储与文件格式

源码下载链接

读写文本格式的数据

本文对Python中的数据加载、存储与文件格式做了一个简要的说明,实际应用中的情况更加复杂,每个小节的内容都很有限。如果用到相关内容,各位读者还需上网查找补充。

#read_csv和read_table都是从文件、URL、文本型对象中加载带分隔符的数据,
# read_csv默认分隔符为逗号,而read_table的默认分隔符为制表符(“\t”)
import pandas as pd
df=pd.read_csv('ch06/ex1.csv')#a,b,c,d,message是文件中的第一行
df


abcdmessage
01234hello
15678world
29101112foo
pd.read_table('ch06/ex1.csv',sep=',')


abcdmessage
01234hello
15678world
29101112foo
pd.read_csv('ch06/ex2.csv',header=None)#ex2.csv中没有标题行,pandas默认分配列名


01234
01234hello
15678world
29101112foo
pd.read_csv('ch06/ex2.csv',names=['a','b','c','d','message'])#自定义列名


abcdmessage
01234hello
15678world
29101112foo
names=['a','b','c','d','message']
pd.read_csv('ch06/ex2.csv',names=names,index_col='message')#将message做出DataFrame的索引


abcd
message
hello1234
world5678
foo9101112
#如果希望将多个列做成一个层次化索引,只需传入由列编号或列名组成的列表即可
parsed=pd.read_csv('ch06/csv_mindex.csv',index_col=['key1','key2'])
parsed


value1value2
key1key2
onea12
b34
c56
d78
twoa910
b1112
c1314
d1516
#有些表格不是用固定的分割符去分割字段的(比如空白符或其他)
#对于这种情况,可以编写一个正则表达式来作为read_table的分隔符
list(open('ch06/ex3.txt'))


[’ A B C\n’,
‘aaa -0.264438 -1.026059 -0.619500\n’,
‘bbb 0.927272 0.302904 -0.032399\n’,
‘ccc -0.264273 -0.386314 -0.217601\n’,
‘ddd -0.871858 -0.348382 1.100491\n’]

result=pd.read_table('ch06/ex3.txt',sep='\s+')#分隔符用正则表达式
result


ABC
aaa-0.264438-1.026059-0.619500
bbb0.9272720.302904-0.032399
ccc-0.264273-0.386314-0.217601
ddd-0.871858-0.3483821.100491
pd.read_csv('ch06/ex4.csv',skiprows=[0,2,3])#skiprows跳过文件的第一、三、四行


abcdmessage
01234hello
15678world
29101112foo
#pandas处理缺失值,会识别NA、-1、#IND以及NULL等
result=pd.read_csv('ch06/ex5.csv')
result


somethingabcdmessage
0one123.04NaN
1two56NaN8world
2three91011.012foo
pd.isnull(result)


somethingabcdmessage
0FalseFalseFalseFalseFalseTrue
1FalseFalseFalseTrueFalseFalse
2FalseFalseFalseFalseFalseFalse
#na_values可以接受一组用于表示缺失值的字符串
result=pd.read_csv('ch06/ex5.csv',na_values=['NULL'])
result


somethingabcdmessage
0one123.04NaN
1two56NaN8world
2three91011.012foo
#可以用一个字典为各列指定不同的NA标记值
sentinels={'message':['foo','NA'],'something':['two']}
pd.read_csv('ch06/ex5.csv',na_values=sentinels)


somethingabcdmessage
0one123.04NaN
1NaN56NaN8world
2three91011.012NaN

逐块读取文本文件

result=pd.read_csv('ch06/ex6.csv')
result


onetwothreefourkey
00.467976-0.038649-0.295344-1.824726L
1-0.3588931.4044530.704965-0.200638B
2-0.5018400.659254-0.421691-0.057688G
30.2048861.0741341.388361-0.982404R
40.354628-0.1331160.283763-0.837063Q
51.8174800.7422730.419395-2.251035Q
6-0.7767640.935518-0.332872-1.875641U
7-0.9131351.530624-0.5726570.477252K
80.358480-0.497572-0.3670160.507702S
9-1.740877-1.160417-1.6378302.172201G
100.240564-0.3282491.2521551.0727968
110.7640181.165476-0.6395441.495258R
120.571035-0.3105370.582437-0.2987651
132.3176580.430710-1.3342160.199679P
141.547771-1.119753-2.2776340.329586J
15-1.3106080.401719-1.0009871.156708E
16-0.0884960.6347120.1533240.415335B
17-0.018663-0.247487-1.4465220.750938A
18-0.070127-1.5790970.1208920.671432F
19-0.194678-0.4920392.3596050.319810H
20-0.2486180.868707-0.492226-0.717959W
21-1.091549-0.867110-0.647760-0.832562C
220.641404-0.138822-0.621963-0.284839C
231.2164080.9926870.165162-0.069619V
24-0.5644740.7928320.7470530.571675I
251.759879-0.515666-0.2304811.362317S
260.1262660.3092810.382820-0.239199L
271.334360-0.100152-0.840731-0.6439676
28-0.7376200.278087-0.053235-0.950972J
29-1.148486-0.986292-0.1449630.124362Y
99700.633495-0.1865240.9276270.1431644
99710.308636-0.1128570.762842-1.0729771
9972-1.627051-0.9781510.154745-1.229037Z
99730.3148470.0979890.1996080.955193P
99741.6669070.9920050.496128-0.686391S
99750.0106030.708540-1.2587110.226541K
99760.118693-0.714455-0.501342-0.254764K
99770.302616-2.011527-0.6280850.768827H
9978-0.0985721.769086-0.215027-0.053076A
9979-0.0190581.9649940.738538-0.883776F
9980-0.5953490.001781-1.423355-1.458477M
99811.392170-1.396560-1.425306-0.847535H
9982-0.896029-0.1522871.9244830.3651846
9983-2.274642-0.9018741.5003520.996541N
9984-0.3018981.0199061.1021602.624526I
9985-2.548389-0.5853741.496201-0.718815D
9986-0.0645880.759292-1.568415-0.420933E
9987-0.143365-1.111760-1.8155810.4352742
9988-0.070412-1.0559210.338017-0.440763X
99890.6491480.994273-1.3842270.485120Q
9990-0.3707690.404356-1.051628-1.0508998
9991-0.4099800.155627-0.8189901.277350W
99920.301214-1.1112030.6682580.671922A
99931.8211170.4164450.1738740.505118X
99940.0688041.3227590.8023460.223618H
99952.311896-0.417070-1.409599-0.515821L
9996-0.479893-0.6504190.745152-0.646038E
99970.5233310.7871120.4860661.093156K
9998-0.3625590.598894-1.8432010.887292G
9999-0.096376-1.012999-0.657431-0.5733150
10000 rows × 5 columns

#如果只想读取几行,通过nrows进行指定即可
pd.read_csv('ch06/ex6.csv',nrows=5)


onetwothreefourkey
00.467976-0.038649-0.295344-1.824726L
1-0.3588931.4044530.704965-0.200638B
2-0.5018400.659254-0.421691-0.057688G
30.2048861.0741341.388361-0.982404R
40.354628-0.1331160.283763-0.837063Q
#要逐块读取文件,需要设置chunksize(行数)
chunker=pd.read_csv('ch06/ex6.csv',chunksize=1000)
chunker


#read_csv返回的这个TextParser对象可以根据chunksize对文件进行逐块迭代
from pandas import Series,DataFrame
tot=Series([])
for piece in chunker:
tot=tot.add(piece['key'].value_counts(),fill_value=0)
tot=tot.sort_values(ascending=False)
tot[:10]


E 368.0
X 364.0
L 346.0
O 343.0
Q 340.0
M 338.0
J 337.0
F 335.0
K 334.0
H 330.0
dtype: float64

#TextParse还有一个get_chunk方法,它可以使你读取任意大小的块


将数据写入到文本格式

data=pd.read_csv('ch06/ex5.csv')
data


somethingabcdmessage
0one123.04NaN
1two56NaN8world
2three91011.012foo
data.to_csv('ch06/myout.csv')


data.to_csv('ch06/myout1.csv',sep='|')


data.to_csv('ch06/myout2.csv',na_rep='MULL')#空值替换为NULL输出


data.to_csv('ch06/myout3.csv',index=False,header=False)#不输出行和列标签


data.to_csv('ch06/myout4.csv',index=False,columns=['a','b','c'])#写出一部分的列,并以指定顺序排序


#Series也有一个to_csv方法
import numpy as np
dates=pd.date_range('1/1/2000',periods=7)
ts=Series(np.arange(7),index=dates)
ts.to_csv('ch06/tseries.csv')


Series.from_csv('ch06/tseries.csv',parse_dates=True)


2000-01-01 0
2000-01-02 1
2000-01-03 2
2000-01-04 3
2000-01-05 4
2000-01-06 5
2000-01-07 6
dtype: int64

##手工处理分隔符格式

#对于任何单字符分隔符文件,可以直接使用python内置的csv模块
import csv
f=open('ch06/ex7.csv')
reader=csv.reader(f)
#对这个reader进行迭代将会为每行产生一个列表,并移除了所有的引号
for line in reader:
print(line)


[‘a’, ‘b’, ‘c’]
[‘1’, ‘2’, ‘3’]
[‘1’, ‘2’, ‘3’, ‘4’]

#为了是数据格式合乎要求,需要对其做一些整理工作
lines=list(csv.reader(open('ch06/ex7.csv')))
header,values=lines[0],lines[1:]
data_dict={h:v for h,v in zip(header,zip(*values))}
data_dict


{‘a’: (‘1’, ‘1’), ‘b’: (‘2’, ‘2’), ‘c’: (‘3’, ‘3’)}

#可以使用csv.writer手工输出分隔符文件
with open('ch06/mydata.csv','w') as f:
writer=csv.writer(f)
writer.writerow(('one','two','three'))
writer.writerow(('1','2','3'))
writer.writerow(('4','5','6'))
writer.writerow(('7','8','9'))


JSON数据

obj="""
{"name":"Wes","places_lived":["United States","Spain","Germany"],
"pet":null,
"siblings":[{"name":"Scott","age":25,"pet":"Zuko"},
{"name":"Katie","age":33,"pet":"Cisco"}]}
"""
import json
result=json.loads(obj)
result


{‘name’: ‘Wes’,
‘pet’: None,
‘places_lived’: [‘United States’, ‘Spain’, ‘Germany’],
‘siblings’: [{‘age’: 25, ‘name’: ‘Scott’, ‘pet’: ‘Zuko’},
{‘age’: 33, ‘name’: ‘Katie’, ‘pet’: ‘Cisco’}]}

#json.dumps则将Python对象转换成JSON格式
asjson=json.dumps(result)


#将一个JSON对象转换为DataFrame
siblings=DataFrame(result['siblings'],columns=['name','age'])
siblings


nameage
0Scott25
1Katie33
siblings.to_json('ch06/json.csv')


XML和HTML:Web信息收集

利用lxml.objectify解析XML

from lxml import objectify
path='ch06/mta_perf/Performance_MNR.xml'
parsed=objectify.parse(open(path))
root=parsed.getroot()
data=[]
skip_fields=['PARENT_SEQ','INDICATOR_SEQ','DESIRED_CHANGE','DECIMAL_PLACES']
for elt in root.INDICATOR:
el_data={}
for child in elt.getchildren():
if child.tag in skip_fields:
continue
el_data[child.tag]=child.pyval
data.append(el_data)


perf=DataFrame(data)
perf


AGENCY_NAMECATEGORYDESCRIPTIONFREQUENCYINDICATOR_NAMEINDICATOR_UNITMONTHLY_ACTUALMONTHLY_TARGETPERIOD_MONTHPERIOD_YEARYTD_ACTUALYTD_TARGET
0Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei…MOn-Time Performance (West of Hudson)%96.9951200896.995
1Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei…MOn-Time Performance (West of Hudson)%9595220089695
2Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei…MOn-Time Performance (West of Hudson)%96.9953200896.395
3Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei…MOn-Time Performance (West of Hudson)%98.3954200896.895
4Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei…MOn-Time Performance (West of Hudson)%95.8955200896.695
5Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei…MOn-Time Performance (West of Hudson)%94.4956200896.295
6Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei…MOn-Time Performance (West of Hudson)%96957200896.295
7Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei…MOn-Time Performance (West of Hudson)%96.4958200896.295
8Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei…MOn-Time Performance (West of Hudson)%93.7959200895.995
9Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei…MOn-Time Performance (West of Hudson)%96.4951020089695
10Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei…MOn-Time Performance (West of Hudson)%96.99511200896.195
11Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei…MOn-Time Performance (West of Hudson)%95.1951220089695
12Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei…MOn-Time Performance (West of Hudson)%92.696.21200992.696.2
13Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei…MOn-Time Performance (West of Hudson)%96.896.22200994.696.2
14Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei…MOn-Time Performance (West of Hudson)%96.996.23200995.496.2
15Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei…MOn-Time Performance (West of Hudson)%97.196.24200995.996.2
16Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei…MOn-Time Performance (West of Hudson)%97.896.25200996.296.2
17Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei…MOn-Time Performance (West of Hudson)%97.396.26200996.496.2
18Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei…MOn-Time Performance (West of Hudson)%96.796.27200996.596.2
19Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei…MOn-Time Performance (West of Hudson)%95.796.28200996.496.2
20Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei…MOn-Time Performance (West of Hudson)%96.196.29200996.396.2
21Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei…MOn-Time Performance (West of Hudson)%94.896.210200996.296.2
22Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei…MOn-Time Performance (West of Hudson)%95.796.211200996.196.2
23Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei…MOn-Time Performance (West of Hudson)%9596.21220099696.2
24Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei…MOn-Time Performance (West of Hudson)%9896.3120109896.3
25Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei…MOn-Time Performance (West of Hudson)%9396.32201095.696.3
26Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei…MOn-Time Performance (West of Hudson)%96.996.33201096.196.3
27Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei…MOn-Time Performance (West of Hudson)%98.196.34201096.696.3
28Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei…MOn-Time Performance (West of Hudson)%97.696.35201096.896.3
29Metro-North RailroadService IndicatorsPercent of commuter trains that arrive at thei…MOn-Time Performance (West of Hudson)%97.496.36201096.996.3
618Metro-North RailroadService IndicatorsPercent of the time that escalators are operat…MEscalator Availability%947200995.14
619Metro-North RailroadService IndicatorsPercent of the time that escalators are operat…MEscalator Availability%978200995.38
620Metro-North RailroadService IndicatorsPercent of the time that escalators are operat…MEscalator Availability%98.39200995.7
621Metro-North RailroadService IndicatorsPercent of the time that escalators are operat…MEscalator Availability%98.710200996
622Metro-North RailroadService IndicatorsPercent of the time that escalators are operat…MEscalator Availability%98.111200996.21
623Metro-North RailroadService IndicatorsPercent of the time that escalators are operat…MEscalator Availability%10012200996.5
624Metro-North RailroadService IndicatorsPercent of the time that escalators are operat…MEscalator Availability%97.95971201097.9597
625Metro-North RailroadService IndicatorsPercent of the time that escalators are operat…MEscalator Availability%100972201098.9297
626Metro-North RailroadService IndicatorsPercent of the time that escalators are operat…MEscalator Availability%100973201099.2997
627Metro-North RailroadService IndicatorsPercent of the time that escalators are operat…MEscalator Availability%100974201099.4797
628Metro-North RailroadService IndicatorsPercent of the time that escalators are operat…MEscalator Availability%100975201099.5897
629Metro-North RailroadService IndicatorsPercent of the time that escalators are operat…MEscalator Availability%91.21976201098.1997
630Metro-North RailroadService IndicatorsPercent of the time that escalators are operat…MEscalator Availability%100977201098.4697
631Metro-North RailroadService IndicatorsPercent of the time that escalators are operat…MEscalator Availability%100978201098.6997
632Metro-North RailroadService IndicatorsPercent of the time that escalators are operat…MEscalator Availability%95.2979201098.397
633Metro-North RailroadService IndicatorsPercent of the time that escalators are operat…MEscalator Availability%90.919710201097.5597
634Metro-North RailroadService IndicatorsPercent of the time that escalators are operat…MEscalator Availability%96.679711201097.4797
635Metro-North RailroadService IndicatorsPercent of the time that escalators are operat…MEscalator Availability%90.039712201096.8497
636Metro-North RailroadService IndicatorsPercent of the time that escalators are operat…MEscalator Availability%100971201110097
637Metro-North RailroadService IndicatorsPercent of the time that escalators are operat…MEscalator Availability%100972201110097
638Metro-North RailroadService IndicatorsPercent of the time that escalators are operat…MEscalator Availability%97.07973201198.8697
639Metro-North RailroadService IndicatorsPercent of the time that escalators are operat…MEscalator Availability%98.18974201198.7697
640Metro-North RailroadService IndicatorsPercent of the time that escalators are operat…MEscalator Availability%79.18975201190.9197
641Metro-North RailroadService IndicatorsPercent of the time that escalators are operat…MEscalator Availability%976201197
642Metro-North RailroadService IndicatorsPercent of the time that escalators are operat…MEscalator Availability%977201197
643Metro-North RailroadService IndicatorsPercent of the time that escalators are operat…MEscalator Availability%978201197
644Metro-North RailroadService IndicatorsPercent of the time that escalators are operat…MEscalator Availability%979201197
645Metro-North RailroadService IndicatorsPercent of the time that escalators are operat…MEscalator Availability%9710201197
646Metro-North RailroadService IndicatorsPercent of the time that escalators are operat…MEscalator Availability%9711201197
647Metro-North RailroadService IndicatorsPercent of the time that escalators are operat…MEscalator Availability%9712201197
648 rows × 12 columns

# from StringIO import StringIO 已过时在Python3中
from io import StringIO
tag='<a href="http://www.google.com">Google</a>'
root=objectify.parse(StringIO(tag)).getroot()
root


root.get('href')


‘http://www.google.com’

root.text


‘Google’

二进制数据格式

#实现数据的二进制格式存储最简单的办法之一就是使用Python内置的pickle序列化。
#pandas对象都有一个用于将数据以pickle形式保存到磁盘上的save方法
frame=pd.read_csv('ch06/ex1.csv')
frame


abcdmessage
01234hello
15678world
29101112foo
frame.to_pickle('ch06/frame_pickle')#写入文件  书上的sava方法没了


pd.read_pickle('ch06/frame_pickle')#读取二进制  书上的load方法没有了


abcdmessage
01234hello
15678world
29101112foo

使用HDF5格式

#pandas有一个最小化的类似于字典的HDFStore类,它通过PyTables存储pandas对象
#由于没有安装PyTables,所以下面代码运行有错误
store=pd.HDFStore('ch06/mydata.h5')
store['obj1']=frame
store['obj1_col']=frame['a']
store
store['obj1']


读取Microsoft Excel文件

#pandas的ExcelFile类支持读取xls和xlsx文件,得先安装xlrd和openpyxl包
xls_file=pd.ExcelFile('ch06/book.xlsx')


table=xls_file.parse('Sheet1')


table


书名索引号作者或出版社
0推荐系统ISBN:9787115310699[[奥地利] Gerhard Friedrich 等 著;蒋凡 译
1推荐系统实践国际标准书号ISBN:9787115281589项亮
2SuperMap iClient for Flex从入门到精通国际标准书号ISBN:9787302335931SuperMap图书编委会
3HTML5与WebGL编程ISBN:9787115421333[美] Tony Parisi
4HTML5实战ISBN:9787115378835[英]罗伯·克洛泽(Rob Crowther)
5Python核心编程(第3版)ISBN:9787115414779美] Wesley Chun 著;孙波翔,李斌,李晗 译
6利用Python进行数据分析ISBN:9787111436737Wes McKinney 著;唐学韬 等 译
7Python网络数据采集ISBN:9787115416292[美] 米切尔(Ryan Mitchell) 著;陶俊杰,陈小莉
8Java编程思想(第4版)/计算机科学丛书ISBN:9787111213826[美] 埃克尔,译者:陈昊鹏 编
9unix网络编程(卷一)国际标准书号ISBN:9787115367198作者:[美]史蒂文斯 注意,匿名 译
10unix网络编程(卷二)国际标准书号ISBN:9787115367204作者:[美]史蒂文斯 注意,匿名 译
11Python金融大数据分析国际标准书号ISBN:9787115404459出版社:人民邮电出版社
12ZigBee无线传感器网络设计与实现国际标准书号ISBN:9787122137463作者:王小强,欧阳骏,黄宁淋 编著
13和秋叶一起学WordISBN:9787115400239出版社: 人民邮电出版社
14和秋叶一起学PPT 又快又好打造说服力幻灯片(第2版)ISBN:9787115349446出版社: 人民邮电出版社
15谁说菜鸟不会数据分析(5周年特别套装共3册)ISBN:11920136出版社: 电子工业出版社
table['书名']


0 推荐系统
1 推荐系统实践
2 SuperMap iClient for Flex从入门到精通
3 HTML5与WebGL编程
4 HTML5实战
5 Python核心编程(第3版)
6 利用Python进行数据分析
7 Python网络数据采集
8 Java编程思想(第4版)/计算机科学丛书
9 unix网络编程(卷一)
10 unix网络编程(卷二)
11 Python金融大数据分析
12 ZigBee无线传感器网络设计与实现
13 和秋叶一起学Word
14 和秋叶一起学PPT 又快又好打造说服力幻灯片(第2版)
15 谁说菜鸟不会数据分析(5周年特别套装共3册)
Name: 书名, dtype: object

使用HTML和WebAPI

import requests
# url='http://search.twitter.com/search.json?q=python%20pandas'#国内访问不了,大家可以找其他的练习
url='http://api.map.baidu.com/telematics/v3/weather?location=海口&output=json&ak=5slgyqGDENN7Sy7pw29IUvrZ'
resp=requests.get(url)
resp


import json
data=json.loads(resp.text)
data.keys()


dict_keys([‘status’, ‘message’])

data['message']


‘APP被用户自己禁用,请在控制台解禁’

使用数据库

#这里以MySQL数据库为例
# http://www.cnblogs.com/W-Kr/p/5456810.html import pymysql.cursors
config = {
'host':'127.0.0.1',
'port':3306,
'user':'root',
'password':'713zjl',
'db':'wuxing',
'charset':'utf8mb4',
'cursorclass':pymysql.cursors.DictCursor,
}

# Connect to the database
connection = pymysql.connect(**config)

#查询
# 执行sql语句
try:
with connection.cursor() as cursor:
# 执行sql语句,进行查询
sql = 'SELECT * from service'
cursor.execute(sql)
# 获取查询结果
# result = cursor.fetchone()
# result = cursor.fetchmany(5)
result = cursor.fetchall()
data=DataFrame(result)
print(result)
# 没有设置默认自动提交,需要主动提交,以保存所执行的语句
connection.commit()

finally:
connection.close()
data


[{‘cost’: 1.0, ‘alility’: 1.0, ‘id’: 1, ‘result’: 1.714, ‘response_time’: 286.0}, {‘cost’: 0.5, ‘alility’: 0.8, ‘id’: 2, ‘result’: 2.053, ‘response_time’: 247.0}, {‘cost’: 0.5, ‘alility’: 0.9, ‘id’: 3, ‘result’: 2.146, ‘response_time’: 254.0}, {‘cost’: 0.5, ‘alility’: 0.8, ‘id’: 4, ‘result’: 2.039, ‘response_time’: 261.0}, {‘cost’: 0.7, ‘alility’: 0.9, ‘id’: 5, ‘result’: 1.869, ‘response_time’: 331.0}, {‘cost’: 0.6, ‘alility’: 0.6, ‘id’: 6, ‘result’: 1.797, ‘response_time’: 203.0}, {‘cost’: 0.7, ‘alility’: 0.9, ‘id’: 7, ‘result’: 1.998, ‘response_time’: 202.0}, {‘cost’: 0.5, ‘alility’: 0.9, ‘id’: 8, ‘result’: 2.2, ‘response_time’: 200.0}, {‘cost’: 0.8, ‘alility’: 0.6, ‘id’: 9, ‘result’: 1.621, ‘response_time’: 179.0}, {‘cost’: 0.6, ‘alility’: 0.8, ‘id’: 10, ‘result’: 2.029, ‘response_time’: 171.0}, {‘cost’: 0.6, ‘alility’: 0.6, ‘id’: 11, ‘result’: 1.683, ‘response_time’: 317.0}, {‘cost’: 0.7, ‘alility’: 0.7, ‘id’: 12, ‘result’: 1.772, ‘response_time’: 228.0}, {‘cost’: 0.6, ‘alility’: 0.7, ‘id’: 13, ‘result’: 1.857, ‘response_time’: 243.0}, {‘cost’: 0.9, ‘alility’: 0.6, ‘id’: 14, ‘result’: 1.402, ‘response_time’: 298.0}, {‘cost’: 0.7, ‘alility’: 1.0, ‘id’: 15, ‘result’: 1.97, ‘response_time’: 330.0}, {‘cost’: 0.6, ‘alility’: 0.5, ‘id’: 16, ‘result’: 1.68, ‘response_time’: 220.0}, {‘cost’: 0.8, ‘alility’: 1.0, ‘id’: 17, ‘result’: 1.937, ‘response_time’: 263.0}, {‘cost’: 0.7, ‘alility’: 1.0, ‘id’: 18, ‘result’: 2.081, ‘response_time’: 219.0}, {‘cost’: 0.6, ‘alility’: 0.7, ‘id’: 19, ‘result’: 1.899, ‘response_time’: 201.0}, {‘cost’: 0.7, ‘alility’: 0.6, ‘id’: 20, ‘result’: 1.693, ‘response_time’: 207.0}, {‘cost’: 0.9, ‘alility’: 1.0, ‘id’: 21, ‘result’: 1.798, ‘response_time’: 302.0}, {‘cost’: 0.6, ‘alility’: 0.9, ‘id’: 22, ‘result’: 2.042, ‘response_time’: 258.0}, {‘cost’: 0.5, ‘alility’: 0.7, ‘id’: 23, ‘result’: 1.92, ‘response_time’: 280.0}, {‘cost’: 0.6, ‘alility’: 0.5, ‘id’: 24, ‘result’: 1.594, ‘response_time’: 306.0}, {‘cost’: 1.0, ‘alility’: 1.0, ‘id’: 25, ‘result’: 1.647, ‘response_time’: 353.0}, {‘cost’: 1.0, ‘alility’: 0.7, ‘id’: 26, ‘result’: 1.489, ‘response_time’: 211.0}, {‘cost’: 1.0, ‘alility’: 0.8, ‘id’: 27, ‘result’: 1.562, ‘response_time’: 238.0}, {‘cost’: 0.6, ‘alility’: 0.5, ‘id’: 28, ‘result’: 1.693, ‘response_time’: 207.0}, {‘cost’: 0.9, ‘alility’: 1.0, ‘id’: 29, ‘result’: 1.904, ‘response_time’: 196.0}, {‘cost’: 0.9, ‘alility’: 0.7, ‘id’: 30, ‘result’: 1.603, ‘response_time’: 197.0}, {‘cost’: 0.6, ‘alility’: 0.9, ‘id’: 31, ‘result’: 1.876, ‘response_time’: 424.0}, {‘cost’: 0.6, ‘alility’: 0.8, ‘id’: 32, ‘result’: 1.711, ‘response_time’: 489.0}, {‘cost’: 0.9, ‘alility’: 0.9, ‘id’: 33, ‘result’: 1.601, ‘response_time’: 399.0}, {‘cost’: 0.5, ‘alility’: 0.9, ‘id’: 34, ‘result’: 2.032, ‘response_time’: 368.0}, {‘cost’: 1.0, ‘alility’: 0.8, ‘id’: 35, ‘result’: 1.497, ‘response_time’: 303.0}, {‘cost’: 0.8, ‘alility’: 0.9, ‘id’: 36, ‘result’: 1.791, ‘response_time’: 309.0}, {‘cost’: 0.5, ‘alility’: 0.5, ‘id’: 37, ‘result’: 1.57, ‘response_time’: 430.0}, {‘cost’: 0.5, ‘alility’: 1.0, ‘id’: 38, ‘result’: 2.188, ‘response_time’: 312.0}, {‘cost’: 0.8, ‘alility’: 0.6, ‘id’: 39, ‘result’: 1.513, ‘response_time’: 287.0}, {‘cost’: 0.6, ‘alility’: 0.9, ‘id’: 40, ‘result’: 2.077, ‘response_time’: 223.0}, {‘cost’: 0.9, ‘alility’: 0.6, ‘id’: 41, ‘result’: 1.423, ‘response_time’: 277.0}, {‘cost’: 0.5, ‘alility’: 0.9, ‘id’: 42, ‘result’: 2.153, ‘response_time’: 247.0}, {‘cost’: 1.0, ‘alility’: 1.0, ‘id’: 43, ‘result’: 1.647, ‘response_time’: 353.0}, {‘cost’: 1.0, ‘alility’: 0.8, ‘id’: 44, ‘result’: 1.571, ‘response_time’: 229.0}, {‘cost’: 1.0, ‘alility’: 0.9, ‘id’: 45, ‘result’: 1.653, ‘response_time’: 247.0}, {‘cost’: 1.0, ‘alility’: 1.0, ‘id’: 46, ‘result’: 1.798, ‘response_time’: 202.0}, {‘cost’: 0.8, ‘alility’: 0.7, ‘id’: 47, ‘result’: 1.707, ‘response_time’: 193.0}, {‘cost’: 1.0, ‘alility’: 0.8, ‘id’: 48, ‘result’: 1.607, ‘response_time’: 193.0}, {‘cost’: 0.8, ‘alility’: 0.7, ‘id’: 49, ‘result’: 1.573, ‘response_time’: 327.0}, {‘cost’: 0.9, ‘alility’: 1.0, ‘id’: 50, ‘result’: 1.876, ‘response_time’: 224.0}, {‘cost’: 0.8, ‘alility’: 0.5, ‘id’: 51, ‘result’: 1.483, ‘response_time’: 217.0}, {‘cost’: 0.8, ‘alility’: 0.9, ‘id’: 52, ‘result’: 1.901, ‘response_time’: 199.0}, {‘cost’: 0.8, ‘alility’: 0.7, ‘id’: 53, ‘result’: 1.668, ‘response_time’: 232.0}, {‘cost’: 0.9, ‘alility’: 0.6, ‘id’: 54, ‘result’: 1.498, ‘response_time’: 202.0}, {‘cost’: 0.5, ‘alility’: 1.0, ‘id’: 55, ‘result’: 2.31, ‘response_time’: 190.0}, {‘cost’: 0.7, ‘alility’: 0.6, ‘id’: 56, ‘result’: 1.726, ‘response_time’: 174.0}, {‘cost’: 0.5, ‘alility’: 0.5, ‘id’: 57, ‘result’: 1.778, ‘response_time’: 222.0}, {‘cost’: 0.7, ‘alility’: 0.6, ‘id’: 58, ‘result’: 1.717, ‘response_time’: 183.0}, {‘cost’: 0.8, ‘alility’: 0.5, ‘id’: 59, ‘result’: 1.352, ‘response_time’: 348.0}, {‘cost’: 0.6, ‘alility’: 0.9, ‘id’: 60, ‘result’: 2.073, ‘response_time’: 227.0}, {‘cost’: 0.8, ‘alility’: 0.7, ‘id’: 61, ‘result’: 1.673, ‘response_time’: 227.0}, {‘cost’: 0.9, ‘alility’: 1.0, ‘id’: 62, ‘result’: 1.881, ‘response_time’: 219.0}, {‘cost’: 0.7, ‘alility’: 1.0, ‘id’: 63, ‘result’: 2.004, ‘response_time’: 296.0}, {‘cost’: 0.9, ‘alility’: 0.9, ‘id’: 64, ‘result’: 1.801, ‘response_time’: 199.0}, {‘cost’: 0.8, ‘alility’: 0.6, ‘id’: 65, ‘result’: 1.603, ‘response_time’: 197.0}, {‘cost’: 0.7, ‘alility’: 0.5, ‘id’: 66, ‘result’: 1.617, ‘response_time’: 183.0}, {‘cost’: 0.9, ‘alility’: 1.0, ‘id’: 67, ‘result’: 1.907, ‘response_time’: 193.0}, {‘cost’: 0.5, ‘alility’: 0.5, ‘id’: 68, ‘result’: 1.802, ‘response_time’: 198.0}, {‘cost’: 0.6, ‘alility’: 0.7, ‘id’: 69, ‘result’: 1.772, ‘response_time’: 328.0}, {‘cost’: 0.8, ‘alility’: 0.7, ‘id’: 70, ‘result’: 1.673, ‘response_time’: 227.0}, {‘cost’: 0.9, ‘alility’: 0.7, ‘id’: 71, ‘result’: 1.572, ‘response_time’: 228.0}, {‘cost’: 0.7, ‘alility’: 1.0, ‘id’: 72, ‘result’: 2.117, ‘response_time’: 183.0}, {‘cost’: 0.8, ‘alility’: 0.9, ‘id’: 73, ‘result’: 1.869, ‘response_time’: 231.0}, {‘cost’: 0.6, ‘alility’: 0.6, ‘id’: 74, ‘result’: 1.835, ‘response_time’: 165.0}, {‘cost’: 0.8, ‘alility’: 0.7, ‘id’: 75, ‘result’: 1.714, ‘response_time’: 186.0}, {‘cost’: 0.6, ‘alility’: 0.5, ‘id’: 76, ‘result’: 1.709, ‘response_time’: 191.0}, {‘cost’: 0.9, ‘alility’: 0.7, ‘id’: 77, ‘result’: 1.583, ‘response_time’: 217.0}, {‘cost’: 1.0, ‘alility’: 0.9, ‘id’: 78, ‘result’: 1.717, ‘response_time’: 183.0}]

alilitycostidresponse_timeresult
01.01.01286.01.714
10.80.52247.02.053
20.90.53254.02.146
30.80.54261.02.039
40.90.75331.01.869
50.60.66203.01.797
60.90.77202.01.998
70.90.58200.02.200
80.60.89179.01.621
90.80.610171.02.029
100.60.611317.01.683
110.70.712228.01.772
120.70.613243.01.857
130.60.914298.01.402
141.00.715330.01.970
150.50.616220.01.680
161.00.817263.01.937
171.00.718219.02.081
180.70.619201.01.899
190.60.720207.01.693
201.00.921302.01.798
210.90.622258.02.042
220.70.523280.01.920
230.50.624306.01.594
241.01.025353.01.647
250.71.026211.01.489
260.81.027238.01.562
270.50.628207.01.693
281.00.929196.01.904
290.70.930197.01.603
480.70.849327.01.573
491.00.950224.01.876
500.50.851217.01.483
510.90.852199.01.901
520.70.853232.01.668
530.60.954202.01.498
541.00.555190.02.310
550.60.756174.01.726
560.50.557222.01.778
570.60.758183.01.717
580.50.859348.01.352
590.90.660227.02.073
600.70.861227.01.673
611.00.962219.01.881
621.00.763296.02.004
630.90.964199.01.801
640.60.865197.01.603
650.50.766183.01.617
661.00.967193.01.907
670.50.568198.01.802
680.70.669328.01.772
690.70.870227.01.673
700.70.971228.01.572
711.00.772183.02.117
720.90.873231.01.869
730.60.674165.01.835
740.70.875186.01.714
750.50.676191.01.709
760.70.977217.01.583
770.91.078183.01.717
78 rows × 5 columns


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐