from __future__ import division
from numpy.random import randn
import numpy as np
import os
import sys
import matplotlib.pyplot as plt
np.random.seed(12345)
plt.rc('figure', figsize=(10, 6))
from pandas import Series, DataFrame
import pandas as pd
np.set_printoptions(precision=4)
chunker = pd.read_csv('data6.csv', chunksize=100)
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 ### 1.3 把数据写入文本格式
lines = list(csv.reader(open('data7.csv')))
header, values = lines[0], lines[1:]
print(values)
data_dict = {h: v for h, v in zip(header, zip(*values))}
print(data_dict)
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)
from pandas import DataFrame
perf = DataFrame(data)
perf
AGENCY_NAME
CATEGORY
DESCRIPTION
FREQUENCY
INDICATOR_NAME
INDICATOR_UNIT
MONTHLY_ACTUAL
MONTHLY_TARGET
PERIOD_MONTH
PERIOD_YEAR
YTD_ACTUAL
YTD_TARGET
0
Metro-North Railroad
Service Indicators
Percent of commuter trains that arrive at thei…
M
On-Time Performance (West of Hudson)
%
96.9
95
1
2008
96.9
95
1
Metro-North Railroad
Service Indicators
Percent of commuter trains that arrive at thei…
M
On-Time Performance (West of Hudson)
%
95
95
2
2008
96
95
2
Metro-North Railroad
Service Indicators
Percent of commuter trains that arrive at thei…
M
On-Time Performance (West of Hudson)
%
96.9
95
3
2008
96.3
95
3
Metro-North Railroad
Service Indicators
Percent of commuter trains that arrive at thei…
M
On-Time Performance (West of Hudson)
%
98.3
95
4
2008
96.8
95
4
Metro-North Railroad
Service Indicators
Percent of commuter trains that arrive at thei…
M
On-Time Performance (West of Hudson)
%
95.8
95
5
2008
96.6
95
5
Metro-North Railroad
Service Indicators
Percent of commuter trains that arrive at thei…
M
On-Time Performance (West of Hudson)
%
94.4
95
6
2008
96.2
95
6
Metro-North Railroad
Service Indicators
Percent of commuter trains that arrive at thei…
M
On-Time Performance (West of Hudson)
%
96
95
7
2008
96.2
95
7
Metro-North Railroad
Service Indicators
Percent of commuter trains that arrive at thei…
M
On-Time Performance (West of Hudson)
%
96.4
95
8
2008
96.2
95
8
Metro-North Railroad
Service Indicators
Percent of commuter trains that arrive at thei…
M
On-Time Performance (West of Hudson)
%
93.7
95
9
2008
95.9
95
9
Metro-North Railroad
Service Indicators
Percent of commuter trains that arrive at thei…
M
On-Time Performance (West of Hudson)
%
96.4
95
10
2008
96
95
10
Metro-North Railroad
Service Indicators
Percent of commuter trains that arrive at thei…
M
On-Time Performance (West of Hudson)
%
96.9
95
11
2008
96.1
95
11
Metro-North Railroad
Service Indicators
Percent of commuter trains that arrive at thei…
M
On-Time Performance (West of Hudson)
%
95.1
95
12
2008
96
95
12
Metro-North Railroad
Service Indicators
Percent of commuter trains that arrive at thei…
M
On-Time Performance (West of Hudson)
%
92.6
96.2
1
2009
92.6
96.2
13
Metro-North Railroad
Service Indicators
Percent of commuter trains that arrive at thei…
M
On-Time Performance (West of Hudson)
%
96.8
96.2
2
2009
94.6
96.2
14
Metro-North Railroad
Service Indicators
Percent of commuter trains that arrive at thei…
M
On-Time Performance (West of Hudson)
%
96.9
96.2
3
2009
95.4
96.2
15
Metro-North Railroad
Service Indicators
Percent of commuter trains that arrive at thei…
M
On-Time Performance (West of Hudson)
%
97.1
96.2
4
2009
95.9
96.2
16
Metro-North Railroad
Service Indicators
Percent of commuter trains that arrive at thei…
M
On-Time Performance (West of Hudson)
%
97.8
96.2
5
2009
96.2
96.2
17
Metro-North Railroad
Service Indicators
Percent of commuter trains that arrive at thei…
M
On-Time Performance (West of Hudson)
%
97.3
96.2
6
2009
96.4
96.2
18
Metro-North Railroad
Service Indicators
Percent of commuter trains that arrive at thei…
M
On-Time Performance (West of Hudson)
%
96.7
96.2
7
2009
96.5
96.2
19
Metro-North Railroad
Service Indicators
Percent of commuter trains that arrive at thei…
M
On-Time Performance (West of Hudson)
%
95.7
96.2
8
2009
96.4
96.2
20
Metro-North Railroad
Service Indicators
Percent of commuter trains that arrive at thei…
M
On-Time Performance (West of Hudson)
%
96.1
96.2
9
2009
96.3
96.2
21
Metro-North Railroad
Service Indicators
Percent of commuter trains that arrive at thei…
M
On-Time Performance (West of Hudson)
%
94.8
96.2
10
2009
96.2
96.2
22
Metro-North Railroad
Service Indicators
Percent of commuter trains that arrive at thei…
M
On-Time Performance (West of Hudson)
%
95.7
96.2
11
2009
96.1
96.2
23
Metro-North Railroad
Service Indicators
Percent of commuter trains that arrive at thei…
M
On-Time Performance (West of Hudson)
%
95
96.2
12
2009
96
96.2
24
Metro-North Railroad
Service Indicators
Percent of commuter trains that arrive at thei…
M
On-Time Performance (West of Hudson)
%
98
96.3
1
2010
98
96.3
25
Metro-North Railroad
Service Indicators
Percent of commuter trains that arrive at thei…
M
On-Time Performance (West of Hudson)
%
93
96.3
2
2010
95.6
96.3
26
Metro-North Railroad
Service Indicators
Percent of commuter trains that arrive at thei…
M
On-Time Performance (West of Hudson)
%
96.9
96.3
3
2010
96.1
96.3
27
Metro-North Railroad
Service Indicators
Percent of commuter trains that arrive at thei…
M
On-Time Performance (West of Hudson)
%
98.1
96.3
4
2010
96.6
96.3
28
Metro-North Railroad
Service Indicators
Percent of commuter trains that arrive at thei…
M
On-Time Performance (West of Hudson)
%
97.6
96.3
5
2010
96.8
96.3
29
Metro-North Railroad
Service Indicators
Percent of commuter trains that arrive at thei…
M
On-Time Performance (West of Hudson)
%
97.4
96.3
6
2010
96.9
96.3
…
…
…
…
…
…
…
…
…
…
…
…
…
618
Metro-North Railroad
Service Indicators
Percent of the time that escalators are operat…
M
Escalator Availability
%
94
7
2009
95.14
619
Metro-North Railroad
Service Indicators
Percent of the time that escalators are operat…
M
Escalator Availability
%
97
8
2009
95.38
620
Metro-North Railroad
Service Indicators
Percent of the time that escalators are operat…
M
Escalator Availability
%
98.3
9
2009
95.7
621
Metro-North Railroad
Service Indicators
Percent of the time that escalators are operat…
M
Escalator Availability
%
98.7
10
2009
96
622
Metro-North Railroad
Service Indicators
Percent of the time that escalators are operat…
M
Escalator Availability
%
98.1
11
2009
96.21
623
Metro-North Railroad
Service Indicators
Percent of the time that escalators are operat…
M
Escalator Availability
%
100
12
2009
96.5
624
Metro-North Railroad
Service Indicators
Percent of the time that escalators are operat…
M
Escalator Availability
%
97.95
97
1
2010
97.95
97
625
Metro-North Railroad
Service Indicators
Percent of the time that escalators are operat…
M
Escalator Availability
%
100
97
2
2010
98.92
97
626
Metro-North Railroad
Service Indicators
Percent of the time that escalators are operat…
M
Escalator Availability
%
100
97
3
2010
99.29
97
627
Metro-North Railroad
Service Indicators
Percent of the time that escalators are operat…
M
Escalator Availability
%
100
97
4
2010
99.47
97
628
Metro-North Railroad
Service Indicators
Percent of the time that escalators are operat…
M
Escalator Availability
%
100
97
5
2010
99.58
97
629
Metro-North Railroad
Service Indicators
Percent of the time that escalators are operat…
M
Escalator Availability
%
91.21
97
6
2010
98.19
97
630
Metro-North Railroad
Service Indicators
Percent of the time that escalators are operat…
M
Escalator Availability
%
100
97
7
2010
98.46
97
631
Metro-North Railroad
Service Indicators
Percent of the time that escalators are operat…
M
Escalator Availability
%
100
97
8
2010
98.69
97
632
Metro-North Railroad
Service Indicators
Percent of the time that escalators are operat…
M
Escalator Availability
%
95.2
97
9
2010
98.3
97
633
Metro-North Railroad
Service Indicators
Percent of the time that escalators are operat…
M
Escalator Availability
%
90.91
97
10
2010
97.55
97
634
Metro-North Railroad
Service Indicators
Percent of the time that escalators are operat…
M
Escalator Availability
%
96.67
97
11
2010
97.47
97
635
Metro-North Railroad
Service Indicators
Percent of the time that escalators are operat…
M
Escalator Availability
%
90.03
97
12
2010
96.84
97
636
Metro-North Railroad
Service Indicators
Percent of the time that escalators are operat…
M
Escalator Availability
%
100
97
1
2011
100
97
637
Metro-North Railroad
Service Indicators
Percent of the time that escalators are operat…
M
Escalator Availability
%
100
97
2
2011
100
97
638
Metro-North Railroad
Service Indicators
Percent of the time that escalators are operat…
M
Escalator Availability
%
97.07
97
3
2011
98.86
97
639
Metro-North Railroad
Service Indicators
Percent of the time that escalators are operat…
M
Escalator Availability
%
98.18
97
4
2011
98.76
97
640
Metro-North Railroad
Service Indicators
Percent of the time that escalators are operat…
M
Escalator Availability
%
79.18
97
5
2011
90.91
97
641
Metro-North Railroad
Service Indicators
Percent of the time that escalators are operat…
M
Escalator Availability
%
97
6
2011
97
642
Metro-North Railroad
Service Indicators
Percent of the time that escalators are operat…
M
Escalator Availability
%
97
7
2011
97
643
Metro-North Railroad
Service Indicators
Percent of the time that escalators are operat…
M
Escalator Availability
%
97
8
2011
97
644
Metro-North Railroad
Service Indicators
Percent of the time that escalators are operat…
M
Escalator Availability
%
97
9
2011
97
645
Metro-North Railroad
Service Indicators
Percent of the time that escalators are operat…
M
Escalator Availability
%
97
10
2011
97
646
Metro-North Railroad
Service Indicators
Percent of the time that escalators are operat…
M
Escalator Availability
%
97
11
2011
97
647
Metro-North Railroad
Service Indicators
Percent of the time that escalators are operat…
M
Escalator Availability
%
97
12
2011
97
648 rows × 12 columns
root
root.get('href')
root.text
## 二进制格式的数据
import pandas as pd
frame = pd.read_csv('data1.csv')
frame
frame.to_pickle('frame_pickle')
pd.read_pickle('frame_pickle')
a
b
c
d
message
0
1
2
3
4
hello
1
5
6
7
8
world
2
9
10
11
12
foo
### 使用HDF5格式
store = pd.HDFStore('mydata.h5')
store['obj1'] = frame
store['obj1_col'] = frame['a']
store
[{‘AGENCY_NAME’: ‘Metro-North Railroad’, ‘CATEGORY’: ‘Service Indicators’, ‘DESCRIPTION’: ‘Percent of commuter trains that arrive at their destinations within 5 minutes and 59 seconds of the scheduled time. West of Hudson services include the Pascack Valley and Port Jervis lines. Metro-North Railroad contracts with New Jersey Transit to operate service on these lines.\n’, ‘FREQUENCY’: ‘M’, ‘INDICATOR_NAME’: ‘On-Time Performance (West of Hudson)’, ‘INDICATOR_UNIT’: ‘%’, ‘MONTHLY_ACTUAL’: 96.9, ‘MONTHLY_TARGET’: 95.0, ‘PERIOD_MONTH’: 1, ‘PERIOD_YEAR’: 2008, ‘YTD_ACTUAL’: 96.9, ‘YTD_TARGET’: 95.0}, {‘AGENCY_NAME’: ‘Metro-North Railroad’, ‘CATEGORY’: ‘Service Indicators’, ‘DESCRIPTION’: ‘Percent of commuter trains that arrive at their destinations within 5 minutes and 59 seconds of the scheduled time. West of Hudson services include the Pascack Valley and Port Jervis lines. Metro-North Railroad contracts with New Jersey Transit to operate service on these lines.\n’, ‘FREQUENCY’: ‘M’, ‘INDICATOR_NAME’: ‘On-Time Performance (West of Hudson)’, ‘INDICATOR_UNIT’: ‘%’, ‘MONTHLY_ACTUAL’: 95.0, ‘MONTHLY_TARGET’: 95.0, ‘PERIOD_MONTH’: 2, ‘PERIOD_YEAR’: 2008, ‘YTD_ACTUAL’: 96.0, ‘YTD_TARGET’: 95.0}, {‘AGENCY_NAME’: ‘Metro-North Railroad’, ‘CATEGORY’: ‘Service Indicators’, ‘DESCRIPTION’: ‘Percent of commuter trains that arrive at their destinations within 5 minutes and 59 seconds of the scheduled time. West of Hudson services include the Pascack Valley and Port Jervis lines. Metro-North Railroad contracts with New Jersey Transit to operate service on these lines.\n’, ‘FREQUENCY’: ‘M’, ‘INDICATOR_NAME’: ‘On-Time Performance (West of Hudson)’, ‘INDICATOR_UNIT’: ‘%’, ‘MONTHLY_ACTUAL’: 96.9, ‘MONTHLY_TARGET’: 95.0, ‘PERIOD_MONTH’: 3, ‘PERIOD_YEAR’: 2008, ‘YTD_ACTUAL’: 96.3, ‘YTD_TARGET’: 95.0}, {‘AGENCY_NAME’: ‘Metro-North Railroad’, ‘CATEGORY’: ‘Service Indicators’, ‘DESCRIPTION’: ‘Percent of commuter trains that arrive at their destinations within 5 minutes and 59 seconds of the scheduled time. West of Hudson services include the Pascack Valley and Port Jervis lines. Metro-North Railroad contracts with New Jersey Transit to operate service on these lines.\n’, ‘FREQUENCY’: ‘M’, ‘INDICATOR_NAME’: ‘On-Time Performance (West of Hudson)’, ‘INDICATOR_UNIT’: ‘%’, ‘MONTHLY_ACTUAL’: 98.3, ‘MONTHLY_TARGET’: 95.0, ‘PERIOD_MONTH’: 4, ‘PERIOD_YEAR’: 2008, ‘YTD_ACTUAL’: 96.8, ‘YTD_TARGET’: 95.0}, {‘AGENCY_NAME’: ‘Metro-North Railroad’, ‘CATEGORY’: ‘Service Indicators’, ‘DESCRIPTION’: ‘Percent of commuter trains that arrive at their destinations within 5 minutes and 59 seconds of the scheduled time. West of Hudson services include the Pascack Valley and Port Jervis lines. Metro-North Railroad contracts with New Jersey Transit to operate service on these lines.\n’, ‘FREQUENCY’: ‘M’, ‘INDICATOR_NAME’: ‘On-Time Performance (West of Hudson)’, ‘INDICATOR_UNIT’: ‘%’, ‘MONTHLY_ACTUAL’: 95.8, ‘MONTHLY_TARGET’: 95.0, ‘PERIOD_MONTH’: 5, ‘PERIOD_YEAR’: 2008, ‘YTD_ACTUAL’: 96.6, ‘YTD_TARGET’: 95.0}]
issue_labels = DataFrame(data)
issue_labels
AGENCY_NAME
CATEGORY
DESCRIPTION
FREQUENCY
INDICATOR_NAME
INDICATOR_UNIT
MONTHLY_ACTUAL
MONTHLY_TARGET
PERIOD_MONTH
PERIOD_YEAR
YTD_ACTUAL
YTD_TARGET
0
Metro-North Railroad
Service Indicators
Percent of commuter trains that arrive at thei…
M
On-Time Performance (West of Hudson)
%
96.9
95
1
2008
96.9
95
1
Metro-North Railroad
Service Indicators
Percent of commuter trains that arrive at thei…
M
On-Time Performance (West of Hudson)
%
95
95
2
2008
96
95
2
Metro-North Railroad
Service Indicators
Percent of commuter trains that arrive at thei…
M
On-Time Performance (West of Hudson)
%
96.9
95
3
2008
96.3
95
3
Metro-North Railroad
Service Indicators
Percent of commuter trains that arrive at thei…
M
On-Time Performance (West of Hudson)
%
98.3
95
4
2008
96.8
95
4
Metro-North Railroad
Service Indicators
Percent of commuter trains that arrive at thei…
M
On-Time Performance (West of Hudson)
%
95.8
95
5
2008
96.6
95
5
Metro-North Railroad
Service Indicators
Percent of commuter trains that arrive at thei…
M
On-Time Performance (West of Hudson)
%
94.4
95
6
2008
96.2
95
6
Metro-North Railroad
Service Indicators
Percent of commuter trains that arrive at thei…
M
On-Time Performance (West of Hudson)
%
96
95
7
2008
96.2
95
7
Metro-North Railroad
Service Indicators
Percent of commuter trains that arrive at thei…
M
On-Time Performance (West of Hudson)
%
96.4
95
8
2008
96.2
95
8
Metro-North Railroad
Service Indicators
Percent of commuter trains that arrive at thei…
M
On-Time Performance (West of Hudson)
%
93.7
95
9
2008
95.9
95
9
Metro-North Railroad
Service Indicators
Percent of commuter trains that arrive at thei…
M
On-Time Performance (West of Hudson)
%
96.4
95
10
2008
96
95
10
Metro-North Railroad
Service Indicators
Percent of commuter trains that arrive at thei…
M
On-Time Performance (West of Hudson)
%
96.9
95
11
2008
96.1
95
11
Metro-North Railroad
Service Indicators
Percent of commuter trains that arrive at thei…
M
On-Time Performance (West of Hudson)
%
95.1
95
12
2008
96
95
12
Metro-North Railroad
Service Indicators
Percent of commuter trains that arrive at thei…
M
On-Time Performance (West of Hudson)
%
92.6
96.2
1
2009
92.6
96.2
13
Metro-North Railroad
Service Indicators
Percent of commuter trains that arrive at thei…
M
On-Time Performance (West of Hudson)
%
96.8
96.2
2
2009
94.6
96.2
14
Metro-North Railroad
Service Indicators
Percent of commuter trains that arrive at thei…
M
On-Time Performance (West of Hudson)
%
96.9
96.2
3
2009
95.4
96.2
15
Metro-North Railroad
Service Indicators
Percent of commuter trains that arrive at thei…
M
On-Time Performance (West of Hudson)
%
97.1
96.2
4
2009
95.9
96.2
16
Metro-North Railroad
Service Indicators
Percent of commuter trains that arrive at thei…
M
On-Time Performance (West of Hudson)
%
97.8
96.2
5
2009
96.2
96.2
17
Metro-North Railroad
Service Indicators
Percent of commuter trains that arrive at thei…
M
On-Time Performance (West of Hudson)
%
97.3
96.2
6
2009
96.4
96.2
18
Metro-North Railroad
Service Indicators
Percent of commuter trains that arrive at thei…
M
On-Time Performance (West of Hudson)
%
96.7
96.2
7
2009
96.5
96.2
19
Metro-North Railroad
Service Indicators
Percent of commuter trains that arrive at thei…
M
On-Time Performance (West of Hudson)
%
95.7
96.2
8
2009
96.4
96.2
20
Metro-North Railroad
Service Indicators
Percent of commuter trains that arrive at thei…
M
On-Time Performance (West of Hudson)
%
96.1
96.2
9
2009
96.3
96.2
21
Metro-North Railroad
Service Indicators
Percent of commuter trains that arrive at thei…
M
On-Time Performance (West of Hudson)
%
94.8
96.2
10
2009
96.2
96.2
22
Metro-North Railroad
Service Indicators
Percent of commuter trains that arrive at thei…
M
On-Time Performance (West of Hudson)
%
95.7
96.2
11
2009
96.1
96.2
23
Metro-North Railroad
Service Indicators
Percent of commuter trains that arrive at thei…
M
On-Time Performance (West of Hudson)
%
95
96.2
12
2009
96
96.2
24
Metro-North Railroad
Service Indicators
Percent of commuter trains that arrive at thei…
M
On-Time Performance (West of Hudson)
%
98
96.3
1
2010
98
96.3
25
Metro-North Railroad
Service Indicators
Percent of commuter trains that arrive at thei…
M
On-Time Performance (West of Hudson)
%
93
96.3
2
2010
95.6
96.3
26
Metro-North Railroad
Service Indicators
Percent of commuter trains that arrive at thei…
M
On-Time Performance (West of Hudson)
%
96.9
96.3
3
2010
96.1
96.3
27
Metro-North Railroad
Service Indicators
Percent of commuter trains that arrive at thei…
M
On-Time Performance (West of Hudson)
%
98.1
96.3
4
2010
96.6
96.3
28
Metro-North Railroad
Service Indicators
Percent of commuter trains that arrive at thei…
M
On-Time Performance (West of Hudson)
%
97.6
96.3
5
2010
96.8
96.3
29
Metro-North Railroad
Service Indicators
Percent of commuter trains that arrive at thei…
M
On-Time Performance (West of Hudson)
%
97.4
96.3
6
2010
96.9
96.3
…
…
…
…
…
…
…
…
…
…
…
…
…
618
Metro-North Railroad
Service Indicators
Percent of the time that escalators are operat…
M
Escalator Availability
%
94
7
2009
95.14
619
Metro-North Railroad
Service Indicators
Percent of the time that escalators are operat…
M
Escalator Availability
%
97
8
2009
95.38
620
Metro-North Railroad
Service Indicators
Percent of the time that escalators are operat…
M
Escalator Availability
%
98.3
9
2009
95.7
621
Metro-North Railroad
Service Indicators
Percent of the time that escalators are operat…
M
Escalator Availability
%
98.7
10
2009
96
622
Metro-North Railroad
Service Indicators
Percent of the time that escalators are operat…
M
Escalator Availability
%
98.1
11
2009
96.21
623
Metro-North Railroad
Service Indicators
Percent of the time that escalators are operat…
M
Escalator Availability
%
100
12
2009
96.5
624
Metro-North Railroad
Service Indicators
Percent of the time that escalators are operat…
M
Escalator Availability
%
97.95
97
1
2010
97.95
97
625
Metro-North Railroad
Service Indicators
Percent of the time that escalators are operat…
M
Escalator Availability
%
100
97
2
2010
98.92
97
626
Metro-North Railroad
Service Indicators
Percent of the time that escalators are operat…
M
Escalator Availability
%
100
97
3
2010
99.29
97
627
Metro-North Railroad
Service Indicators
Percent of the time that escalators are operat…
M
Escalator Availability
%
100
97
4
2010
99.47
97
628
Metro-North Railroad
Service Indicators
Percent of the time that escalators are operat…
M
Escalator Availability
%
100
97
5
2010
99.58
97
629
Metro-North Railroad
Service Indicators
Percent of the time that escalators are operat…
M
Escalator Availability
%
91.21
97
6
2010
98.19
97
630
Metro-North Railroad
Service Indicators
Percent of the time that escalators are operat…
M
Escalator Availability
%
100
97
7
2010
98.46
97
631
Metro-North Railroad
Service Indicators
Percent of the time that escalators are operat…
M
Escalator Availability
%
100
97
8
2010
98.69
97
632
Metro-North Railroad
Service Indicators
Percent of the time that escalators are operat…
M
Escalator Availability
%
95.2
97
9
2010
98.3
97
633
Metro-North Railroad
Service Indicators
Percent of the time that escalators are operat…
M
Escalator Availability
%
90.91
97
10
2010
97.55
97
634
Metro-North Railroad
Service Indicators
Percent of the time that escalators are operat…
M
Escalator Availability
%
96.67
97
11
2010
97.47
97
635
Metro-North Railroad
Service Indicators
Percent of the time that escalators are operat…
M
Escalator Availability
%
90.03
97
12
2010
96.84
97
636
Metro-North Railroad
Service Indicators
Percent of the time that escalators are operat…
M
Escalator Availability
%
100
97
1
2011
100
97
637
Metro-North Railroad
Service Indicators
Percent of the time that escalators are operat…
M
Escalator Availability
%
100
97
2
2011
100
97
638
Metro-North Railroad
Service Indicators
Percent of the time that escalators are operat…
M
Escalator Availability
%
97.07
97
3
2011
98.86
97
639
Metro-North Railroad
Service Indicators
Percent of the time that escalators are operat…
M
Escalator Availability
%
98.18
97
4
2011
98.76
97
640
Metro-North Railroad
Service Indicators
Percent of the time that escalators are operat…
M
Escalator Availability
%
79.18
97
5
2011
90.91
97
641
Metro-North Railroad
Service Indicators
Percent of the time that escalators are operat…
M
Escalator Availability
%
97
6
2011
97
642
Metro-North Railroad
Service Indicators
Percent of the time that escalators are operat…
M
Escalator Availability
%
97
7
2011
97
643
Metro-North Railroad
Service Indicators
Percent of the time that escalators are operat…
M
Escalator Availability
%
97
8
2011
97
644
Metro-North Railroad
Service Indicators
Percent of the time that escalators are operat…
M
Escalator Availability
%
97
9
2011
97
645
Metro-North Railroad
Service Indicators
Percent of the time that escalators are operat…
M
Escalator Availability
%
97
10
2011
97
646
Metro-North Railroad
Service Indicators
Percent of the time that escalators are operat…
M
Escalator Availability
%
97
11
2011
97
647
Metro-North Railroad
Service Indicators
Percent of the time that escalators are operat…
M
Escalator Availability
%
97
12
2011
97
648 rows × 12 columns
## 2.数据库相关操作 ## 2.1 sqlite数据库
import sqlite3
query = """
CREATE TABLE test
(a VARCHAR(20), b VARCHAR(20),
c REAL, d INTEGER
);"""
con = sqlite3.connect(':memory:')
con.execute(query)
con.commit()
data = [('Atlanta', 'Georgia', 1.25, 6),
('Tallahassee', 'Florida', 2.6, 3),
('Sacramento', 'California', 1.7, 5)]
stmt = "INSERT INTO test VALUES(?, ?, ?, ?)"
con.executemany(stmt, data)
con.commit()
cursor = con.execute('select * from test')
rows = cursor.fetchall()
rows
import pandas.io.sql as sql
sql.read_sql('select * from test', con)
a
b
c
d
0
Atlanta
Georgia
1.25
6
1
Tallahassee
Florida
2.60
3
2
Sacramento
California
1.70
5
3.2 MySQL数据库
#coding=utf-8import pymysql
conn= pymysql.connect(
host='localhost',
port = 3306,
user='root',
passwd='123456',
db ='test',
)
cur = conn.cursor()
#创建数据表#cur.execute("create table student(id int ,name varchar(20),class varchar(30),age varchar(10))")#插入一条数据#cur.execute("insert into student values('2','Tom','3 year 2 class','9')")#修改查询条件的数据#cur.execute("update student set class='3 year 1 class' where name = 'Tom'")#删除查询条件的数据#cur.execute("delete from student where age='9'")
cur.close()
conn.commit()
conn.close()
---------------------------------------------------------------------------
ConnectionRefusedError Traceback (most recent call last)
C:\Program Files\Anaconda3\lib\site-packages\pymysql\connections.py in connect(self, sock)
889 sock = socket.create_connection(
--> 890 (self.host, self.port), self.connect_timeout)
891 break
C:\Program Files\Anaconda3\lib\socket.py in create_connection(address, timeout, source_address)
710 if err is not None:
--> 711 raise err
712 else:
C:\Program Files\Anaconda3\lib\socket.py in create_connection(address, timeout, source_address)
701 sock.bind(source_address)
--> 702 sock.connect(sa)
703 return sock
ConnectionRefusedError: [WinError 10061] 由于目标计算机积极拒绝,无法连接。
During handling of the above exception, another exception occurred:
OperationalError Traceback (most recent call last)
<ipython-input-46-1caebd09ae3e> in <module>()
7 user='root',
8 passwd='123456',
----> 9 db ='test',
10 )
11 cur = conn.cursor()
C:\Program Files\Anaconda3\lib\site-packages\pymysql\__init__.py in Connect(*args, **kwargs)
88 """
89 from .connections import Connection
---> 90 return Connection(*args, **kwargs)
91
92 from pymysql import connections as _orig_conn
C:\Program Files\Anaconda3\lib\site-packages\pymysql\connections.py in __init__(self, host, user, password, database, port, unix_socket, charset, sql_mode, read_default_file, conv, use_unicode, client_flag, cursorclass, init_command, connect_timeout, ssl, read_default_group, compress, named_pipe, no_delay, autocommit, db, passwd, local_infile, max_allowed_packet, defer_connect, auth_plugin_map, read_timeout, write_timeout)
686 self._sock = None
687 else:
--> 688 self.connect()
689
690 def _create_ssl_ctx(self, sslp):
C:\Program Files\Anaconda3\lib\site-packages\pymysql\connections.py in connect(self, sock)
935 exc.traceback = traceback.format_exc()
936 if DEBUG: print(exc.traceback)
--> 937 raise exc
938
939 # If e is neither DatabaseError or IOError, It's a bug.
OperationalError: (2003, "Can't connect to MySQL server on 'localhost' ([WinError 10061] 由于目标计算机积极拒绝,无法连接。)")
3.3 Memcache
#coding:utf8import memcache
classMemcachedClient():''' python memcached 客户端操作示例 '''def__init__(self, hostList):
self.__mc = memcache.Client(hostList);
defset(self, key, value):
result = self.__mc.set("name", "NieYong")
return result
defget(self, key):
name = self.__mc.get("name")
return name
defdelete(self, key):
result = self.__mc.delete("name")
return result
if __name__ == '__main__':
mc = MemcachedClient(["127.0.0.1:11511", "127.0.0.1:11512"])
key = "name"
result = mc.set(key, "NieYong")
print("set的结果:", result)
name = mc.get(key)
print("get的结果:", name)
result = mc.delete(key)
print("delete的结果:", result)
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-48-1c58d1aac9bf> in <module>()
1
----> 2 import memcache
3
4 class MemcachedClient():
5 ''' python memcached 客户端操作示例 '''
ImportError: No module named 'memcache'
3.4 MongoDB
#encoding:utf=8 import pymongo
connection=pymongo.Connection('10.32.38.50',27017)
#选择myblog库
db=connection.myblog
# 使用users集合
collection=db.users
# 添加单条数据到集合中
user = {"name":"cui","age":"10"}
collection.insert(user)
#同时添加多条数据到集合中
users=[{"name":"cui","age":"9"},{"name":"cui","age":"11"}]
collection.insert(users)
#查询单条记录
print(collection.find_one() )
#查询所有记录 for data in collection.find():
print(data )
#查询此集合中数据条数
print(collection.count())
#简单参数查询 for data in collection.find({"name":"1"}):
print(data)
#使用find_one获取一条记录
print(collection.find_one({"name":"1"}))
#高级查询
print("__________________________________________")
print('''''collection.find({"age":{"$gt":"10"}})''')
print("__________________________________________")
for data in collection.find({"age":{"$gt":"10"}}).sort("age"):
print(data)
# 查看db下的所有集合
print(db.collection_names())
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-49-7bcb5fe4f264> in <module>()
1
----> 2 import pymongo
3
4 connection=pymongo.Connection('10.32.38.50',27017)
5
ImportError: No module named 'pymongo'