Advertisement
Guest User

Untitled

a guest
Apr 28th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. import pandas as pd
  2. import numpy as np
  3. from IPython.display import display
  4.  
  5. # ------------------
  6. # Print Functions
  7. # ------------------
  8.  
  9. def print_all(df):
  10. '''Print all the full columns of the dataframe'''
  11. with pd.option_context('display.max_columns', None):
  12. print(df)
  13.  
  14. def smart_print_all(df):
  15. '''Print all the full columns of the dataframe'''
  16. with pd.option_context('display.max_columns', None):
  17. if 'msisdn' in df.columns:
  18. df = df.copy()
  19. df['msisdn'] = df['msisdn'].astype(str).str[-4:]
  20. print(df)
  21.  
  22. def nb_print_all(df):
  23. '''Print all the full columns of the dataframe. Jupyter notebook version.'''
  24. with pd.option_context('display.max_columns', None):
  25. display(df)
  26.  
  27. def smart_nb_print_all(df):
  28. '''Print all the full columns of the dataframe. Jupyter notebook version.'''
  29. with pd.option_context('display.max_columns', None):
  30. if 'msisdn' in df.columns:
  31. df = df.copy()
  32. df['msisdn'] = df['msisdn'].astype(str).str[-4:]
  33. display(df)
  34.  
  35.  
  36. # ------------------
  37. # DB Connection
  38. # ------------------
  39.  
  40.  
  41. '''
  42. # db connection credential
  43. import getpass
  44. cred = {
  45. 'server': '',
  46. 'database': '',
  47. 'username': '',
  48. 'password': getpass.getpass()
  49. }
  50.  
  51. '''
  52.  
  53. import pyodbc, os
  54. def get_data(cred,query,start=None,end=None):
  55. server = cred['server']
  56. database=cred['database']
  57. username=cred['username']
  58. password=cred['password']
  59. data_frame = pd.DataFrame();
  60. cnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
  61. data_frame = pd.read_sql(query, cnxn)
  62. cnxn.close()
  63. return data_frame
  64.  
  65. def insert_data(cred, data, table_name):
  66. from sqlalchemy import create_engine
  67. import urllib
  68. server = cred['server']
  69. database=cred['database']
  70. username=cred['username']
  71. password=cred['password']
  72. params = urllib.parse.quote_plus('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+password)
  73. engine = create_engine("mssql+pyodbc:///?odbc_connect=%s" % params)
  74. data.to_sql(name=table_name
  75. ,con=engine
  76. ,index=False
  77. ,if_exists='append'
  78. )
  79. engine.close();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement