Data Transfer Between Files, Databases
and DataFrames
Based on CBSE Curriculum
Class -11
By-
Neha Tyagi
PGT CS
KV 5 Jaipur II Shift
Jaipur Region
Neha Tyagi, PGT CS II Shift Jaipur
Introduction
• In the last chapter we have learnt about python pandas library
in which we have gone through dataframes and series.
• In this chapter we will see that how to change data into .CSV
file and how to import data in the program from a .CSV file.
(.CSV file, is a file of Comma Seperated Values from which
data can be taken to a dataframe and vice-versa).
• In this chapter we will learn that how to connect a database
table with python by SQL commands.
Neha Tyagi, PGT CS II Shift Jaipur
Data transfer between DataFrames and .CSV file
• CSV format is a kind of tabular data separated by comma and
is stored in the form of plaintext.
Roll No Name Marks Roll No., Name,Marks
101 Ramesh 77.5 101,Ramesh,77.5
After conversion to CSV Format
102 Harish 45.6 102,Harish,45.6
Tabular Data
In CSV format-
• Each row of the table is stored in one row.
• The field-vales of a row are stored together with comma after
every field value.
Advantages of CSV format-
• A simple, compact and ubiquitous format for data storage.
• A common format for data interchange.
• It can be opened in popular spredsheet packages like MS-EXCEL etc.
• Nearly all spredsheets and databases support import/export to csv
format.
Neha Tyagi, PGT CS II Shift Jaipur
Loading Data from CSV to DataFrame
Emp.csv file , in tabular format Emp.csv file , in Notepad format
Neha Tyagi, PGT CS II Shift Jaipur
Reading from a CSV file to DataFrame
import pandas as pd
<DF>=pd.read_csv(<FilePath>)
• Assume the file path as c:\data\emp.csv then following type of
file will be opened-
Neha Tyagi, PGT CS II Shift Jaipur
Reading from a CSV file to DataFrame
• If a file does not have top row i.e. Headings then it is possible
to provide headings in python.
Headings does nt come
from header=None
It takes data by skipping one
row from skiprows = 1
Neha Tyagi, PGT CS II Shift Jaipur
Reading selected lines fron CSV file
Use of nrows= <n>
Reading from CSV file when separator is other than comma
Use of sep= <char>
Neha Tyagi, PGT CS II Shift Jaipur
Writing from a DataFrame to CSV file
import pandas as pd
<DF>.to_csv(<FilePath>)
or
<DF>.to_csv(<FilePath>,sep=<char>)
• Suppose our file path is c:\data\data.csv then -
If there os NaN
values then it is
stored in file as
empty string.
Here @ is used
as seperator
Neha Tyagi, PGT CS II Shift Jaipur
Data transfer between DataFrames and SQL Database
• In this chapter we will learn that how to transfer data from SQL
table with the help of sqlite3 library of python. sqlite3 comes
inbuilt with python. sqlite3 library deals with SQLite databases.
• Use www.sqlite.org/download.html to download Sqlite3.
• We work on sqlite> prompt on Sqlite3. It supports all
commands of SQL which are supported by mysql.
Neha Tyagi, PGT CS II Shift Jaipur
Data transfer between DataFrames and SQL Database
Creation of table in sqlite3 is shown here.
Here sqlite3 is installed.
Neha Tyagi, PGT CS II Shift Jaipur
Data transfer between DataFrames and SQL Database
Data has
transferred to
database from
DataFrame.
Neha Tyagi, PGT CS II Shift Jaipur
Plotting from DataFrames
Use pip install for installing matplotlib module.
Neha Tyagi, PGT CS II Shift Jaipur
Plotting from DataFrames
Neha Tyagi, PGT CS II Shift Jaipur
Thank you
Please follow us on our blog
www.pythontrends.wordpress.com
Neha Tyagi, KV 5 Jaipur II Shift