Python ClassXII AI
Python ClassXII AI
Creating Series
import pandas as pd
series = pd.Series([10, 20, 30])
print(series)
✅ Explanation: This creates a simple 1D labeled array with automatic index values starting from 0.
ResultSheet = {
'Maths': pd.Series([90, 91, 97, 89, 65, 93], index=['Heena', 'Shefali', 'Meera', 'Joseph', 'Suhana',
'Bismeet']),
'Science': pd.Series([92, 81, np.NaN, 87, 50, 88], index=['Heena', 'Shefali', 'Meera', 'Joseph', 'Suhana',
'Bismeet']),
'English': pd.Series([89, 91, 88, 78, 77, 82], index=['Heena', 'Shefali', 'Meera', 'Joseph', 'Suhana',
'Bismeet']),
'Hindi': pd.Series([81, 71, 67, 82, np.NaN, 89], index=['Heena', 'Shefali', 'Meera', 'Joseph', 'Suhana',
'Bismeet']),
'AI': pd.Series([94, 95, 99, np.NaN, 96, 99], index=['Heena', 'Shefali', 'Meera', 'Joseph', 'Suhana',
'Bismeet'])
}
marks = pd.DataFrame(ResultSheet)
✅ Explanation:
Creates a full DataFrame with students and subjects
Some entries (like Science for Meera) are missing (np.NaN)
print(marks.isnull()) # Shows where data is missing
print(marks['Science'].isnull()) # Check NaNs in Science only
print(marks.isnull().sum().sum()) # Count of total missing entries
drop = marks.dropna()
print(drop)
✅ Drops all rows with missing values.
fillZero = marks.fillna(0)
print(fillZero)
✅ Replaces all missing values with 0, so the data can still be used.
3. In Linear Regression, which library is typically used for importing and managing data?
Answer: b) Pandas
4. What is the correct syntax to read a CSV file into a Pandas DataFrame?
Answer: b) pd.read_csv("filename.csv")
5. How can we add new rows and columns to an existing DataFrame? Explain with code examples.
Answer: To add a column:
df['new_col'] = [val1, val2, val3]
To add a row:
df.loc[len(df)] = [val1, val2, val3]
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.
Answer:
import pandas as pd
df = pd.read_csv('student_marks.csv')
df.fillna(df.mean(), inplace=True)
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.
Answer:
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.
Answer:
df = pd.read_csv('marketing.csv')
avg_metrics = df.groupby('campaign_type')[['sales', 'engagement']].mean()
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.
Answer:
1. Remove irrelevant columns: df.drop(['col1', 'col2'], axis=1, inplace=True)
2. Handle missing values: df.fillna(method='ffill', inplace=True)
3. Convert datatypes if needed: df['col'] = df['col'].astype('int')