You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
932 B
44 lines
932 B
import pandas as pd
|
|
import psycopg2 as psy
|
|
|
|
|
|
data = pd.read_csv(r'co2_emission.csv')
|
|
df = pd.DataFrame(data)
|
|
df2 = df.drop_duplicates()
|
|
co = None
|
|
|
|
|
|
try :
|
|
co = psy.connect(host='berlin',
|
|
database = 'dbmalanone',
|
|
user = 'malanone',
|
|
password = 'azertyuiop')
|
|
curs=co.cursor()
|
|
|
|
curs.execute('''DROP TABLE IF EXISTS CO_DEUX;''')
|
|
|
|
curs.execute('''
|
|
CREATE TABLE CO_DEUX(
|
|
entity varchar(20) NOT NULL,
|
|
code char(3),
|
|
year numeric(4) NOT NULL,
|
|
emission numeric,
|
|
PRIMARY KEY(entity, year)
|
|
);
|
|
''')
|
|
|
|
for row in df2.intertuples():
|
|
curs.execute(
|
|
'''INSERT INTO CO_DEUX VALUES(%s%s%d%f)''',
|
|
(row.entity, row.code, row.year, row.emission)
|
|
)
|
|
|
|
|
|
curs.execute('''SELECT * FROM CO_DEUX;''')
|
|
|
|
except(Exception, psy.DatabaseError) as error:
|
|
print(error)
|
|
finally:
|
|
if co is not None:
|
|
co.close()
|