Here is the logic to convert a list to a table in Python. The code will do two things. Write list data to a dataframe and save it to a table. From the table, you can query and get insights.
Do it in two steps. First, write list data for the dataframe. Second, from it write into a table for future analysis.
Step#1: Write data to dataframe
In the first statement, you imported the Pandas library. There are two lists – names and grades. Then the zip function compress these two lists. The df objects contain the dataframe.
import pandas as pd
names = ['Bob','Jessica','Mary','John','Mel']
grades = [76,95,77,78,99]
GradeList = zip(names,grades)
df = pd.DataFrame(data = GradeList,
columns=['Names', 'Grades'])
df
Step#2: Export data to a Table
Here you imported the sqlite3 database. The con object connects to the database. Here mydb.db. Next, the df. to_sql exports data to the SQLite table. Below is the self-explanatory syntax. The logic is helpful for data-analytics engineers.
import os
import sqlite3 as lite
db_filename = r'mydb.db'
con = lite.connect(db_filename)
df.to_sql('mytable',
con,
flavor='sqlite',
schema=None,
if_exists='replace',
index=True,
index_label=None,
chunksize=None,
dtype=None)
con.close()
Once data exports to SQL Table, you can query it to get meaningful insights.
Tip#1
I have gone through some of the best resources books and Udemy course. Here are the links for you. The book is Learning Python from Basic to Advance and the Udemy course is Python Django and E-commerce Web application development.
Related
References