In [2]:
from sqlalchemy import create_engine
import pandas as pd

Crear el "engine" pasando la dirección de la db


In [9]:
engine = create_engine('postgresql://celia@localhost:5432/mytestdb')
engine


Out[9]:
Engine(postgresql://celia@localhost:5432/mytestdb)

In [20]:
df_customer.to_json('/tmp/test.json')

In [6]:
json_df  = pd.read_json('/home/celia/Downloads/MOCK_DATA.json')
json_df


Out[6]:
bday email first_name gender id last_name
0 7/13/2016 gdiaz0@gizmodo.com Gloria Female 1 Diaz
1 10/27/1993 pgarrett1@diigo.com Peter Male 2 Garrett
2 5/17/2004 bbradley2@themeforest.net Betty Female 3 Bradley
3 7/6/2013 rmorales3@shinystat.com Russell Male 4 Morales
4 11/9/1995 rsims4@sina.com.cn Randy Male 5 Sims
5 10/25/2004 jcruz5@wikia.com Jerry Male 6 Cruz
6 3/23/2009 mmedina6@seattletimes.com Michelle Female 7 Medina
7 4/5/2002 gellis7@hhs.gov Gregory Male 8 Ellis
8 5/29/2014 blawson8@blog.com Brenda Female 9 Lawson
9 10/30/2006 lkelly9@pcworld.com Lawrence Male 10 Kelly
10 1/30/2010 jalvareza@fc2.com Jason Male 11 Alvarez
11 7/16/2003 hmarshallb@nps.gov Harold Male 12 Marshall
12 10/31/1990 scampbellc@theguardian.com Steven Male 13 Campbell
13 5/16/2013 llongd@ask.com Lois Female 14 Long
14 12/8/1997 dhowelle@mit.edu David Male 15 Howell
15 7/11/2013 jwhitef@netlog.com Julie Female 16 White
16 8/30/2010 eknightg@auda.org.au Eric Male 17 Knight
17 11/12/2004 woliverh@imageshack.us Wayne Male 18 Oliver
18 9/16/2006 cperryi@wikia.com Cheryl Female 19 Perry
19 5/21/1991 ssanchezj@jugem.jp Sean Male 20 Sanchez
20 10/2/2008 dwellsk@nytimes.com Donald Male 21 Wells
21 3/21/2013 jhudsonl@github.com Justin Male 22 Hudson
22 11/18/2007 krobinsonm@feedburner.com Keith Male 23 Robinson
23 5/28/1994 gwilliamsonn@liveinternet.ru Gloria Female 24 Williamson
24 7/26/1995 jpierceo@forbes.com Jimmy Male 25 Pierce
25 9/5/2010 drosep@omniture.com Daniel Male 26 Rose
26 1/17/1993 ajenkinsq@hc360.com Adam Male 27 Jenkins
27 9/4/1994 jgardnerr@theguardian.com Jacqueline Female 28 Gardner
28 8/24/1990 amorenos@nhs.uk Arthur Male 29 Moreno
29 6/24/2009 flewist@facebook.com Frank Male 30 Lewis
... ... ... ... ... ... ...
970 9/10/2013 acrawfordqy@acquirethisname.com Amanda Female 971 Crawford
971 3/17/2002 rwilliamsqz@zdnet.com Ruth Female 972 Williams
972 4/15/2001 cwilliamsr0@devhub.com Christine Female 973 Williams
973 3/29/2014 jelliottr1@businessinsider.com Jessica Female 974 Elliott
974 1/31/2012 aarnoldr2@lycos.com Alan Male 975 Arnold
975 9/5/2007 fcarterr3@technorati.com Frances Female 976 Carter
976 3/30/2007 jhayesr4@opensource.org Jerry Male 977 Hayes
977 2/9/2014 rpricer5@cpanel.net Rose Female 978 Price
978 2/14/2015 amorrisonr6@nba.com Amy Female 979 Morrison
979 4/24/2011 erogersr7@csmonitor.com Earl Male 980 Rogers
980 2/26/1991 tgarciar8@oracle.com Tina Female 981 Garcia
981 7/11/2004 hweaverr9@newyorker.com Howard Male 982 Weaver
982 10/7/2012 sdavisra@ted.com Shirley Female 983 Davis
983 11/3/1993 lroserb@mayoclinic.com Lawrence Male 984 Rose
984 12/31/2005 mbryantrc@geocities.com Melissa Female 985 Bryant
985 5/23/2009 wgomezrd@nymag.com Walter Male 986 Gomez
986 1/25/2015 kgibsonre@usa.gov Karen Female 987 Gibson
987 2/25/2000 kpricerf@loc.gov Karen Female 988 Price
988 5/26/2013 psullivanrg@cbc.ca Paula Female 989 Sullivan
989 7/9/2012 jmorrisonrh@123-reg.co.uk Judy Female 990 Morrison
990 8/29/2000 jolsonri@nbcnews.com Jane Female 991 Olson
991 2/6/2012 dsimsrj@ox.ac.uk Doris Female 992 Sims
992 3/9/1998 aelliottrk@umich.edu Amanda Female 993 Elliott
993 2/13/1999 bmendozarl@cdc.gov Brenda Female 994 Mendoza
994 8/8/2016 isimsrm@fotki.com Irene Female 995 Sims
995 6/20/2000 jtaylorrn@usgs.gov Justin Male 996 Taylor
996 6/7/2000 phudsonro@sakura.ne.jp Paula Female 997 Hudson
997 11/22/1997 scampbellrp@wix.com Sandra Female 998 Campbell
998 6/22/1996 vpaynerq@fda.gov Victor Male 999 Payne
999 11/9/1995 rwarrenrr@mlb.com Richard Male 1000 Warren

1000 rows × 6 columns


In [10]:
json_df.to_sql('Customer', engine, index=None)

Hacer la query especificando el "engine" que se desea usar


In [15]:
df_customer = pd.read_sql_query('select email, bday, first_name from "Customer"',con=engine)
df_customer


Out[15]:
email bday first_name
0 gdiaz0@gizmodo.com 7/13/2016 Gloria
1 pgarrett1@diigo.com 10/27/1993 Peter
2 bbradley2@themeforest.net 5/17/2004 Betty
3 rmorales3@shinystat.com 7/6/2013 Russell
4 rsims4@sina.com.cn 11/9/1995 Randy
5 jcruz5@wikia.com 10/25/2004 Jerry
6 mmedina6@seattletimes.com 3/23/2009 Michelle
7 gellis7@hhs.gov 4/5/2002 Gregory
8 blawson8@blog.com 5/29/2014 Brenda
9 lkelly9@pcworld.com 10/30/2006 Lawrence
10 jalvareza@fc2.com 1/30/2010 Jason
11 hmarshallb@nps.gov 7/16/2003 Harold
12 scampbellc@theguardian.com 10/31/1990 Steven
13 llongd@ask.com 5/16/2013 Lois
14 dhowelle@mit.edu 12/8/1997 David
15 jwhitef@netlog.com 7/11/2013 Julie
16 eknightg@auda.org.au 8/30/2010 Eric
17 woliverh@imageshack.us 11/12/2004 Wayne
18 cperryi@wikia.com 9/16/2006 Cheryl
19 ssanchezj@jugem.jp 5/21/1991 Sean
20 dwellsk@nytimes.com 10/2/2008 Donald
21 jhudsonl@github.com 3/21/2013 Justin
22 krobinsonm@feedburner.com 11/18/2007 Keith
23 gwilliamsonn@liveinternet.ru 5/28/1994 Gloria
24 jpierceo@forbes.com 7/26/1995 Jimmy
25 drosep@omniture.com 9/5/2010 Daniel
26 ajenkinsq@hc360.com 1/17/1993 Adam
27 jgardnerr@theguardian.com 9/4/1994 Jacqueline
28 amorenos@nhs.uk 8/24/1990 Arthur
29 flewist@facebook.com 6/24/2009 Frank
... ... ... ...
970 acrawfordqy@acquirethisname.com 9/10/2013 Amanda
971 rwilliamsqz@zdnet.com 3/17/2002 Ruth
972 cwilliamsr0@devhub.com 4/15/2001 Christine
973 jelliottr1@businessinsider.com 3/29/2014 Jessica
974 aarnoldr2@lycos.com 1/31/2012 Alan
975 fcarterr3@technorati.com 9/5/2007 Frances
976 jhayesr4@opensource.org 3/30/2007 Jerry
977 rpricer5@cpanel.net 2/9/2014 Rose
978 amorrisonr6@nba.com 2/14/2015 Amy
979 erogersr7@csmonitor.com 4/24/2011 Earl
980 tgarciar8@oracle.com 2/26/1991 Tina
981 hweaverr9@newyorker.com 7/11/2004 Howard
982 sdavisra@ted.com 10/7/2012 Shirley
983 lroserb@mayoclinic.com 11/3/1993 Lawrence
984 mbryantrc@geocities.com 12/31/2005 Melissa
985 wgomezrd@nymag.com 5/23/2009 Walter
986 kgibsonre@usa.gov 1/25/2015 Karen
987 kpricerf@loc.gov 2/25/2000 Karen
988 psullivanrg@cbc.ca 5/26/2013 Paula
989 jmorrisonrh@123-reg.co.uk 7/9/2012 Judy
990 jolsonri@nbcnews.com 8/29/2000 Jane
991 dsimsrj@ox.ac.uk 2/6/2012 Doris
992 aelliottrk@umich.edu 3/9/1998 Amanda
993 bmendozarl@cdc.gov 2/13/1999 Brenda
994 isimsrm@fotki.com 8/8/2016 Irene
995 jtaylorrn@usgs.gov 6/20/2000 Justin
996 phudsonro@sakura.ne.jp 6/7/2000 Paula
997 scampbellrp@wix.com 11/22/1997 Sandra
998 vpaynerq@fda.gov 6/22/1996 Victor
999 rwarrenrr@mlb.com 11/9/1995 Richard

1000 rows × 3 columns


In [16]:
df_customer.info()


<class 'pandas.core.frame.DataFrame'>
Int64Index: 1000 entries, 0 to 999
Data columns (total 3 columns):
email         1000 non-null object
bday          1000 non-null object
first_name    1000 non-null object
dtypes: object(3)
memory usage: 31.2+ KB

In [18]:
df_customer.describe()


Out[18]:
email bday first_name
count 1000 1000 1000
unique 1000 953 199
top bbellbc@gmpg.org 8/2/2008 James
freq 1 2 12

Link a Pandas NB para ver join, merge, append, etc

Agregando un nuevo registro a nuestra tabla "Customer" con pandas


In [19]:
json_df.columns


Out[19]:
Index([u'bday', u'email', u'first_name', u'gender', u'id', u'last_name'], dtype='object')

In [20]:
new_df = pd.DataFrame([[pd.datetime(1990, 3, 19),'cintas.celia@gmail.com', 'celia', 'Female', 10000, 'cintas']], columns=json_df.columns)
new_df


Out[20]:
bday email first_name gender id last_name
0 1990-03-19 cintas.celia@gmail.com celia Female 10000 cintas

In [21]:
new_df.to_sql('Customer', engine, if_exists='append', index=None)

In [32]:
df_customer = pd.read_sql_query('select * from "Customer" WHERE id = 10000;', con=engine)
df_customer


Out[32]:
bday email first_name gender id last_name
0 1990-03-19 00:00:00 cintas.celia@gmail.com celia Female 10000 cintas

Agregando una nueva tabla a nuestra db desde pandas


In [33]:
new_table = pd.DataFrame([], columns=['WineCode', 'Type', 'Vintage'])
new_table


Out[33]:
WineCode Type Vintage

In [ ]:
new_table.to_sql('Wine', engine, index=None)

Ahora hagamos lo mismo con sqlalchemy


In [36]:
from sqlalchemy import MetaData, types
from sqlalchemy import Table, Column

In [37]:
metadata = MetaData()

In [38]:
time = Table('Time', metadata,
    Column('TimeCode', types.Integer, primary_key=True),
    Column('Date', types.DateTime, nullable=False),
 )

In [39]:
metadata.create_all(engine)

Agregando elementos a Wine con pandas


In [40]:
data = [[1, 'White', 2000],
        [2, 'red', 2015],
        [3, 'rose', 2014]]

In [42]:
new_df = pd.DataFrame(data, columns=new_table.columns)

In [43]:
new_df.to_sql('Wine', engine, if_exists='append', index=None)

In [44]:
df_wine = pd.read_sql_query('select * from "Wine"',con=engine)
df_wine


Out[44]:
WineCode Type Vintage
0 1 White 2000
1 2 red 2015
2 3 rose 2014

In [45]:
data = [[1, 'White', pd.datetime(2000, 10, 10)],
        [2, 'red', pd.datetime(2010, 9, 9)],
        [3, 'rose', pd.datetime(2011, 9, 9)]]

In [46]:
new_df = pd.DataFrame(data, columns=df_wine.columns)
new_df['Vintage']


Out[46]:
0   2000-10-10
1   2010-09-09
2   2011-09-09
Name: Vintage, dtype: datetime64[ns]

In [56]:
new_df.to_json('/tmp/lero.json', date_unit='ns')

In [57]:
json_demo = pd.read_json('/tmp/lero.json')
json_demo


Out[57]:
Type Vintage WineCode
0 White 971136000000000000 1
1 red 1283990400000000000 2
2 rose 1315526400000000000 3

In [58]:
new_df


Out[58]:
WineCode Type Vintage
0 1 White 2000-10-10
1 2 red 2010-09-09
2 3 rose 2011-09-09

In [59]:
json_demo['Vintage'] = pd.to_datetime(json_demo['Vintage'], unit='ns')

In [60]:
json_demo


Out[60]:
Type Vintage WineCode
0 White 2000-10-10 1
1 red 2010-09-09 2
2 rose 2011-09-09 3

In [61]:
new_df


Out[61]:
WineCode Type Vintage
0 1 White 2000-10-10
1 2 red 2010-09-09
2 3 rose 2011-09-09

In [62]:
json_demo.columns.values


Out[62]:
array([u'Type', u'Vintage', u'WineCode'], dtype=object)

In [63]:
json_demo.values


Out[63]:
array([[u'White', Timestamp('2000-10-10 00:00:00'), 1],
       [u'red', Timestamp('2010-09-09 00:00:00'), 2],
       [u'rose', Timestamp('2011-09-09 00:00:00'), 3]], dtype=object)

In [64]:
pd.merge(json_demo, new_df, on=list(json_demo.columns.values), how='outer')


Out[64]:
Type Vintage WineCode
0 White 2000-10-10 1
1 red 2010-09-09 2
2 rose 2011-09-09 3

In [82]:
pd.Series?

In [65]:
score = pd.Series([10, 9, 8], name='score')
score


Out[65]:
0    10
1     9
2     8
Name: score, dtype: int64

In [66]:
out = pd.concat([json_demo, score], axis=1)
new_row = pd.DataFrame([[4, 'espumeante',pd.datetime(2000,2,2)]], columns=new_df.columns)

In [67]:
append_df = new_df.append(new_row)

In [68]:
append_df.to_sql('Wine', engine, if_exists='append', index=None)