Exit Chapter 04 ยท ๐Ÿ“˜ Importing/Exporting Data (CSV & MySQL) โ€” Quiz

Playing as a guest? to earn XP and track your progress.

Question 1 of 50 0 correct so far

๐Ÿ“˜ Importing/Exporting Data (CSV & MySQL) Quiz

Q1.

Which function is used to load data from a CSV file into a Pandas DataFrame?

Explanation

The function read_csv() is used to read a comma-separated values (CSV) file into a DataFrame.

Q2.

What does CSV stand for?

Explanation

CSV stands for Comma Separated Values, a common text file format for tabular data.

Q3.

Which parameter in read_csv() is used to specify a delimiter other than a comma?

Explanation

The sep (separator) parameter allows you to define delimiters like tabs, spaces, or semicolons.

Q4.

What is the default behavior of read_csv() regarding the first row of the file?

Explanation

By default, read_csv() assumes the first row (index 0) contains the column names (headers).

Q5.

If a CSV file does not have a header row, which parameter should be used in read_csv()?

Explanation

Setting header=None tells Pandas that the file contains no column names, so it generates default integers (0, 1, 2...).

Q6.

Which function is used to save a Pandas DataFrame to a CSV file?

Explanation

to_csv() is the method used to write DataFrame data into a CSV file.

Q7.

Which parameter in to_csv() prevents writing the row index numbers to the file?

Explanation

Setting index=False ensures that the row labels (0, 1, 2...) are not saved into the CSV file.

Q8.

What is the default value for the sep parameter in read_csv()?

Explanation

The default separator for CSV files is a comma (,).

Q9.

Which library is required to connect Python to a MySQL database using standard connectors?

Explanation

The mysql.connector library is the standard driver used to establish a connection between Python and MySQL.

Q10.

Which Pandas function is used to execute a SQL query and fetch the result into a DataFrame?

Explanation

read_sql() executes a SQL query on a database connection and returns the result as a DataFrame.

Q11.

When using read_csv(), which parameter allows you to assign your own list of column names?

Explanation

The names parameter accepts a list of strings to be used as column names.

Q12.

In read_csv(), what does the nrows parameter do?

Explanation

nrows limits the number of rows read from the file, which is useful for large datasets.

Q13.

What happens if you provide a names list in read_csv() but do not set header=None?

Explanation

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.

Q14.

Which parameter in read_csv() is used to use a specific column as the row index?

Explanation

index_col allows you to specify a column (by name or index) to become the DataFrame's index.

Q15.

In to_csv(), which parameter specifies the string to use for missing values (NaN)?

Explanation

na_rep stands for 'NA representation' and sets the string used for missing data in the output file.

Q16.

Which parameter allows skipping a specific number of rows at the beginning of a CSV file while reading?

Explanation

skiprows accepts an integer indicating how many lines to skip at the start of the file.

Q17.

What is the return type of pd.read_csv()?

Explanation

read_csv() always returns a Pandas DataFrame containing the data.

Q18.

To save only specific columns to a CSV file, which parameter is used in to_csv()?

Explanation

The columns parameter in to_csv() takes a list of column names to write to the file.

Q19.

What is the mandatory argument for read_csv()?

Explanation

The file path (or buffer) is the only mandatory argument required to locate the file.

Q20.

Which file mode is generally used by to_csv() internally to write data?

Explanation

By default, to_csv() opens the file in write mode ('w'), overwriting existing files.

Q21.

Which function is used to store DataFrame data into a MySQL table?

Explanation

to_sql() is the method used to write records stored in a DataFrame to a SQL database.

Q22.

Which library is recommended by Pandas for creating connection engines for to_sql()?

Explanation

Pandas recommends SQLAlchemy because it handles database dialects and data types more robustly than raw connectors.

Q23.

In df.to_sql(), what does the parameter if_exists='fail' do?

Explanation

'fail' is the default behavior; it prevents accidental overwriting by raising a ValueError if the table already exists.

Q24.

Which value for if_exists in to_sql() is used to add data to an existing table without deleting old data?

Explanation

'append' adds new rows to the existing table.

Q25.

Which value for if_exists in to_sql() drops the old table and creates a new one?

Explanation

'replace' drops the table before creating a new one with the current DataFrame's data.

Q26.

What are the two mandatory arguments for pd.read_sql()?

Explanation

read_sql() requires a SQL query string and a valid connection object.

Q27.

Which parameter is used in read_sql() to pass variables safely into a query?

Explanation

The params parameter accepts a list or tuple of values to safely substitute placeholders in the SQL query.

Q28.

When using to_sql(), which parameter prevents the DataFrame index from being written as a column?

Explanation

Setting index=False prevents the index column from being saved to the SQL table.

Q29.

What error occurs if you try to read a CSV file that does not exist?

Explanation

Python raises a FileNotFoundError if the specified file path cannot be found.

Q30.

Can read_csv() read a file directly from a URL?

Explanation

read_csv() supports reading directly from HTTP/HTTPS URLs.

Q31.

What function creates a connection object in mysql.connector?

Explanation

mysql.connector.connect() is the standard method to establish a session with the database.

Q32.

Which argument in read_csv() allows reading only a subset of columns?

Explanation

usecols accepts a list of column names or indices to read specific columns.

Q33.

Which method is used to close a database connection?

Explanation

The .close() method is used to terminate the connection to the database.

Q34.

If sep='\t' is used in read_csv(), what type of file is being read?

Explanation

\t represents a tab character, so the file is Tab Separated.

Q35.

What is the primary advantage of read_sql() over read_csv()?

Explanation

read_sql() allows utilizing the database engine to filter (WHERE clause) and aggregate data before bringing it into Python.

Q36.

When saving a DataFrame to CSV, what is the default behavior for the header parameter?

Explanation

By default, header=True, meaning column names are written as the first row.

Q37.

Which Python symbol is used to pass arguments safely in a parameterized SQL query (using mysql.connector)?

Explanation

The standard placeholder for mysql.connector is %s.

Q38.

What does df.shape return after loading a CSV?

Explanation

df.shape returns a tuple representing the dimensionality: (number of rows, number of columns).

Q39.

If you want to read a file data.txt where values are separated by a pipe (|), how should you call read_csv?

Explanation

You must explicitly specify the separator sep='|'.

Q40.

What is the purpose of na_values parameter in read_csv()?

Explanation

na_values allows you to specify a list of strings (like 'NULL', 'Missing') that should be treated as NaN.

Q41.

Which function allows creating a SQLAlchemy engine?

Explanation

sqlalchemy.create_engine() is the function used to establish the connection interface.

Q42.

When using to_sql(), what does the index_label parameter define?

Explanation

It specifies the column label for the index if index=True.

Q43.

Which of the following is NOT a valid if_exists value in to_sql()?

Explanation

'update' is not a valid option. The valid options are 'fail', 'replace', and 'append'.

Q44.

Can read_csv() handle compressed files (like .zip or .gz)?

Explanation

read_csv() can detect compression from the file extension or via the compression parameter.

Q45.

What happens if the columns list provided in to_csv() contains a name not in the DataFrame?

Explanation

Pandas raises a KeyError because it cannot find the specified column in the DataFrame.

Q46.

Which method is used to verify the first 5 rows of a loaded DataFrame?

Explanation

head() displays the first 5 rows by default.

Q47.

What does the encoding parameter in read_csv() handle?

Explanation

encoding specifies the character encoding of the file, which is crucial for reading special characters.

Q48.

If you want to append data to a CSV file instead of overwriting, which mode should be passed?

Explanation

mode='a' stands for append mode.

Q49.

In read_sql(), what is the return type if the query returns no rows?

Explanation

It returns an empty DataFrame with the correct columns but zero rows.

Q50.

Which statement best describes Data Persistence?

Explanation

Data persistence refers to saving data to a non-volatile storage system (like a file or DB) so it survives after the process ends.