Created
February 22, 2020 20:50
-
-
Save jamescalam/9a5008f064516d5fb4177362a03e2594 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import sys | |
| sys.path.insert(0, r'C:\\User\medium\pysqlplus\lib') | |
| import os | |
| from data import Sql | |
| sql = Sql('database123') # initialise the Sql object | |
| directory = r'C:\\User\medium\data\\' # this is where our generic data is stored | |
| file_list = os.listdir(directory) # get a list of all files | |
| for file in file_list: # loop to import files to sql | |
| df = pd.read_csv(directory+file) # read file to dataframe | |
| sql.push_dataframe(df, file[:-4]) | |
| # now we convert our file_list names into the table names we have imported to SQL | |
| table_names = [x[:-4] for x in file_list] | |
| sql.union(table_names, 'generic_jan') # union our files into one new table called 'generic_jan' | |
| sql.drop(table_names) # drop our original tables as we now have full table | |
| # get list of categories in colX, eg ['hr', 'finance', 'tech', 'c_suite'] | |
| sets = list(sql.manual("SELECT colX AS 'category' FROM generic_jan GROUP BY colX", response=True)['category']) | |
| for category in sets: | |
| sql.manual("SELECT * INTO generic_jan_"+category+" FROM generic_jan WHERE colX = '"+category+"'") | |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi toconner9, I've adjusted your code to use pandas dataframe, which is the object type that my code exports the data from your query as, maybe this works for you?
Printing the dataframe isn't always the easiest, so maybe (depending on what you are using it for) it would be easier to save to Excel or csv?
product_data.to_excel('output.xlsx', index=False) # index = False just prevents an index column from being included in your outputor
product_data.to_csv('output.csv', sep='|') # I usually set the delimiter (sep) to |, but can use any characterAlso, there is documentation for using it all here if that helps!