SlideShare a Scribd company logo
From Data to Display
DSP E1
Tim Hong
Tim Hong
E-mail:  timhong@dsp.im 
的
不DSP
不
不
不
OpenData
Data
在
了 你
在
在
Pandas
The IPython Notebook
IPython Notebook 1
Web-based
IPython Notebook II
Server
IPython Notebook III
IPython Notebook
Hello World
IPython Notebook
Hello World
Cell
IPython Notebook
Hello World
File naming
Just like google Doc.
IPython Notebook
Hello World
IPython Notebook
Now you try
(1+12)*15
IPython Notebook
Now you try
(1+12)*15
IPython Notebook
Now you try
Sin
IPython Notebook
Now you try
“Sin ”?????!!!!!
IPython Notebook Now you try
“sin”
and try
“cos”
Ubike
在
在
CSV
Ubike
iid ItemId
sv Ex.“12”
sd yyyyMMddhhmmss vtyp Ex.“1”.
sno
sna
sip IP
tot Ex.“38”
sbi Ex.“23”
sarea EX.“ ”
mday yyyyMMddhhmmss EX.“20120426132314”
lat Ex.“25.0408388889”
lng 我 Ex.“121.567894444”
ar EX.“ 2 235 ”
sareaen EX.“Xinyi Dist.”
snaen
nbcnt EX,“2”
bemp EX,“12”
Using DataFrames
broken_df = pd.read_csv(' / /
ubikeCSV.csv',
encoding='latin1',
parse_dates=['id'],
dayfirst=True,
index_col='id')
BUT!!!!!!!!
Multiple columns
Selection
Multiple columns
Ubike
http://public.enthought.com/~kjordahl/pydata/slides/#1
在
在
How to do it?
Plotting
But!!!
local
server
PostgreSQL
iPython Notebook
Postgresql
Adaptor “Psycopg2”
Psycopg2??
SQL type data base
Adaptor “Psycopg2”
Local
⼀一
⼀一 ⼀一Port
是 Print 是
iPython Notebook SQL
Select ___ , ____ From ____ Where .....
了 在你
iPython Pandas
+
PostgreSQL
+
Display
在
In [14]:
# Import all libraries needed for the tutorial
# General syntax to import specific functions in a library:
##from (library) import (specific library function)
from pandas import DataFrame, read_csv
# General syntax to import a library but no functions:
##import (library) as (give the library a nickname/alias)
import matplotlib.pyplot as plt
import pandas as pd #this is how I usually import pandas
import sys #only needed to determine Python version number
# Enable inline plotting
%matplotlib inline
%pylab inline
# Must get this or you will get # NameError: name 'figsize' is not defined
import matplotlib.pylab
pd.set_option('display.mpl_style', 'default') # Make the graphs a bit prettier
figsize(15, 5)
print 'Python version ' + sys.version
print 'Pandas version ' + pd.__version__
sql
In [15]:
import pandas.io.sql
import psycopg2
conn = psycopg2.connect(user='lab')
cur = conn.cursor()
print 0 ...
In [16]:
# conn.close()
Populating the interactive namespace from numpy and matplotlib
Python version 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2]
Pandas version 0.16.0
In [17]:
try:
cur = conn.cursor()
cur.execute('SELECT 1')
except psycopg2.OperationalError:
pass
print conn.closed # 2
In [18]:
# query db
sql = """
select * from ubike limit 3
"""
ubike_df = pandas.io.sql.read_sql(sql, conn)
ubike_df.head() # try
In [19]:
ubike_df.columns
0
Out[18]:
when_ts where_pt code name area_name space_num avg_bike_num max_bike_
0
2014-
12-08
15:00:00
(25.041,121.556945) 2
(2
)
48 24.000 27
1
2014-
12-08
15:00:00
(25.037797,121.565169) 3 40 10.333 13
2
2014-
12-08
15:00:00
(25.036036,121.562325) 4 60 39.333 40
Out[19]:
Index([u'when_ts', u'where_pt', u'code', u'name', u'area_name', u'
space_num', u'avg_bike_num', u'max_bike_num', u'min_bike_num', u'b
ike_num_std', u'avg_space_num', u'max_space_num', u'min_space_num'
, u'space_num_std'], dtype='object')
In [20]:
ubike_df.dtypes
In [31]:
# query db
sql = """
select * from ubike
"""
all = pandas.io.sql.read_sql(sql, conn)
In [33]:
all['avg_bike_num'] .plot(legend=False)
Out[20]:
when_ts datetime64[ns]
where_pt object
code object
name object
area_name object
space_num int64
avg_bike_num float64
max_bike_num int64
min_bike_num int64
bike_num_std float64
avg_space_num float64
max_space_num int64
min_space_num int64
space_num_std float64
dtype: object
Out[33]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f373b908310>
when_ts datetime64[ns] u' ', u' ', where_pt object u' ', u' ', code object u' ', name
object u' ', area_name object u' ', space_num int64 u' ', avg_bike_num float64
u' ', max_bike_num int64 u' ', min_bike_num int64 u' ', bike_num_std
float64 u' ', avg_space_num float64 u' ', max_space_num int64 u' ',
min_space_num int64 u' ', space_num_std float64 u' '
In [21]:
# query db
sql = """
select * from ubike where name = ' ' and (when_ts BETWEEN '2014-12-08'
AND '2014-12-09')
order by when_ts;
"""
df = pandas.io.sql.read_sql(sql, conn)
len(df)
In [22]:
df
Out[21]:
10
pandas or python
Out[22]:
when_ts where_pt code name area_name space_num avg_bike_num max_bike_
0
2014-
12-08
15:00:00
(25.049845,121.571885) 15 60 38.667 39
1
2014-
12-08
16:00:00
(25.049845,121.571885) 15 60 40.867 44
2
2014-
12-08
17:00:00
(25.049845,121.571885) 15 60 44.000 48
3
2014-
12-08
18:00:00
(25.049845,121.571885) 15 60 39.933 49
4
2014-
12-08
19:00:00
(25.049845,121.571885) 15 60 26.400 29
5
2014-
12-08
20:00:00
(25.049845,121.571885) 15 60 30.000 31
6
2014-
12-08
21:00:00
(25.049845,121.571885) 15 60 32.667 34
7
2014-
12-08
22:00:00
(25.049845,121.571885) 15 60 35.133 39
8
2014-
12-08
23:00:00
(25.049845,121.571885) 15 60 35.000 39
9
2014-
12-09
00:00:00
(25.049845,121.571885) 15 60 28.643 33
In [23]:
df[:3] # :3, :10, 5:10, 'name'
In [24]:
df[['when_ts','avg_bike_num']]
Out[23]:
when_ts where_pt code name area_name space_num avg_bike_num max_bike_
0
2014-
12-08
15:00:00
(25.049845,121.571885) 15 60 38.667 39
1
2014-
12-08
16:00:00
(25.049845,121.571885) 15 60 40.867 44
2
2014-
12-08
17:00:00
(25.049845,121.571885) 15 60 44.000 48
Out[24]:
when_ts avg_bike_num
0 2014-12-08 15:00:00 38.667
1 2014-12-08 16:00:00 40.867
2 2014-12-08 17:00:00 44.000
3 2014-12-08 18:00:00 39.933
4 2014-12-08 19:00:00 26.400
5 2014-12-08 20:00:00 30.000
6 2014-12-08 21:00:00 32.667
7 2014-12-08 22:00:00 35.133
8 2014-12-08 23:00:00 35.000
9 2014-12-09 00:00:00 28.643
In [25]:
df[['when_ts','avg_bike_num']].plot()
In [26]:
from matplotlib.font_manager import FontProperties, findfont
fp = FontProperties(family='monospace',
style='normal',
variant='normal',
weight='normal',
stretch='normal',
size='medium')
font = findfont(fp)
In [27]:
df[['when_ts','avg_bike_num']].plot()
Out[25]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f3755f87110>
Out[27]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f37560d2050>
In [28]:
df[['when_ts','avg_bike_num']].plot(kind='kde')
In [16]:
# query db
sql = """
select * from ubike where name = ' 'and (when_ts BETWEEN '2014-12-08' AND
'2014-12-09')
order by when_ts;
"""
ponit2_df = pandas.io.sql.read_sql(sql, conn)
len(ponit2_df)
In [17]:
ponit2_df[:1]
Out[28]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f3756096910>
Out[16]:
10
Out[17]:
when_ts where_pt code name area_name space_num avg_bike_num max_bike_
0
2014-
12-08
15:00:00
(25.048268,121.552278) 18 38 13.5 15
In [18]:
df['avg_bike_num'].plot()
ponit2_df['avg_bike_num'].plot()
In [19]:
ponit2_df.plot(x='when_ts', y='avg_bike_num')
Out[18]:
<matplotlib.axes._subplots.AxesSubplot at 0x7fba9e2b9490>
Out[19]:
<matplotlib.axes._subplots.AxesSubplot at 0x7fba9e167290>
In [20]:
ponit2_df.plot(x='when_ts', y='avg_bike_num');
df.plot(x='when_ts', y='avg_bike_num')
In [21]:
df[['max_space_num','min_space_num']].plot(kind='area');
Out[20]:
<matplotlib.axes._subplots.AxesSubplot at 0x7fba9dfa1810>
In [22]:
df[['space_num','max_space_num','min_space_num']].plot(kind='area', stacked=Fa
lse);
Table
In [112]:
fig, ax = plt.subplots(1, 1)
ax.get_xaxis().set_visible(False)
df[['space_num','max_space_num','min_space_num']].plot(table=True, ax=ax)
Out[112]:
<matplotlib.axes._subplots.AxesSubplot at 0x7fba80d96490>
In [121]:
from pandas.tools.plotting import table
fig, ax = plt.subplots(1, 1)
table(ax, np.round(df[['space_num','max_space_num','min_space_num']].describe(
), 2),
loc='upper left', colWidths=[0.1, 0.1, 0.1])
df[['space_num','max_space_num','min_space_num']].plot(table=True, ax=ax)
sql
In [124]:
# query db
sql = """
select a.when_ts as time ,
a.avg_bike_num as point_A,
b.avg_bike_num as point_B
from ubike a
inner join ubike b on
a.when_ts = b.when_ts
and (a.when_ts BETWEEN '2014-12-08' AND '2014-12-09')
and (a.name = ' ' and b.name = ' ');
"""
PointJoin = pandas.io.sql.read_sql(sql, conn)
len(PointJoin)
Out[121]:
<matplotlib.axes._subplots.AxesSubplot at 0x7fba80348ad0>
Out[124]:
10
In [126]:
PointJoin
In [127]:
fig, ax = plt.subplots(1, 1)
ax.get_xaxis().set_visible(False)
PointJoin[['point_a','point_b']].plot(table=True, ax=ax)
Bar
In [ ]:
## Now you try loc='upper left' colWidths=[0.1, 0.1, 0.1] remove tabl
e=True
Out[126]:
time point_a point_b
0 2014-12-08 15:00:00 13.500 38.667
1 2014-12-08 16:00:00 12.733 40.867
2 2014-12-08 17:00:00 9.615 44.000
3 2014-12-08 18:00:00 12.267 39.933
4 2014-12-08 19:00:00 13.933 26.400
5 2014-12-08 20:00:00 17.200 30.000
6 2014-12-08 21:00:00 10.667 32.667
7 2014-12-08 22:00:00 4.400 35.133
8 2014-12-08 23:00:00 3.867 35.000
9 2014-12-09 00:00:00 0.929 28.643
Out[127]:
<matplotlib.axes._subplots.AxesSubplot at 0x7fba8000e150>
In [23]:
ponit2_df.plot(kind='bar');
In [25]:
ponit2_df.plot(kind='bar',x='when_ts', y='avg_bike_num');
In [26]:
ponit2_df[['max_space_num','min_space_num']].plot(kind='bar');
In [27]:
ponit2_df[['max_space_num','min_space_num']].plot(kind='bar',alpha=0.5,stacked
=True);
In [28]:
ponit2_df[['max_space_num','min_space_num']].plot(kind='barh', stacked=True);
In [29]:
ponit2_df[['max_space_num','min_space_num']].plot(kind='hist')
In [30]:
ponit2_df[['max_space_num','min_space_num']].hist()
In [31]:
ponit2_df[:1]
Out[29]:
<matplotlib.axes._subplots.AxesSubplot at 0x7fba9dab6c90>
Out[30]:
array([[<matplotlib.axes._subplots.AxesSubplot object at 0x7fba9da
71d90>,
<matplotlib.axes._subplots.AxesSubplot object at 0x7fba9da
ece90>]], dtype=object)
Out[31]:
when_ts where_pt code name area_name space_num avg_bike_num max_bike_
0
2014-
12-08
15:00:00
(25.048268,121.552278) 18 38 13.5 15
In [32]:
# query db
sql = """
select name from ubike where area_name like '% %' group by name
"""
pointA = pandas.io.sql.read_sql(sql, conn)
len(pointA)
In [33]:
pointA
Out[32]:
14
Out[33]:
name
0
1
2 (2 )
3
4
5 (2 )
6
7
8
9
10
11 (2 )
12
13
In [90]:
# query db
sql = """
select * from ubike where name = ' ' and (when_ts BETWEEN '2014-12
-25' AND '2014-12-31')
"""
pointA = pandas.io.sql.read_sql(sql, conn)
len(pointA)
In [35]:
pointA[:1]
In [91]:
# query db
sql = """
select * from tpweather where name = ' ' and (when_ts BETWEEN '2014-1
2-25' AND '2014-12-31');
"""
weaterA = pandas.io.sql.read_sql(sql, conn)
len(weaterA)
In [37]:
weaterA[:1]
Out[90]:
145
Out[35]:
when_ts where_pt code name area_name space_num avg_bike_num max_bike_
0
2014-
12-08
15:00:00
(25.116325,121.534136) 123 44 31 33
Out[91]:
145
Out[37]:
when_ts where_pt name temp max_temp min_temp hum_pct pressure win
0
2014-
12-19
(25.1180133,121.5373439) 17.3889 17.4 16.9 76 1022.38 2.7
sub select
In [92]:
# query db
sql = """
select * from tpweather where when_ts in
(select when_ts
from ubike
where name = ' ' order by when_ts )
and name = ' ' and (when_ts BETWEEN '2014-12-25' AND '2014-12-31' ) ord
er by when_ts
"""
weatherA = pandas.io.sql.read_sql(sql, conn)
len(weatherA)
In [93]:
len(pointA)
In [94]:
len(weatherA)
Out[92]:
145
Out[93]:
145
Out[94]:
145
In [95]:
pointA.plot(x='when_ts', y='avg_bike_num')
weatherA.plot(x='when_ts', y='rainfall')
Out[95]:
<matplotlib.axes._subplots.AxesSubplot at 0x7fba9c0f5890>
In [96]:
pointA['avg_bike_num'].plot()
weatherA['rainfall'].plot()
inner join
In [ ]:
Out[96]:
<matplotlib.axes._subplots.AxesSubplot at 0x7fba977c5e50>
• ——
不了Sam Redwine你
• Software and cathedrals are much the same
– first we build them, then we pray. (Sam
Redwine)

More Related Content

What's hot

Building Real Time Systems on MongoDB Using the Oplog at Stripe
Building Real Time Systems on MongoDB Using the Oplog at StripeBuilding Real Time Systems on MongoDB Using the Oplog at Stripe
Building Real Time Systems on MongoDB Using the Oplog at StripeMongoDB
 
Python 내장 함수
Python 내장 함수Python 내장 함수
Python 내장 함수
용 최
 
The Ring programming language version 1.5 book - Part 8 of 31
The Ring programming language version 1.5 book - Part 8 of 31The Ring programming language version 1.5 book - Part 8 of 31
The Ring programming language version 1.5 book - Part 8 of 31
Mahmoud Samir Fayed
 
Functional Scala 2020
Functional Scala 2020Functional Scala 2020
Functional Scala 2020
Alexander Ioffe
 
The Ring programming language version 1.5.2 book - Part 66 of 181
The Ring programming language version 1.5.2 book - Part 66 of 181The Ring programming language version 1.5.2 book - Part 66 of 181
The Ring programming language version 1.5.2 book - Part 66 of 181
Mahmoud Samir Fayed
 
Goptuna Distributed Bayesian Optimization Framework at Go Conference 2019 Autumn
Goptuna Distributed Bayesian Optimization Framework at Go Conference 2019 AutumnGoptuna Distributed Bayesian Optimization Framework at Go Conference 2019 Autumn
Goptuna Distributed Bayesian Optimization Framework at Go Conference 2019 Autumn
Masashi Shibata
 
Машинное обучение на JS. С чего начать и куда идти | Odessa Frontend Meetup #12
Машинное обучение на JS. С чего начать и куда идти | Odessa Frontend Meetup #12Машинное обучение на JS. С чего начать и куда идти | Odessa Frontend Meetup #12
Машинное обучение на JS. С чего начать и куда идти | Odessa Frontend Meetup #12
OdessaFrontend
 
The Ring programming language version 1.8 book - Part 53 of 202
The Ring programming language version 1.8 book - Part 53 of 202The Ring programming language version 1.8 book - Part 53 of 202
The Ring programming language version 1.8 book - Part 53 of 202
Mahmoud Samir Fayed
 
Time Series Analysis Sample Code
Time Series Analysis Sample CodeTime Series Analysis Sample Code
Time Series Analysis Sample Code
Aiden Wu, FRM
 
Артём Акуляков - F# for Data Analysis
Артём Акуляков - F# for Data AnalysisАртём Акуляков - F# for Data Analysis
Артём Акуляков - F# for Data Analysis
SpbDotNet Community
 
python高级内存管理
python高级内存管理python高级内存管理
python高级内存管理
rfyiamcool
 
MongoDB Analytics
MongoDB AnalyticsMongoDB Analytics
MongoDB Analyticsdatablend
 
R part II
R part IIR part II
R part II
Ruru Chowdhury
 
Clustering com numpy e cython
Clustering com numpy e cythonClustering com numpy e cython
Clustering com numpy e cythonAnderson Dantas
 
미려한 UI/UX를 위한 여정
미려한 UI/UX를 위한 여정미려한 UI/UX를 위한 여정
미려한 UI/UX를 위한 여정
SeungChul Kang
 
Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語
ikdysfm
 
The Ring programming language version 1.7 book - Part 71 of 196
The Ring programming language version 1.7 book - Part 71 of 196The Ring programming language version 1.7 book - Part 71 of 196
The Ring programming language version 1.7 book - Part 71 of 196
Mahmoud Samir Fayed
 
Time Series Analysis for Network Secruity
Time Series Analysis for Network SecruityTime Series Analysis for Network Secruity
Time Series Analysis for Network Secruity
mrphilroth
 
Introduction to R
Introduction to RIntroduction to R
Introduction to R
Sander Kieft
 
Raspberry Pi à la GroovyFX
Raspberry Pi à la GroovyFXRaspberry Pi à la GroovyFX
Raspberry Pi à la GroovyFX
Stephen Chin
 

What's hot (20)

Building Real Time Systems on MongoDB Using the Oplog at Stripe
Building Real Time Systems on MongoDB Using the Oplog at StripeBuilding Real Time Systems on MongoDB Using the Oplog at Stripe
Building Real Time Systems on MongoDB Using the Oplog at Stripe
 
Python 내장 함수
Python 내장 함수Python 내장 함수
Python 내장 함수
 
The Ring programming language version 1.5 book - Part 8 of 31
The Ring programming language version 1.5 book - Part 8 of 31The Ring programming language version 1.5 book - Part 8 of 31
The Ring programming language version 1.5 book - Part 8 of 31
 
Functional Scala 2020
Functional Scala 2020Functional Scala 2020
Functional Scala 2020
 
The Ring programming language version 1.5.2 book - Part 66 of 181
The Ring programming language version 1.5.2 book - Part 66 of 181The Ring programming language version 1.5.2 book - Part 66 of 181
The Ring programming language version 1.5.2 book - Part 66 of 181
 
Goptuna Distributed Bayesian Optimization Framework at Go Conference 2019 Autumn
Goptuna Distributed Bayesian Optimization Framework at Go Conference 2019 AutumnGoptuna Distributed Bayesian Optimization Framework at Go Conference 2019 Autumn
Goptuna Distributed Bayesian Optimization Framework at Go Conference 2019 Autumn
 
Машинное обучение на JS. С чего начать и куда идти | Odessa Frontend Meetup #12
Машинное обучение на JS. С чего начать и куда идти | Odessa Frontend Meetup #12Машинное обучение на JS. С чего начать и куда идти | Odessa Frontend Meetup #12
Машинное обучение на JS. С чего начать и куда идти | Odessa Frontend Meetup #12
 
The Ring programming language version 1.8 book - Part 53 of 202
The Ring programming language version 1.8 book - Part 53 of 202The Ring programming language version 1.8 book - Part 53 of 202
The Ring programming language version 1.8 book - Part 53 of 202
 
Time Series Analysis Sample Code
Time Series Analysis Sample CodeTime Series Analysis Sample Code
Time Series Analysis Sample Code
 
Артём Акуляков - F# for Data Analysis
Артём Акуляков - F# for Data AnalysisАртём Акуляков - F# for Data Analysis
Артём Акуляков - F# for Data Analysis
 
python高级内存管理
python高级内存管理python高级内存管理
python高级内存管理
 
MongoDB Analytics
MongoDB AnalyticsMongoDB Analytics
MongoDB Analytics
 
R part II
R part IIR part II
R part II
 
Clustering com numpy e cython
Clustering com numpy e cythonClustering com numpy e cython
Clustering com numpy e cython
 
미려한 UI/UX를 위한 여정
미려한 UI/UX를 위한 여정미려한 UI/UX를 위한 여정
미려한 UI/UX를 위한 여정
 
Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語
 
The Ring programming language version 1.7 book - Part 71 of 196
The Ring programming language version 1.7 book - Part 71 of 196The Ring programming language version 1.7 book - Part 71 of 196
The Ring programming language version 1.7 book - Part 71 of 196
 
Time Series Analysis for Network Secruity
Time Series Analysis for Network SecruityTime Series Analysis for Network Secruity
Time Series Analysis for Network Secruity
 
Introduction to R
Introduction to RIntroduction to R
Introduction to R
 
Raspberry Pi à la GroovyFX
Raspberry Pi à la GroovyFXRaspberry Pi à la GroovyFX
Raspberry Pi à la GroovyFX
 

Similar to Pandas+postgre sql 實作 with code

CONFidence 2015: DTrace + OSX = Fun - Andrzej Dyjak
CONFidence 2015: DTrace + OSX = Fun - Andrzej Dyjak   CONFidence 2015: DTrace + OSX = Fun - Andrzej Dyjak
CONFidence 2015: DTrace + OSX = Fun - Andrzej Dyjak
PROIDEA
 
Beyond PHP - It's not (just) about the code
Beyond PHP - It's not (just) about the codeBeyond PHP - It's not (just) about the code
Beyond PHP - It's not (just) about the code
Wim Godden
 
r2con 2017 r2cLEMENCy
r2con 2017 r2cLEMENCyr2con 2017 r2cLEMENCy
r2con 2017 r2cLEMENCy
Ray Song
 
Beyond php - it's not (just) about the code
Beyond php - it's not (just) about the codeBeyond php - it's not (just) about the code
Beyond php - it's not (just) about the code
Wim Godden
 
Business Dashboards using Bonobo ETL, Grafana and Apache Airflow
Business Dashboards using Bonobo ETL, Grafana and Apache AirflowBusiness Dashboards using Bonobo ETL, Grafana and Apache Airflow
Business Dashboards using Bonobo ETL, Grafana and Apache Airflow
Romain Dorgueil
 
A miało być tak... bez wycieków
A miało być tak... bez wyciekówA miało być tak... bez wycieków
A miało być tak... bez wycieków
Konrad Kokosa
 
All I know about rsc.io/c2go
All I know about rsc.io/c2goAll I know about rsc.io/c2go
All I know about rsc.io/c2goMoriyoshi Koizumi
 
DSD-INT 2018 Work with iMOD MODFLOW models in Python - Visser Bootsma
DSD-INT 2018 Work with iMOD MODFLOW models in Python - Visser BootsmaDSD-INT 2018 Work with iMOD MODFLOW models in Python - Visser Bootsma
DSD-INT 2018 Work with iMOD MODFLOW models in Python - Visser Bootsma
Deltares
 
pa-pe-pi-po-pure Python Text Processing
pa-pe-pi-po-pure Python Text Processingpa-pe-pi-po-pure Python Text Processing
pa-pe-pi-po-pure Python Text Processing
Rodrigo Senra
 
Beyond php - it's not (just) about the code
Beyond php - it's not (just) about the codeBeyond php - it's not (just) about the code
Beyond php - it's not (just) about the code
Wim Godden
 
Learning Dtrace
Learning DtraceLearning Dtrace
Learning Dtrace
JeongHun Byeon
 
Python for data science by www.dmdiploma.com
Python for data science by www.dmdiploma.comPython for data science by www.dmdiploma.com
Python for data science by www.dmdiploma.com
ShwetaAggarwal56
 
Python Software Testing in Kytos.io
Python Software Testing in Kytos.ioPython Software Testing in Kytos.io
Python Software Testing in Kytos.io
Carlos Eduardo Moreira dos Santos
 
End-to-end Big Data Projects with Python - StampedeCon Big Data Conference 2017
End-to-end Big Data Projects with Python - StampedeCon Big Data Conference 2017End-to-end Big Data Projects with Python - StampedeCon Big Data Conference 2017
End-to-end Big Data Projects with Python - StampedeCon Big Data Conference 2017
StampedeCon
 
Beyond php - it's not (just) about the code
Beyond php - it's not (just) about the codeBeyond php - it's not (just) about the code
Beyond php - it's not (just) about the code
Wim Godden
 
Beyond php - it's not (just) about the code
Beyond php - it's not (just) about the codeBeyond php - it's not (just) about the code
Beyond php - it's not (just) about the code
Wim Godden
 
PPT on Data Science Using Python
PPT on Data Science Using PythonPPT on Data Science Using Python
PPT on Data Science Using Python
NishantKumar1179
 
Beyond PHP - it's not (just) about the code
Beyond PHP - it's not (just) about the codeBeyond PHP - it's not (just) about the code
Beyond PHP - it's not (just) about the code
Wim Godden
 
A Few of My Favorite (Python) Things
A Few of My Favorite (Python) ThingsA Few of My Favorite (Python) Things
A Few of My Favorite (Python) Things
Michael Pirnat
 
A Map of the PyData Stack
A Map of the PyData StackA Map of the PyData Stack
A Map of the PyData Stack
Peadar Coyle
 

Similar to Pandas+postgre sql 實作 with code (20)

CONFidence 2015: DTrace + OSX = Fun - Andrzej Dyjak
CONFidence 2015: DTrace + OSX = Fun - Andrzej Dyjak   CONFidence 2015: DTrace + OSX = Fun - Andrzej Dyjak
CONFidence 2015: DTrace + OSX = Fun - Andrzej Dyjak
 
Beyond PHP - It's not (just) about the code
Beyond PHP - It's not (just) about the codeBeyond PHP - It's not (just) about the code
Beyond PHP - It's not (just) about the code
 
r2con 2017 r2cLEMENCy
r2con 2017 r2cLEMENCyr2con 2017 r2cLEMENCy
r2con 2017 r2cLEMENCy
 
Beyond php - it's not (just) about the code
Beyond php - it's not (just) about the codeBeyond php - it's not (just) about the code
Beyond php - it's not (just) about the code
 
Business Dashboards using Bonobo ETL, Grafana and Apache Airflow
Business Dashboards using Bonobo ETL, Grafana and Apache AirflowBusiness Dashboards using Bonobo ETL, Grafana and Apache Airflow
Business Dashboards using Bonobo ETL, Grafana and Apache Airflow
 
A miało być tak... bez wycieków
A miało być tak... bez wyciekówA miało być tak... bez wycieków
A miało być tak... bez wycieków
 
All I know about rsc.io/c2go
All I know about rsc.io/c2goAll I know about rsc.io/c2go
All I know about rsc.io/c2go
 
DSD-INT 2018 Work with iMOD MODFLOW models in Python - Visser Bootsma
DSD-INT 2018 Work with iMOD MODFLOW models in Python - Visser BootsmaDSD-INT 2018 Work with iMOD MODFLOW models in Python - Visser Bootsma
DSD-INT 2018 Work with iMOD MODFLOW models in Python - Visser Bootsma
 
pa-pe-pi-po-pure Python Text Processing
pa-pe-pi-po-pure Python Text Processingpa-pe-pi-po-pure Python Text Processing
pa-pe-pi-po-pure Python Text Processing
 
Beyond php - it's not (just) about the code
Beyond php - it's not (just) about the codeBeyond php - it's not (just) about the code
Beyond php - it's not (just) about the code
 
Learning Dtrace
Learning DtraceLearning Dtrace
Learning Dtrace
 
Python for data science by www.dmdiploma.com
Python for data science by www.dmdiploma.comPython for data science by www.dmdiploma.com
Python for data science by www.dmdiploma.com
 
Python Software Testing in Kytos.io
Python Software Testing in Kytos.ioPython Software Testing in Kytos.io
Python Software Testing in Kytos.io
 
End-to-end Big Data Projects with Python - StampedeCon Big Data Conference 2017
End-to-end Big Data Projects with Python - StampedeCon Big Data Conference 2017End-to-end Big Data Projects with Python - StampedeCon Big Data Conference 2017
End-to-end Big Data Projects with Python - StampedeCon Big Data Conference 2017
 
Beyond php - it's not (just) about the code
Beyond php - it's not (just) about the codeBeyond php - it's not (just) about the code
Beyond php - it's not (just) about the code
 
Beyond php - it's not (just) about the code
Beyond php - it's not (just) about the codeBeyond php - it's not (just) about the code
Beyond php - it's not (just) about the code
 
PPT on Data Science Using Python
PPT on Data Science Using PythonPPT on Data Science Using Python
PPT on Data Science Using Python
 
Beyond PHP - it's not (just) about the code
Beyond PHP - it's not (just) about the codeBeyond PHP - it's not (just) about the code
Beyond PHP - it's not (just) about the code
 
A Few of My Favorite (Python) Things
A Few of My Favorite (Python) ThingsA Few of My Favorite (Python) Things
A Few of My Favorite (Python) Things
 
A Map of the PyData Stack
A Map of the PyData StackA Map of the PyData Stack
A Map of the PyData Stack
 

More from Tim Hong

網頁爬蟲入門 Python web crawler at 淡江大學 20170930
網頁爬蟲入門 Python web crawler at 淡江大學 20170930網頁爬蟲入門 Python web crawler at 淡江大學 20170930
網頁爬蟲入門 Python web crawler at 淡江大學 20170930
Tim Hong
 
資料分析101
資料分析101資料分析101
資料分析101
Tim Hong
 
建築與都市的黏著劑:開放資料 (淡江建築 2016.10.9)
建築與都市的黏著劑:開放資料 (淡江建築 2016.10.9)建築與都市的黏著劑:開放資料 (淡江建築 2016.10.9)
建築與都市的黏著劑:開放資料 (淡江建築 2016.10.9)
Tim Hong
 
AIC x IBM 機器人開發工作營 機構報告
AIC x IBM 機器人開發工作營 機構報告AIC x IBM 機器人開發工作營 機構報告
AIC x IBM 機器人開發工作營 機構報告
Tim Hong
 
AIC x IBM 機器人開發工作營 概念簡報
AIC x IBM 機器人開發工作營 概念簡報AIC x IBM 機器人開發工作營 概念簡報
AIC x IBM 機器人開發工作營 概念簡報
Tim Hong
 
DSP Company Profile
DSP Company ProfileDSP Company Profile
DSP Company Profile
Tim Hong
 
2015 東海 概念工作坊
2015 東海 概念工作坊2015 東海 概念工作坊
2015 東海 概念工作坊
Tim Hong
 
Hacker X Maker 2015
Hacker X Maker 2015Hacker X Maker 2015
Hacker X Maker 2015
Tim Hong
 
APP 概念工作坊
APP 概念工作坊APP 概念工作坊
APP 概念工作坊
Tim Hong
 
Spark !! 人人可上流 支持開放公共空間
Spark !! 人人可上流 支持開放公共空間Spark !! 人人可上流 支持開放公共空間
Spark !! 人人可上流 支持開放公共空間
Tim Hong
 
城市x程式0.80
城市x程式0.80城市x程式0.80
城市x程式0.80
Tim Hong
 
Smart gov conference
Smart gov conferenceSmart gov conference
Smart gov conferenceTim Hong
 
DSP Wellcome
DSP WellcomeDSP Wellcome
DSP WellcomeTim Hong
 
Cft data weekend #3
Cft data weekend #3Cft data weekend #3
Cft data weekend #3Tim Hong
 
City dashboard
City dashboardCity dashboard
City dashboard
Tim Hong
 
2012 twdw app_04
2012 twdw app_042012 twdw app_04
2012 twdw app_04
Tim Hong
 

More from Tim Hong (16)

網頁爬蟲入門 Python web crawler at 淡江大學 20170930
網頁爬蟲入門 Python web crawler at 淡江大學 20170930網頁爬蟲入門 Python web crawler at 淡江大學 20170930
網頁爬蟲入門 Python web crawler at 淡江大學 20170930
 
資料分析101
資料分析101資料分析101
資料分析101
 
建築與都市的黏著劑:開放資料 (淡江建築 2016.10.9)
建築與都市的黏著劑:開放資料 (淡江建築 2016.10.9)建築與都市的黏著劑:開放資料 (淡江建築 2016.10.9)
建築與都市的黏著劑:開放資料 (淡江建築 2016.10.9)
 
AIC x IBM 機器人開發工作營 機構報告
AIC x IBM 機器人開發工作營 機構報告AIC x IBM 機器人開發工作營 機構報告
AIC x IBM 機器人開發工作營 機構報告
 
AIC x IBM 機器人開發工作營 概念簡報
AIC x IBM 機器人開發工作營 概念簡報AIC x IBM 機器人開發工作營 概念簡報
AIC x IBM 機器人開發工作營 概念簡報
 
DSP Company Profile
DSP Company ProfileDSP Company Profile
DSP Company Profile
 
2015 東海 概念工作坊
2015 東海 概念工作坊2015 東海 概念工作坊
2015 東海 概念工作坊
 
Hacker X Maker 2015
Hacker X Maker 2015Hacker X Maker 2015
Hacker X Maker 2015
 
APP 概念工作坊
APP 概念工作坊APP 概念工作坊
APP 概念工作坊
 
Spark !! 人人可上流 支持開放公共空間
Spark !! 人人可上流 支持開放公共空間Spark !! 人人可上流 支持開放公共空間
Spark !! 人人可上流 支持開放公共空間
 
城市x程式0.80
城市x程式0.80城市x程式0.80
城市x程式0.80
 
Smart gov conference
Smart gov conferenceSmart gov conference
Smart gov conference
 
DSP Wellcome
DSP WellcomeDSP Wellcome
DSP Wellcome
 
Cft data weekend #3
Cft data weekend #3Cft data weekend #3
Cft data weekend #3
 
City dashboard
City dashboardCity dashboard
City dashboard
 
2012 twdw app_04
2012 twdw app_042012 twdw app_04
2012 twdw app_04
 

Recently uploaded

Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
Kari Kakkonen
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
danishmna97
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
Pierluigi Pugliese
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
Neo4j
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
SOFTTECHHUB
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
Neo4j
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
Zilliz
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
Matthew Sinclair
 
Large Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial ApplicationsLarge Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial Applications
Rohit Gautam
 
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex ProofszkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
Alex Pruden
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
sonjaschweigert1
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
Alpen-Adria-Universität
 
Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
Kumud Singh
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Nexer Digital
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
Uni Systems S.M.S.A.
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 

Recently uploaded (20)

Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
 
Large Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial ApplicationsLarge Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial Applications
 
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex ProofszkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
 
Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 

Pandas+postgre sql 實作 with code

  • 1. From Data to Display DSP E1 Tim Hong
  • 3.
  • 5.
  • 7.
  • 9.
  • 10.
  • 18. IPython Notebook Hello World File naming Just like google Doc.
  • 20. IPython Notebook Now you try (1+12)*15
  • 21. IPython Notebook Now you try (1+12)*15
  • 23. IPython Notebook Now you try “Sin ”?????!!!!!
  • 24. IPython Notebook Now you try “sin” and try “cos”
  • 25.
  • 27. CSV
  • 28.
  • 29. Ubike iid ItemId sv Ex.“12” sd yyyyMMddhhmmss vtyp Ex.“1”. sno sna sip IP tot Ex.“38” sbi Ex.“23” sarea EX.“ ” mday yyyyMMddhhmmss EX.“20120426132314” lat Ex.“25.0408388889” lng 我 Ex.“121.567894444” ar EX.“ 2 235 ” sareaen EX.“Xinyi Dist.” snaen nbcnt EX,“2” bemp EX,“12”
  • 30.
  • 32.
  • 33.
  • 34. broken_df = pd.read_csv(' / / ubikeCSV.csv', encoding='latin1', parse_dates=['id'], dayfirst=True, index_col='id')
  • 35.
  • 36.
  • 38.
  • 42. How to do it?
  • 44.
  • 45.
  • 46.
  • 47.
  • 53. iPython Notebook SQL Select ___ , ____ From ____ Where ..... 了 在你
  • 55.
  • 56.
  • 57. In [14]: # Import all libraries needed for the tutorial # General syntax to import specific functions in a library: ##from (library) import (specific library function) from pandas import DataFrame, read_csv # General syntax to import a library but no functions: ##import (library) as (give the library a nickname/alias) import matplotlib.pyplot as plt import pandas as pd #this is how I usually import pandas import sys #only needed to determine Python version number # Enable inline plotting %matplotlib inline %pylab inline # Must get this or you will get # NameError: name 'figsize' is not defined import matplotlib.pylab pd.set_option('display.mpl_style', 'default') # Make the graphs a bit prettier figsize(15, 5) print 'Python version ' + sys.version print 'Pandas version ' + pd.__version__ sql In [15]: import pandas.io.sql import psycopg2 conn = psycopg2.connect(user='lab') cur = conn.cursor() print 0 ... In [16]: # conn.close() Populating the interactive namespace from numpy and matplotlib Python version 2.7.6 (default, Mar 22 2014, 22:59:56) [GCC 4.8.2] Pandas version 0.16.0
  • 58. In [17]: try: cur = conn.cursor() cur.execute('SELECT 1') except psycopg2.OperationalError: pass print conn.closed # 2 In [18]: # query db sql = """ select * from ubike limit 3 """ ubike_df = pandas.io.sql.read_sql(sql, conn) ubike_df.head() # try In [19]: ubike_df.columns 0 Out[18]: when_ts where_pt code name area_name space_num avg_bike_num max_bike_ 0 2014- 12-08 15:00:00 (25.041,121.556945) 2 (2 ) 48 24.000 27 1 2014- 12-08 15:00:00 (25.037797,121.565169) 3 40 10.333 13 2 2014- 12-08 15:00:00 (25.036036,121.562325) 4 60 39.333 40 Out[19]: Index([u'when_ts', u'where_pt', u'code', u'name', u'area_name', u' space_num', u'avg_bike_num', u'max_bike_num', u'min_bike_num', u'b ike_num_std', u'avg_space_num', u'max_space_num', u'min_space_num' , u'space_num_std'], dtype='object')
  • 59. In [20]: ubike_df.dtypes In [31]: # query db sql = """ select * from ubike """ all = pandas.io.sql.read_sql(sql, conn) In [33]: all['avg_bike_num'] .plot(legend=False) Out[20]: when_ts datetime64[ns] where_pt object code object name object area_name object space_num int64 avg_bike_num float64 max_bike_num int64 min_bike_num int64 bike_num_std float64 avg_space_num float64 max_space_num int64 min_space_num int64 space_num_std float64 dtype: object Out[33]: <matplotlib.axes._subplots.AxesSubplot at 0x7f373b908310>
  • 60. when_ts datetime64[ns] u' ', u' ', where_pt object u' ', u' ', code object u' ', name object u' ', area_name object u' ', space_num int64 u' ', avg_bike_num float64 u' ', max_bike_num int64 u' ', min_bike_num int64 u' ', bike_num_std float64 u' ', avg_space_num float64 u' ', max_space_num int64 u' ', min_space_num int64 u' ', space_num_std float64 u' ' In [21]: # query db sql = """ select * from ubike where name = ' ' and (when_ts BETWEEN '2014-12-08' AND '2014-12-09') order by when_ts; """ df = pandas.io.sql.read_sql(sql, conn) len(df) In [22]: df Out[21]: 10
  • 61. pandas or python Out[22]: when_ts where_pt code name area_name space_num avg_bike_num max_bike_ 0 2014- 12-08 15:00:00 (25.049845,121.571885) 15 60 38.667 39 1 2014- 12-08 16:00:00 (25.049845,121.571885) 15 60 40.867 44 2 2014- 12-08 17:00:00 (25.049845,121.571885) 15 60 44.000 48 3 2014- 12-08 18:00:00 (25.049845,121.571885) 15 60 39.933 49 4 2014- 12-08 19:00:00 (25.049845,121.571885) 15 60 26.400 29 5 2014- 12-08 20:00:00 (25.049845,121.571885) 15 60 30.000 31 6 2014- 12-08 21:00:00 (25.049845,121.571885) 15 60 32.667 34 7 2014- 12-08 22:00:00 (25.049845,121.571885) 15 60 35.133 39 8 2014- 12-08 23:00:00 (25.049845,121.571885) 15 60 35.000 39 9 2014- 12-09 00:00:00 (25.049845,121.571885) 15 60 28.643 33
  • 62. In [23]: df[:3] # :3, :10, 5:10, 'name' In [24]: df[['when_ts','avg_bike_num']] Out[23]: when_ts where_pt code name area_name space_num avg_bike_num max_bike_ 0 2014- 12-08 15:00:00 (25.049845,121.571885) 15 60 38.667 39 1 2014- 12-08 16:00:00 (25.049845,121.571885) 15 60 40.867 44 2 2014- 12-08 17:00:00 (25.049845,121.571885) 15 60 44.000 48 Out[24]: when_ts avg_bike_num 0 2014-12-08 15:00:00 38.667 1 2014-12-08 16:00:00 40.867 2 2014-12-08 17:00:00 44.000 3 2014-12-08 18:00:00 39.933 4 2014-12-08 19:00:00 26.400 5 2014-12-08 20:00:00 30.000 6 2014-12-08 21:00:00 32.667 7 2014-12-08 22:00:00 35.133 8 2014-12-08 23:00:00 35.000 9 2014-12-09 00:00:00 28.643
  • 63. In [25]: df[['when_ts','avg_bike_num']].plot() In [26]: from matplotlib.font_manager import FontProperties, findfont fp = FontProperties(family='monospace', style='normal', variant='normal', weight='normal', stretch='normal', size='medium') font = findfont(fp) In [27]: df[['when_ts','avg_bike_num']].plot() Out[25]: <matplotlib.axes._subplots.AxesSubplot at 0x7f3755f87110> Out[27]: <matplotlib.axes._subplots.AxesSubplot at 0x7f37560d2050>
  • 64. In [28]: df[['when_ts','avg_bike_num']].plot(kind='kde') In [16]: # query db sql = """ select * from ubike where name = ' 'and (when_ts BETWEEN '2014-12-08' AND '2014-12-09') order by when_ts; """ ponit2_df = pandas.io.sql.read_sql(sql, conn) len(ponit2_df) In [17]: ponit2_df[:1] Out[28]: <matplotlib.axes._subplots.AxesSubplot at 0x7f3756096910> Out[16]: 10 Out[17]: when_ts where_pt code name area_name space_num avg_bike_num max_bike_ 0 2014- 12-08 15:00:00 (25.048268,121.552278) 18 38 13.5 15
  • 65. In [18]: df['avg_bike_num'].plot() ponit2_df['avg_bike_num'].plot() In [19]: ponit2_df.plot(x='when_ts', y='avg_bike_num') Out[18]: <matplotlib.axes._subplots.AxesSubplot at 0x7fba9e2b9490> Out[19]: <matplotlib.axes._subplots.AxesSubplot at 0x7fba9e167290>
  • 66. In [20]: ponit2_df.plot(x='when_ts', y='avg_bike_num'); df.plot(x='when_ts', y='avg_bike_num') In [21]: df[['max_space_num','min_space_num']].plot(kind='area'); Out[20]: <matplotlib.axes._subplots.AxesSubplot at 0x7fba9dfa1810>
  • 67. In [22]: df[['space_num','max_space_num','min_space_num']].plot(kind='area', stacked=Fa lse); Table In [112]: fig, ax = plt.subplots(1, 1) ax.get_xaxis().set_visible(False) df[['space_num','max_space_num','min_space_num']].plot(table=True, ax=ax) Out[112]: <matplotlib.axes._subplots.AxesSubplot at 0x7fba80d96490>
  • 68. In [121]: from pandas.tools.plotting import table fig, ax = plt.subplots(1, 1) table(ax, np.round(df[['space_num','max_space_num','min_space_num']].describe( ), 2), loc='upper left', colWidths=[0.1, 0.1, 0.1]) df[['space_num','max_space_num','min_space_num']].plot(table=True, ax=ax) sql In [124]: # query db sql = """ select a.when_ts as time , a.avg_bike_num as point_A, b.avg_bike_num as point_B from ubike a inner join ubike b on a.when_ts = b.when_ts and (a.when_ts BETWEEN '2014-12-08' AND '2014-12-09') and (a.name = ' ' and b.name = ' '); """ PointJoin = pandas.io.sql.read_sql(sql, conn) len(PointJoin) Out[121]: <matplotlib.axes._subplots.AxesSubplot at 0x7fba80348ad0> Out[124]: 10
  • 69. In [126]: PointJoin In [127]: fig, ax = plt.subplots(1, 1) ax.get_xaxis().set_visible(False) PointJoin[['point_a','point_b']].plot(table=True, ax=ax) Bar In [ ]: ## Now you try loc='upper left' colWidths=[0.1, 0.1, 0.1] remove tabl e=True Out[126]: time point_a point_b 0 2014-12-08 15:00:00 13.500 38.667 1 2014-12-08 16:00:00 12.733 40.867 2 2014-12-08 17:00:00 9.615 44.000 3 2014-12-08 18:00:00 12.267 39.933 4 2014-12-08 19:00:00 13.933 26.400 5 2014-12-08 20:00:00 17.200 30.000 6 2014-12-08 21:00:00 10.667 32.667 7 2014-12-08 22:00:00 4.400 35.133 8 2014-12-08 23:00:00 3.867 35.000 9 2014-12-09 00:00:00 0.929 28.643 Out[127]: <matplotlib.axes._subplots.AxesSubplot at 0x7fba8000e150>
  • 72. In [29]: ponit2_df[['max_space_num','min_space_num']].plot(kind='hist') In [30]: ponit2_df[['max_space_num','min_space_num']].hist() In [31]: ponit2_df[:1] Out[29]: <matplotlib.axes._subplots.AxesSubplot at 0x7fba9dab6c90> Out[30]: array([[<matplotlib.axes._subplots.AxesSubplot object at 0x7fba9da 71d90>, <matplotlib.axes._subplots.AxesSubplot object at 0x7fba9da ece90>]], dtype=object) Out[31]: when_ts where_pt code name area_name space_num avg_bike_num max_bike_ 0 2014- 12-08 15:00:00 (25.048268,121.552278) 18 38 13.5 15
  • 73. In [32]: # query db sql = """ select name from ubike where area_name like '% %' group by name """ pointA = pandas.io.sql.read_sql(sql, conn) len(pointA) In [33]: pointA Out[32]: 14 Out[33]: name 0 1 2 (2 ) 3 4 5 (2 ) 6 7 8 9 10 11 (2 ) 12 13
  • 74. In [90]: # query db sql = """ select * from ubike where name = ' ' and (when_ts BETWEEN '2014-12 -25' AND '2014-12-31') """ pointA = pandas.io.sql.read_sql(sql, conn) len(pointA) In [35]: pointA[:1] In [91]: # query db sql = """ select * from tpweather where name = ' ' and (when_ts BETWEEN '2014-1 2-25' AND '2014-12-31'); """ weaterA = pandas.io.sql.read_sql(sql, conn) len(weaterA) In [37]: weaterA[:1] Out[90]: 145 Out[35]: when_ts where_pt code name area_name space_num avg_bike_num max_bike_ 0 2014- 12-08 15:00:00 (25.116325,121.534136) 123 44 31 33 Out[91]: 145 Out[37]: when_ts where_pt name temp max_temp min_temp hum_pct pressure win 0 2014- 12-19 (25.1180133,121.5373439) 17.3889 17.4 16.9 76 1022.38 2.7
  • 75. sub select In [92]: # query db sql = """ select * from tpweather where when_ts in (select when_ts from ubike where name = ' ' order by when_ts ) and name = ' ' and (when_ts BETWEEN '2014-12-25' AND '2014-12-31' ) ord er by when_ts """ weatherA = pandas.io.sql.read_sql(sql, conn) len(weatherA) In [93]: len(pointA) In [94]: len(weatherA) Out[92]: 145 Out[93]: 145 Out[94]: 145
  • 76. In [95]: pointA.plot(x='when_ts', y='avg_bike_num') weatherA.plot(x='when_ts', y='rainfall') Out[95]: <matplotlib.axes._subplots.AxesSubplot at 0x7fba9c0f5890>
  • 77. In [96]: pointA['avg_bike_num'].plot() weatherA['rainfall'].plot() inner join In [ ]: Out[96]: <matplotlib.axes._subplots.AxesSubplot at 0x7fba977c5e50>
  • 78. • —— 不了Sam Redwine你 • Software and cathedrals are much the same – first we build them, then we pray. (Sam Redwine)