Playing as a guest? to earn XP and track your progress.
๐ Importing/Exporting Data (CSV & MySQL) Quiz
๐ Importing/Exporting Data (CSV & MySQL)
Which function is used to load data from a CSV file into a Pandas DataFrame?
pd.load_csv()
pd.import_csv()
pd.read_csv()
pd.fetch_csv()
The function read_csv() is used to read a comma-separated values (CSV) file into a DataFrame.
What does CSV stand for?
Common Separated Values
Comma Separated Values
Common System Values
Comma System Variables
CSV stands for Comma Separated Values, a common text file format for tabular data.
Which parameter in read_csv() is used to specify a delimiter other than a comma?
delimiter
sep
split
break
The sep (separator) parameter allows you to define delimiters like tabs, spaces, or semicolons.
What is the default behavior of read_csv() regarding the first row of the file?
It is ignored
It is treated as data
It is treated as column headers
It raises an error
By default, read_csv() assumes the first row (index 0) contains the column names (headers).
If a CSV file does not have a header row, which parameter should be used in read_csv()?
header=None
header=False
no_header=True
skip_header=True
Setting header=None tells Pandas that the file contains no column names, so it generates default integers (0, 1, 2...).
Which function is used to save a Pandas DataFrame to a CSV file?
df.write_csv()
df.save_csv()
df.to_csv()
df.export_csv()
to_csv() is the method used to write DataFrame data into a CSV file.
Which parameter in to_csv() prevents writing the row index numbers to the file?
row_index=False
index=False
no_index=True
header=False
Setting index=False ensures that the row labels (0, 1, 2...) are not saved into the CSV file.
What is the default value for the sep parameter in read_csv()?
\t
,
;
The default separator for CSV files is a comma (,).
Which library is required to connect Python to a MySQL database using standard connectors?
mysql.connector
pandas.sql
numpy
matplotlib
The mysql.connector library is the standard driver used to establish a connection between Python and MySQL.
Which Pandas function is used to execute a SQL query and fetch the result into a DataFrame?
pd.read_db()
pd.read_sql()
pd.sql_query()
pd.fetch_sql()
read_sql() executes a SQL query on a database connection and returns the result as a DataFrame.
When using read_csv(), which parameter allows you to assign your own list of column names?
columns
header
names
titles
The names parameter accepts a list of strings to be used as column names.
In read_csv(), what does the nrows parameter do?
Skips the first n rows
Reads only the first n rows
Reads the last n rows
Reads every nth row
nrows limits the number of rows read from the file, which is useful for large datasets.
What happens if you provide a names list in read_csv() but do not set header=None?
The first row is replaced by new names
The first row is treated as data
An error occurs
The file is not read
If header=0 (default) is active, the first row is treated as the header and then replaced by names. To treat the first row as data, you must use header=None.
Which parameter in read_csv() is used to use a specific column as the row index?
index_col
use_index
key
primary_key
index_col allows you to specify a column (by name or index) to become the DataFrame's index.
In to_csv(), which parameter specifies the string to use for missing values (NaN)?
nan_rep
na_rep
missing_val
fill_na
na_rep stands for 'NA representation' and sets the string used for missing data in the output file.
Which parameter allows skipping a specific number of rows at the beginning of a CSV file while reading?
skip_header
skiprows
ignore_rows
start_row
skiprows accepts an integer indicating how many lines to skip at the start of the file.
What is the return type of pd.read_csv()?
List
Dictionary
DataFrame
Series
read_csv() always returns a Pandas DataFrame containing the data.
To save only specific columns to a CSV file, which parameter is used in to_csv()?
usecols
columns
fields
only
The columns parameter in to_csv() takes a list of column names to write to the file.
What is the mandatory argument for read_csv()?
sep
filepath
header
encoding
The file path (or buffer) is the only mandatory argument required to locate the file.
Which file mode is generally used by to_csv() internally to write data?
'r'
'w'
'a'
'wb'
By default, to_csv() opens the file in write mode ('w'), overwriting existing files.
Which function is used to store DataFrame data into a MySQL table?
df.write_sql()
df.to_sql()
df.save_sql()
df.export_sql()
to_sql() is the method used to write records stored in a DataFrame to a SQL database.
Which library is recommended by Pandas for creating connection engines for to_sql()?
mysql.connector
SQLAlchemy
PyMySQL
sqlite3
Pandas recommends SQLAlchemy because it handles database dialects and data types more robustly than raw connectors.
In df.to_sql(), what does the parameter if_exists='fail' do?
It overwrites the table
It appends data
It raises an error if the table exists
It creates a copy
'fail' is the default behavior; it prevents accidental overwriting by raising a ValueError if the table already exists.
Which value for if_exists in to_sql() is used to add data to an existing table without deleting old data?
'add'
'update'
'append'
'insert'
'append' adds new rows to the existing table.
Which value for if_exists in to_sql() drops the old table and creates a new one?
'delete'
'replace'
'overwrite'
'drop'
'replace' drops the table before creating a new one with the current DataFrame's data.
What are the two mandatory arguments for pd.read_sql()?
Query and Connection
Table Name and Database
Query and DataFrame
File and Mode
read_sql() requires a SQL query string and a valid connection object.
Which parameter is used in read_sql() to pass variables safely into a query?
args
data
params
variables
The params parameter accepts a list or tuple of values to safely substitute placeholders in the SQL query.
When using to_sql(), which parameter prevents the DataFrame index from being written as a column?
index=False
write_index=False
index_label=None
exclude_index=True
Setting index=False prevents the index column from being saved to the SQL table.
What error occurs if you try to read a CSV file that does not exist?
ValueError
IOError
FileNotFoundError
ImportError
Python raises a FileNotFoundError if the specified file path cannot be found.
Can read_csv() read a file directly from a URL?
Yes
No
Only if it is zipped
Only on Linux
read_csv() supports reading directly from HTTP/HTTPS URLs.
What function creates a connection object in mysql.connector?
create()
connect()
login()
open()
mysql.connector.connect() is the standard method to establish a session with the database.
Which argument in read_csv() allows reading only a subset of columns?
columns
usecols
read_cols
select
usecols accepts a list of column names or indices to read specific columns.
Which method is used to close a database connection?
stop()
end()
close()
disconnect()
The .close() method is used to terminate the connection to the database.
If sep='\t' is used in read_csv(), what type of file is being read?
Comma Separated
Tab Separated
Space Separated
Pipe Separated
\t represents a tab character, so the file is Tab Separated.
What is the primary advantage of read_sql() over read_csv()?
It is faster
It uses less memory
It allows filtering data using SQL queries before loading
It requires no libraries
read_sql() allows utilizing the database engine to filter (WHERE clause) and aggregate data before bringing it into Python.
When saving a DataFrame to CSV, what is the default behavior for the header parameter?
False
True
None
Numeric
By default, header=True, meaning column names are written as the first row.
Which Python symbol is used to pass arguments safely in a parameterized SQL query (using mysql.connector)?
?
%s
$
&
The standard placeholder for mysql.connector is %s.
What does df.shape return after loading a CSV?
(rows, columns)
(columns, rows)
Total cells
File size
df.shape returns a tuple representing the dimensionality: (number of rows, number of columns).
If you want to read a file data.txt where values are separated by a pipe (|), how should you call read_csv?
read_csv('data.txt', sep='|')
read_csv('data.txt', delimiter=',')
read_csv('data.txt', pipe=True)
read_csv('data.txt')
You must explicitly specify the separator sep='|'.
What is the purpose of na_values parameter in read_csv()?
To remove NaN values
To recognize specific strings as NaN
To fill NaN values
To count NaN values
na_values allows you to specify a list of strings (like 'NULL', 'Missing') that should be treated as NaN.
Which function allows creating a SQLAlchemy engine?
create_engine()
make_engine()
db_engine()
sql_engine()
sqlalchemy.create_engine() is the function used to establish the connection interface.
When using to_sql(), what does the index_label parameter define?
The name of the index column in the database
Whether to save the index
The data type of the index
The maximum index value
It specifies the column label for the index if index=True.
Which of the following is NOT a valid if_exists value in to_sql()?
fail
replace
append
update
'update' is not a valid option. The valid options are 'fail', 'replace', and 'append'.
Can read_csv() handle compressed files (like .zip or .gz)?
No
Yes, automatically or via compression parameter
Only .zip
Only .tar
read_csv() can detect compression from the file extension or via the compression parameter.
What happens if the columns list provided in to_csv() contains a name not in the DataFrame?
It adds a blank column
It raises a KeyError
It ignores the name
It warns the user
Pandas raises a KeyError because it cannot find the specified column in the DataFrame.
Which method is used to verify the first 5 rows of a loaded DataFrame?
df.tail()
df.view()
df.head()
df.top()
head() displays the first 5 rows by default.
What does the encoding parameter in read_csv() handle?
File compression
Character sets (e.g., utf-8, cp1252)
Password protection
Row indexing
encoding specifies the character encoding of the file, which is crucial for reading special characters.
If you want to append data to a CSV file instead of overwriting, which mode should be passed?
mode='w'
mode='x'
mode='a'
mode='r'
mode='a' stands for append mode.
In read_sql(), what is the return type if the query returns no rows?
None
An empty DataFrame
An empty list
An error
It returns an empty DataFrame with the correct columns but zero rows.
Which statement best describes Data Persistence?
Keeping data in RAM
Saving data permanently to storage
Deleting unused data
Sorting data in memory
Data persistence refers to saving data to a non-volatile storage system (like a file or DB) so it survives after the process ends.