Как преобразовать pandas DataFrame в SQL в Python?
В этой статье мы стремимся преобразовать фрейм данных в базу данных SQL, а затем попытаться прочитать содержимое из базы данных SQL с помощью запросов SQL или через таблицу.
Чтобы иметь дело с SQL в python, нам нужно установить библиотеку sqlalchemy, используя указанную ниже команду, запустив ее в cmd:
pip install sqlalchemy
There is a need to create a pandas data frame to proceed further.
Python3
# import pandas libraryimport pandas as pd # create a dataframe # object from dictionarydataset = pd.DataFrame({"Names":["Abhinav","Aryan", "Manthan"], "DOB" : ["10/01/2009","24/03/2009", "28/02/2009"]})# show the dataframeprint(dataset) |
Выход :
Names DOB 0 Abhinav 10/01/2009 1 Aryan 24/03/2009 2 Manthan 28/02/2009
After creating the dataset we need to connect the data frame to the database support which is provided for sqlite3.Connection objects.
Python3
#importing sql libraryfrom sqlalchemy import create_engine # create a referrence # for sql library echo = False) # attach the data frame to the sql # with a name of the table # as "Employee_Data"dataset.to_sql("Employee_Data", con = engine) # show the complete data# from Employee_Data tableprint(engine.execute("SELECT * FROM Employee_Data").fetchall()) |
Выход :
[(0, 'Abhinav', '01.10.2009'), (1, 'Aryan', '24.03.2009'), (2, 'Manthan', '28.02.2009')]
After adding the data to the database, it is visible to us in the form of records. Data can also be appended to the previously created database as shown below:
Python3
# Create a dataframe # object from dictionarydf1 = pd.DataFrame({"Names" : ["Sonia", "Priya"], "DOB":["18/10/2009","14/06/2009"]}) # appending new data frame # to existing data framedf1.to_sql("Employee_Data", con = engine, if_exists = "append") # run a sql queryprint(engine.execute("SELECT * FROM Employee_Data").fetchall()) |
Выход :
[(0, 'Abhinav', '01.10.2009'), (1, 'Aryan', '24.03.2009'), (2, 'Manthan', '28.02.2009'), (0, 'Соня', '18.10.2009'), (1, 'Priya', '14.06.2009 ')]
As understood from the above example that although data is appended the indexing again started from 0 only when a new data frame is appended.A data frame can be transferred to the SQL database, the same way data frame can also be read from the SQL database. the return type of the read_sql is data frame.
Python3
# reading the sql database# with index "Names"df2 = pd.read_sql("Employee_Data", con = engine, index_col = "Names", parse_dates = ["DOB"])# show the dataframeprint(df2) # print new lineprint() # show the type of df2print(type(df2)) |
Выход :
id DOB Имена Соня 0 18.10.2009 Прия 1 14.06.2009
we can also access a particular column in a database rather than the whole table.
Python3
# acccesing only a particular # column from the databasedf3 = pd.read_sql("Employee_Data", con = engine, columns = ["Names"])# show the dataprint(df3) |
Выход :
Имена 0 Соня 1 Прия
If we want to have the data in the database in the form of a list that to is possible.
Python3
# get a particular column # from a database in the# form of listdf4 = pd.read_sql("Employee_Data", con = engine, index_col = "Names", columns = ["Names"])# show the dataprint(df4) |
Выход :
Пустой фрейм данных Столбцы: [] Индекс: [Соня, Прия]
Можно писать SQL-запросы на Python, используя команду read_sql_query () и передавая соответствующий SQL-запрос и объект подключения.
parse_dates: This parameter helps to converts the dates that were originally passed as dates from our side into the genuine dates format.
Python3
# run a sql query in the database# and store result in a dataframedf5 = pd.read_sql_query("Select DOB from Employee_Data", con = engine, parse_dates = ["DOB"])# show the dataframeprint(df5) |
Выход :
Дата рождения 0 18.10.2009 1 2009-06-14
Внимание компьютерщик! Укрепите свои основы с помощью базового курса программирования Python и изучите основы.
Для начала подготовьтесь к собеседованию. Расширьте свои концепции структур данных с помощью курса Python DS. А чтобы начать свое путешествие по машинному обучению, присоединяйтесь к курсу Машинное обучение - базовый уровень.