Python Programming 2 Class 12 Questions and Answers, This chapter provides a fundamental python programming questions and answers. Students will gain hands-on experience with essential Python libraries, preparing them for more advanced data analysis and machine learning tasks which can be incorporated in their capstone project.
Python Programming 2 Class 12 Questions and Answers
B. Short Answer Questions
1. What is a DataFrame in Pandas?
Answer: A dataframe is a data structure which is constructed with rows and columns, just like an Excel spreadsheet or database. DataFrame works on Tabular data; this table organises data into rows and columns.
2. How do you create a Pandas Series from a dictionary?
Answer: Pandas provides two data structures for manipulating data, series and dataframes. In a series data structure, simply pass the dictionary to the pd.Series() function with dictionary keys, which will become the index and values of the series. Example,
import pandas as pd
series1 = pd.series([10,20.30])
3. Name two strategies to handle missing values in a DataFrame.
Answer: The two most common strategies for handling missing values explained in this section are:
- Drop the row having missing values: The dropna() function can be used to drop an entire row from the DataFrame
- Estimate the missing value: The fillna(num) function can be used to replace missing value(s) by the value specified in num.
4. What does the head(n) function do in a DataFrame?
Answer: The head(n) function in a dataframe returns the first n rows of the dataframe, which helps to inspect the beginning of the data quickly.
5. What is the role of NumPy in Python programming?
Answer: NumPy is a fundamental library of numerical and scientific computer in python. It is a general-purpose array-processing package. In NumPy, handles mathemattical computations which involving arrays and matrices.
6. Explain the use of the isnull() function in Pandas.
Answer: Pandas provide a function isnull() to check whether any value is missing or not in the DataFrame. This function checks all attributes and returns True in case that attribute has missing values, otherwise returns False.
C. Long Answer Questions
1. Describe the steps to import and export data using Pandas.
Answer: The steps to import and export data using Pandas are:
Importing Data
- Step 1: Use the command “pip install pandas” to install pandas library
- Step 2: Use the command to “import pandas as pd”
- Step 3: Read data from file using “pd.read_csv(“filename.csv”)”
- Step 4: Preview the data using “pd.head()”
Exporting Data:
- Step 1: Ensure your DataFrame (df) is ready for export
- Step 2: Use “df.to_csv(“filename.csv”, index-False)” to save the DataFrame as a CSV file.
2. Explain the concept of handling missing values in a DataFrame with examples.
Answer: Handling missiog values in a DataFrame is important step in data preprocessing and data cleaning. To handle missing values in python, there are multiple ways.
- Identifying Missing Values: Pandas provide a function isnull() to check whether any value is missing or not in the DataFrame. This function checks all attributes and returns True in case that attribute has missing values, otherwise returns False.
- Drop Missing Values: Dropping will remove the entire row (object) having the missing value(s). The dropna() function can be used to drop an entire row from the DataFrame.
- Estimate the missing value: The fillna(num) function can be used to replace missing value(s) by the value specified in num. For example, fillna(0) replaces missing value by 0. Similarly fillna(1) replaces missing value by 1.
3. What is Linear Regression, and how is it implemented in Python?
Answer: Linear regression is a data analysis technique which helps to predict the value from the unknown data. Linear regression is a mathematical model which is used to find a linear relationship between a dependent variable and one or more independent variables. In Python, linear regression is applied using libraries like scikit-learn and statsmodels to fit models and make predictions.
Applying the Linear Regression Algorithm
from sklearn.linear_model import LinerRegression
m=LinearRegression()
m.fit(x_train,y_train)
4. Compare NumPy arrays and Pandas DataFrames.
NumPy Array | Pandas DataFrame |
---|---|
It is used in numerical data | It is used in Tabular data |
Powerful tool for Array | Prowerful tools of DataFrame and Series |
Better performance when number of rows less than 50K | Better performance when number of rows more than 500K |
Less used in industry application | Higher used in industry application |
5. How can we add new rows and columns to an existing DataFrame? Explain with code examples.
Answer: The Pandas library is used to add new rows and columns in a Python DataFrame.
Adding a New Column
import pandas as pd
data = {'Name': ['Alice', 'Bob'], 'Age': [25, 30]}
df = pd.DataFrame(data)
# Add a new column
df['City'] = ['New York', 'Los Angeles']
print("After Adding a Column:")
print(df)
Adding a New Row
# Add a new row
df.loc[2] = ['Charlie', 35, 'Chicago']
print("After Adding a Row with loc:")
print(df)
6. What are the attributes of a DataFrame? Provide examples.
Answer: Several attributes are used in the DataFrame in pandas; these attributes are helpful for exploring and manipulating the DataFrame. Some of the common attributes are used:
i) DataFrame.index
>>>df.index
ii) DataFrame.columns
>>>df.columns
iii) DataFrame.shape
>>>df.shape
iv) DataFrame.head(n)
>>>df.head(2)
Disclaimer: We have taken an effort to provide you with the accurate handout of “Make Machine See Class 12 Questions and Answers“. If you feel that there is any error or mistake, please contact me at anuraganand2017@gmail.com. The above CBSE study material present on our websites is for education purpose, not our copyrights. All the above content and Screenshot are taken from Artificial Intelligence Class 12 CBSE Textbook, Sample Paper, Old Sample Paper, Board Paper and Support Material which is present in CBSEACADEMIC website, This Textbook and Support Material are legally copyright by Central Board of Secondary Education. We are only providing a medium and helping the students to improve the performances in the examination.
Images and content shown above are the property of individual organizations and are used here for reference purposes only.
For more information, refer to the official CBSE textbooks available at cbseacademic.nic.in