Ch. 4 – Importing Exporting Data Between CSV Files MySQL and Pandas
Sumita Arora Book Exercise Solution
Type A: Short Answer Questions/Conceptual Questions
1. What are the advantages of CSV file formats?
Ans: Advantages of CSV file formats are: –
i) Easy to create
ii) Easy to maintain
iii) Can open in Notepad and Spreadsheet software
iv) Portable – can be transfer
v) Less memory consumption
2. What all libraries do you require in order to bring data from a CSV file into a DataFrame?
Ans: Pandas library is require to bring data from CSV into a DataFrames.
3. You want to read data from a CSV file in a DataFrame but you want to provide your own column names to DataFrame. What additional argument would you specify in read_csv( ) ?
Ans: names argument will be used with read_csv( ) to specify your own column name.
For example :-
df = pd.read_csv( “data.csv”, names = [‘En. No’, ‘Sname’, ‘City’, ‘Amount’] )
4. By default, read_csv( ) uses the values of first row as column headers in DataFrame. Which argument will you give to ensure that the top/first row’s data is used as data and not as column headers?
Ans: header = None argument is used to ignore the column header.
For example :-
df = pd.read_csv(“data.csv”, header = None)
5. Which argument would you give to read.csv( ) if you only want to read top 10 rows of data?
Df = pd.read_csv(“c:\\python\\data\\text.csv”, nrows = 10)
6. Write a command to store data of dataframe mdf into a CSV file Mydata.csv, with separator character as ‘@’.
Ans: sep = ‘@’ -> use sep argument for the same.
Df = pd.read_csv(“c:\\python\\data\\text.csv”, sep = ‘@’, names = [“State”, “Capital”, “Region”] )
7. Why do you need connection to an SQL database in order to get data from a table?
Ans: We need to connection to an SQL database in order to get data from a table because Python and MySQL both are having different architecture and Database connection object controls the connection and ensure the proper data transfer.
8. What is pymysql library of Python?
Ans: pymysql is a library of Python which is used to connect with MySQL.
9. What all libraries do you require in order to interact with MySQL databases (and dataframe) from within Python?
Ans: mysql-connector-python and pymysql
10. What additional argument do you need to specify in to_sql( ) so that old data of MySQL table is retained?
Ans: if_exists argument. The value of if_exists must be append to retain old data of MySQL table.
11. If query is a string storing an SQL statement. Write statements so that the data is fetched based on query from SQL database Mydata.
import pandas as pd
import mysql.connector as sqltor
mycon = sqltor.connect(host = “localhost”, user = “root”, passwd = “abcd”, database = “Mydata”)
query = “——“ #SQL Squrey Statement, as given in question
Df = pd.read_sql(query, mycon)
print(“Error :: MySQL Connection problem”)