IT Learning

実践形式でITのお勉強

「 月別アーカイブ:2020年07月 」 一覧

Bulk insert to Oracle with Python

2020/07/04   -Oracle, Python
 ,

Overview There is an example code to insert data in bulk to Oracle database with python cx_Oracle Environments python 3.7.3Oracle 18c Express Edition Step1 : Creating the table Creating the table ‘oracle_insert’ in the schema ‘USER01’. CREATE TABLE USER01.ORACLE_INSERT( col1 int ,col2 int ,col3 int ) Step2 : Insert in bulk with executemany dataset is the dataset to be insertedMaking a connection to database with cx_OracleInserting in bulk with using cur.executemany() import cx_Oracle dataset =[ [1,2,3] ,[4,5,6] ,[7,8,9] ,[10,11,12] ,[13,14,15] ,[16,17,18] ,[19,20,21] ] HOST = ‘localhost’ PORT = ‘1521’ DB_NAME = ‘xepd1’ USER = ‘user01’ PASSWORD = ‘password01’ SERVICE_NAME = …

【Python】Transforming datetime to date and time with pandas dateframe

2020/07/01   -Python

Original Data This code is to make sample dataframe. import pandas as pd import datetime as dt df = pd.DataFrame([dt.datetime(2020,6,1,0,0,0),dt.datetime(2020,6,2,10,0,0),dt.datetime(2020,6,3,15,0,0)],columns=[’datetimes’])     datetimes 0 2020-06-01 00:00:00 1 2020-06-02 10:00:00 2 2020-06-03 15:00:00 Transforming dataframe to date and time. It can realize to use lambda & apply functions like following. df[’dates’] = df[’datetimes’].apply(lambda x : dt.date(x.year,x.month,x.day)) df[’times’] = df[’datetimes’].apply(lambda x : dt.time(x.hour,x.minute,x.second))      datetimes      dates   times 0 2020-06-01 00:00:00 2020-06-01 00:00:00 1 2020-06-02 10:00:00 2020-06-02 10:00:00 2 2020-06-03 15:00:00 2020-06-03 15:00:00 If you want to get with string, these code is better. df[’dates’] = df[’datetimes’].apply(lambda x : x.strftime(‘%Y/%m/%d’)) df[’times’] = df[’datetimes’].apply(lambda x …