Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
Working from home

Aveek aveek22

🏠
Working from home
View GitHub Profile
View cs621_lab_exam.sql
select * from cs621labexam1;
--1
select * from cs621labexam1 where char_length(observer) > 9;
--2
select * from cs621labexam1
where 1=1
and date_part('month', ts) = 01
and date_part('year', ts) = 2019
@aveek22
aveek22 / export_data_from_pandas.py
Created Dec 25, 2020
This file exports data from pandas to a table in PostgreSQL database.
View export_data_from_pandas.py
import pandas as pd
import sqlalchemy
# Create the engine to connect to a PostgreSQL database
engine = sqlalchemy.create_engine('postgresql://aveek:test1234@localhost:5432/sql-shack-demo')
# Read data from the SQL Table
data = pd.read_csv('superstore.csv')
# Print first few rows of the dataframe
View app.py
from flask import Flask
from flask_restful import Api, Resource, reqparse
import pandas as pd
app = Flask(__name__)
api = Api(app)
class Users(Resource):
def get(self):
data = pd.read_csv('users.csv')
View flask_class_users.py
class Users(Resource):
def get(self):
data = pd.read_csv('users.csv')
data = data.to_dict('records')
return {'data' : data}, 200
def post(self):
parser = reqparse.RequestParser()
parser.add_argument('name', required=True)
View flask_run.py
if __name__ == '__main__':
app.run()
View superstore.csv
We can make this file beautiful and searchable if this error is corrected: It looks like row 5 should actually have columns, instead of 5. in line 4.
order,product,qty,amount
1,car,2,5000
2,bikes,1,2000
3,cookies,10,80
4,phone,1,5,300
View serialization_deserialization.py
# imports the JSON library to work in python
import json
# assign the JSON data to a variable
json_data = """{
"data": [
{
"type": "articles",
"id": "1",
"attributes": {
View json_pandas.py
# imports the Pandas library to work in python
import pandas as pd
# assign the JSON data to a variable
json_data = """{
"data": [
{
"type": "articles",
"id": "1",
"attributes": {
View pandas_to_json.py
# imports the Pandas library to work in python
import pandas as pd
# Create the data as a list
data = [['Bob', 30], ['Fred', 35], ['Nick', 40]]
# Load the data as a dataframe
df = pd.DataFrame(data=data, columns=['Name','Age'])
# Covnert the dataframe to JSON
View sample_function.py
import json
def greet():
print('Hello user')
def main():
greet()
if __name__ == '__main__':
main()