Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pandas as pd
- import numpy as np
- from IPython.display import display
- # ------------------
- # Print Functions
- # ------------------
- def print_all(df):
- '''Print all the full columns of the dataframe'''
- with pd.option_context('display.max_columns', None):
- print(df)
- def smart_print_all(df):
- '''Print all the full columns of the dataframe'''
- with pd.option_context('display.max_columns', None):
- if 'msisdn' in df.columns:
- df = df.copy()
- df['msisdn'] = df['msisdn'].astype(str).str[-4:]
- print(df)
- def nb_print_all(df):
- '''Print all the full columns of the dataframe. Jupyter notebook version.'''
- with pd.option_context('display.max_columns', None):
- display(df)
- def smart_nb_print_all(df):
- '''Print all the full columns of the dataframe. Jupyter notebook version.'''
- with pd.option_context('display.max_columns', None):
- if 'msisdn' in df.columns:
- df = df.copy()
- df['msisdn'] = df['msisdn'].astype(str).str[-4:]
- display(df)
- # ------------------
- # DB Connection
- # ------------------
- '''
- # db connection credential
- import getpass
- cred = {
- 'server': '',
- 'database': '',
- 'username': '',
- 'password': getpass.getpass()
- }
- '''
- import pyodbc, os
- def get_data(cred,query,start=None,end=None):
- server = cred['server']
- database=cred['database']
- username=cred['username']
- password=cred['password']
- data_frame = pd.DataFrame();
- cnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
- data_frame = pd.read_sql(query, cnxn)
- cnxn.close()
- return data_frame
- def insert_data(cred, data, table_name):
- from sqlalchemy import create_engine
- import urllib
- server = cred['server']
- database=cred['database']
- username=cred['username']
- password=cred['password']
- params = urllib.parse.quote_plus('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+password)
- engine = create_engine("mssql+pyodbc:///?odbc_connect=%s" % params)
- data.to_sql(name=table_name
- ,con=engine
- ,index=False
- ,if_exists='append'
- )
- engine.close();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement