Author: tom
-
Flask and JSON
It is possible to use the Flask framework to play with json data. The programme looks like: # import main Flask class and request object from flask import Flask, request import json # create the Flask app app = Flask(__name__) @app.route(‘/json-example’, methods=[‘POST’]) def json_example(): request_data = request.get_json() language = None framework = None python_version =…
-
A webserver from the command line
Python has a module (Flask) that allows to run a Python module from the commmand line. The code looks like: # import main Flask class and request object from flask import Flask, request # create the Flask app app = Flask(__name__) @app.route(‘/form-example’, methods=[‘GET’, ‘POST’]) def form_example(): if request.args.get(‘new’) == ‘NEW’: return ”’ <form method=”POST” action…
-
Use the node.js server as restful app server
One may use the node.js server as a means to get acquainted with restful services. Let us assume this server is installed and started with json-server “C:\Users\tomva\SynologyDrive\python\json server\db.json” –port 8000 We can then see the json database within the browser “http://localhost:8000/extra”. We may add some information by: import requests import json api_url = “http://localhost:8000/extra” todo…
-
Reading a CSV file and translate into dataframe
Below, a script is provided that reads a CSV file and translate the outcome into a dataframe: import csv import pandas as pd with open(r”C:\Users\tomva\SynologyDrive\python\pandas\Incomplete\banking.csv”,”r”) as csv_file: csv_reader = csv.reader(csv_file, delimiter = ‘,’) print(type(csv_reader)) df = pd.DataFrame() i = 0 for row in csv_reader: if i == 0: cols = row if i > 0:…
-
Plotting in pandas
Below, you see a snippet on how to create a plot in Pandas. import pandas as pd data = pd.read_csv(r”C:\Users\tomva\SynologyDrive\python\pandas\Incomplete\banking.csv”, header = 0) deel = data.sample(frac =.005) ax1 = deel.plot.scatter(x=’age’, y=’duration’, c=’DarkBlue’)
-
Logistic regression with Pandas
Below, you see a logistic regression with Panda import pandas as pd import numpy as np from sklearn import preprocessing import matplotlib.pyplot as plt plt.rc(“font”, size=14) from sklearn.linear_model import LogisticRegression from sklearn.model_selection import train_test_split import seaborn as sns sns.set(style=”white”) sns.set(style=”whitegrid”, color_codes=True) data = pd.read_csv(r”C:\Users\tomva\SynologyDrive\python\pandas\Incomplete\banking.csv”, header = 0) data[‘education’]=np.where(data[‘education’] ==’basic.9y’, ‘Basic’, data[‘education’]) data[‘education’]=np.where(data[‘education’] ==’basic.6y’, ‘Basic’, data[‘education’])…
-
Crosstabs in Python
Find below a Python programme that shows a crosstab whereby continuous variables are translated into classes: import pandas as pd import sqlalchemy as sa import matplotlib.pyplot as plt import numpy as np table_name = ‘MIGRATED_DIVORCE_SETTLEMENT__C’ connection_string = “DRIVER={ODBC Driver 17 for SQL Server};SERVER=DESKTOP-8J58OIP\MSSQLSERVER_19;DATABASE=Speel;UID=sa;PWD=**” connection_url = sa.engine.URL.create(“mssql+pyodbc”, query={“odbc_connect”: connection_string}) engine = sa.engine.create_engine(connection_url) with engine.begin() as conn:…
-
Univariate descriptive statistics in python
Below, a programme is given on the univariate statistics. import pandas as pd import sqlalchemy as sa import matplotlib.pyplot as plt import numpy as np table_name = ‘MIGRATED_DIVORCE_SETTLEMENT__C’ connection_string = “DRIVER={ODBC Driver 17 for SQL Server};SERVER=DESKTOP-8J58OIP\MSSQLSERVER_19;DATABASE=Speel;UID=sa;PWD=AAaa11!!” connection_url = sa.engine.URL.create(“mssql+pyodbc”, query={“odbc_connect”: connection_string}) engine = sa.engine.create_engine(connection_url) with engine.begin() as conn: df = pd.read_sql_query(sa.text(“SELECT * FROM Rapportage.” +…