Anjeev Singh Academy

12 AI Unit 1 Python Programming-II Book Solution

12 AI Unit 1 Python Programming-II Book Solution

Unit 1: Python Programming – II

Unit 1 Python Programming - II

A. Objective-type questions

1. Which of the following is a primary data structure in Pandas?
a) List
b) Tuple
c) Series
d) Matrix

Ans: c) Series

2. What does the fillna(0) function do in Pandas?
a) Removes rows with missing values
b) Fills missing values with zeros
c) Estimates missing values based on averages
d) Converts all data to zero

Ans: b) Fills missing values with zeros

3. In Linear Regression, which library is typically used for importing and managing data?
a) NumPy
b) Pandas
c) Matplotlib
d) Scikit-learn

Ans: b) Pandas

4. What is the correct syntax to read a CSV file into a Pandas DataFrame?
a) pd.DataFrame(“filename.csv”)
b) pd.read_csv(“filename.csv”)
c) pandas.read_file(“filename.csv”)
d) pd.file_read(“filename.csv”)

Ans: b) pd.read_csv(“filename.csv”)

5. What is the result of the df.shape function?
a) Data type of the DataFrame
b) Number of rows and columns in the DataFrame
c) Memory usage of the DataFrame
d) Column names of the DataFrame

Ans: b) Number of rows and columns in the DataFrame

6. Which function can be used to export a DataFrame to a CSV file?
a) export_csv()
b) to_file()
c) to_csv()
d) save_csv()

Ans: c) to_csv()

B. Short Answer Questions

1. What is a DataFrame in Pandas?

Ans: A DataFrame is a 2D data structure in Pandas, similar to a table in a database or Excel
sheet. It consists of rows and columns, where each column can hold different types of data.

2. How do you create a Pandas Series from a dictionary?

Ans:
import pandas as pd
data = {‘a’: 1, ‘b’: 2, ‘c’: 3}
series = pd.Series(data)
print(series)

3. Name two strategies to handle missing values in a DataFrame.

Ans: (i) Dropping rows or columns with missing values using dropna().
(ii) Filling missing values using fillna() with mean, median, or a specific value.

4. What does the head(n) function do in a DataFrame?

Ans: It returns the first n rows of the DataFrame.

5. What is the role of NumPy in Python programming?

Ans: NumPy is used for numerical computations. It provides support for arrays, matrices, and mathematical functions like linear algebra and statistical operations.

6. Explain the use of the isnull() function in Pandas.

Ans: The isnull() function checks for missing values in a DataFrame or Series and returns True
where data is missing and False otherwise.

C. Long Answer Questions

1. Describe the steps to import and export data using Pandas.

Ans:

  • Importing Data: Use pd.read_csv(‘filename.csv’) to read a CSV file into a DataFrame.
  • Exporting Data: Use df.to_csv(‘filename.csv’) to save the DataFrame to a CSV file.

Example:
import pandas as pd
df = pd.read_csv(‘data.csv’)
df.to_csv(‘output.csv’, index=False)

2. Explain the concept of handling missing values in a DataFrame with examples.

Ans: Missing values can be handled by:

  • Dropping rows/columns:
    df = df.dropna()
  • Filling with mean/median:
    df[‘column’] = df[‘column’].fillna(df[‘column’].mean())

3. What is Linear Regression, and how is it implemented in Python?

Ans: Linear Regression is a statistical method used to model the relationship between a dependent variable and one or more independent variables.

Example:
from sklearn.linear_model import LinearRegression
model = LinearRegression()
model.fit(X_train, y_train)
predictions = model.predict(X_test)

4. Compare NumPy arrays and Pandas DataFrames.

Ans: NumPy Arrays vs Pandas DataFrames

  • NumPy Arrays: Homogeneous, fast for mathematical operations.
  • Pandas DataFrames: Heterogeneous, better for data analysis and manipulation.

5. How can we add new rows and columns to an existing DataFrame? Explain with code examples.

Ans:

* Add a column:
df[‘new_column’] = [value1, value2, value]

  • Add a row:
    df.loc[len(df)] = [value1, value2, value]

6. What are the attributes of a DataFrame? Provide examples.
Ans:

  • df.index: Returns the index of the DataFrame.
  • df.columns: Returns the column labels.
  • df.shape: Returns the dimensions (rows, columns).

D. Case study

1. A dataset of student marks contains missing values for some subjects. Write Python code to handle these missing values by replacing them with the mean of the respective columns.

Ans:
import pandas as pd
df = pd.DataFrame({‘Maths’: [90, None, 88], ‘Science’: [None, 92, 85]})
df.fillna(df.mean(), inplace=True)
print(df)

2. Write Python code to load the file into a Pandas DataFrame, calculate the total sales for each product, and save the results into a new CSV file.

Click in the link below to access sales.csv dataset.

Ans:
import pandas as pd
df = pd.read_csv(‘sales.csv’)
total_sales = df.groupby(‘Product’)[‘Sales’].sum()
total_sales.to_csv(‘total_sales.csv’)

3. In a marketing dataset, analyze the performance of campaigns using Pandas. Describe steps to group data by campaign type and calculate average sales and engagement metrics.

Ans:
grouped = df.groupby(‘Campaign Type’).agg({‘Sales’: ‘mean’, ‘Engagement’: ‘mean’})
print(grouped)

4. A company has collected data on employee performance. Some values are missing, and certain columns are irrelevant. Explain how to clean and preprocess this data for analysis using Pandas.

Ans:

  • Drop irrelevant columns using drop().
  • Handle missing values using fillna() or dropna().
  • Normalize or scale data if needed.
    Example:
    df = df.drop([‘Irrelevant Column’], axis=1)
    df.fillna(0, inplace=True)

Sorry! You cannot copy content of this page. Please contact, in case you want this content.

Scroll to Top