Page 243 - AI_Ver_3.0_class_11
P. 243
2. Why CSV files are famous and what are some basic operations of CSV files?
Ans. Due to the text-based nature, CSV files are platform-independent and easily readable by both humans and machines.
This simplicity, combined with its compatibility with various programming languages and tools, makes CSV a go-to
choice for data interchange, storage, and analysis across diverse domains such as finance, research, and software
development.
Some basic operations of CSV files are as follows:
Importing the CSV library: In Python, the csv module provides functionality to work with CSV files. It includes
classes to read and write tabular data in CSV format.
Opening a CSV file in reading mode: When opening a file in reading mode ('r'), you are telling Python that you
only intend to read from the file, not modify it. The csv.reader() function then reads the contents of the file
line by line.
Opening a CSV File in writing mode: When opening a file in writing mode ('w'), you are telling Python that you
intend to write to the file. If the file already exists, it will be truncated (emptied) first. If it doesn't exist, a new file
will be created.
Closing a CSV file: In Python, it is important to close files after you have finished working with them. However,
using the with statement automatically closes the file when the block is exited, so you don't have to worry about
explicitly closing it.
Writing rows to a CSV file: You can use the writerows() method to write multiple rows to a CSV file at once.
Each row should be a list of values. This function will replace all existing data in the CSV file.
Append rows to a CSV file: Appending a row to an existing CSV file involves opening the file in append mode
('a'), and writing a single row of data to it. You can achieve this using the csv.writer object's writerow() method.
3. List and explain several ways for handling missed values in a DataFrame.
Ans. There are several ways by which you can handle missing values in DataFrame:
Imputation: Imputation involves replacing missing values with a specific value. Common strategies include
replacing missing values with the mean, median, or mode of the column. This method helps in retaining the
structure of the dataset and avoids losing valuable information.
Pandas provides methods like fillna() to perform imputation. For example, you can fill missing values in a
DataFrame df with the mean of each column using df.fillna(df.mean()).
Dropping: Dropping involves removing rows or columns containing missing values. This method is useful when
the missing values are sparse and dropping them doesn't significantly impact the analysis.
Pandas provides the dropna() function to drop rows or columns with missing values. For example, you can drop
rows with any missing values in a DataFrame df using df.dropna().
Interpolation: Interpolation involves estimating missing values based on existing data. Pandas provides
interpolation methods such as interpolate() to estimate missing values. For example, you can perform linear
interpolation on a DataFrame df using df.interpolate(). This method is particularly useful for time series or
ordered data where missing values can be inferred from neighbouring values.
4. Explain and exemplify multiple assignments in Python.
Ans. Multiple assignment of variables, also known as parallel assignment, is a powerful feature in many programming
languages that allows you to assign multiple variables at once.
For example:
x, y, z = 1, 2, 3
In given code, the values 1, 2, and 3 are assigned to variables x, y, and z, respectively. This simultaneous assignment
saves lines of code and improves readability compared to assigning each variable separately.
Multiple assignment is not limited to simple variables. It works with any iterable data structure, including tuples, lists,
and even custom objects that support iteration and unpacking.
Python Programming 241

