Mohammad Rehan Commerce 3.0
Mohammad Rehan Commerce 3.0
Acknowledgement……..
CERTIFICATE….
I, ‘Mohammad Rehan’, a student of Class 12, hereby express my heartfelt gratitude to “Delhi Public
School”, for providing me the opportunity to work on the project during the academic year 2024
I would like to sincerely thank Mr Neeraj Sir for their valuable guidance, support, and
I am also grateful to my school, principal, and classmates for their constant motivation
and cooperation during the process. This project has been a significant learning
---
Date: 21/11/24
(Mohammad Rehan)
4
5
6-23
24
25
5
6
Explanation:
1. scalar_value = 5: The scalar value 5 is stored in
a variable.
2. for _ in range(4): This loop will iterate 4 times.
The underscore (_) is commonly used when the
loop variable is not needed.
3. print(scalar_value): In each iteration, the
scalar value (5) is printed.
Output:
5
5
5
5
7
Output:
Table 40
8
Sofa 2
Chair 45
Stool 26
dtype: int64
# Create a dictionary with house names as keys and the number of students as values
house_data = {
'Beas': 18,
'Chenab': 2,
'Ravi': 20,
'Satluj': 18
students_series = pd.Series(house_data)
print(students_series)
Explanation:
1. Dictionary house_data: The dictionary contains house names as keys and the
number of students in each house as values.
Output:
9
Beas 18
Chenab 2
Ravi 20
Satluj 18
dtype: int64
import pandas as pd
print(s[s % 2 == 0])
Explanation:
1. pd.Series creates a pandas Series with the given data [1, 2, 3, 4, 5, 6] and the
specified indices ['A', 'B', 'C', 'D', 'E', 'F'].
3. s[s % 2 == 0] filters the Series using this mask, retaining only the even values.
Output:
css
Copy code
B 2
D 4
F 6
dtype: int64
import pandas as pd
print(p)
p1 = p.reindex([2, 5])
print(p1)
The first print(p) prints the full p series, which is created with
the names ['Raj', 'Ankur', 'Harsh'] and the indices [2, 5, 6].
Thus, it displays:
2 Raj
5 Ankur
6 Harsh
dtype: object
0 5
1 6
2 7
3 8
4 9
dtype: int64
Thus, S1 contains 5 elements.
python
Copy code
import pandas as pd
Stock = pd.DataFrame({
'Name': ['Nancy Drew', 'Hardy boys', 'Diary of a wimpy kid', 'Harry Potter'],
})
python
Copy code
ii. Add a new book named ‘The Secret’ with price 800
python
Copy code
python
Copy code
Stock = Stock.drop(columns=['Price'])
iv. Remove the first and third rows from the DataFrame
python
Copy code
Final Code
Combining everything:
python
Copy code
import pandas as pd
13
# Initial DataFrame
Stock = pd.DataFrame({
'Name': ['Nancy Drew', 'Hardy boys', 'Diary of a wimpy kid', 'Harry Potter'],
})
Stock = Stock.drop(columns=['Price'])
print(Stock)
Final Output
After executing all steps, the resulting DataFrame will look like this:
markdown
Copy code
Name Special_Price
Q9) Consider the following dataframe ndf as shown below: (A) Write statements to do the
following: (a) Extract column col3 (b) Extract row t3 (c) Extract row t2’s column col2’s value (d)
Extract row t4’s columns col2, col3, and Res (e) Delete rows t2 and t3 (f) Delete columns col2 and
Res.
14
import pandas as pd
ndf = pd.DataFrame({
'Res': [1, 0, 1, 0]
markdown
Copy code
t1 10 15 20 1
t2 20 25 30 0
t3 30 35 40 1
t4 40 45 50 0
python
Copy code
col3 = ndf['col3']
print(col3)
Output:
yaml
Copy code
t1 20
t2 30
t3 40
t4 50
python
Copy code
row_t3 = ndf.loc['t3']
print(row_t3)
Output:
col1 30
col2 35
col3 40
Res 1
python
print(t2_col2_value)
Output:
Copy code
25
python
print(t4_selected_columns)
Output:
col2 45
col3 50
Res 0
python
print(ndf_dropped_rows)
Output:
16
markdown
t1 10 15 20 1
t4 40 45 50 0
python
Copy code
print(ndf_dropped_columns)
Output:
markdown
Copy code
col1 col3
t1 10 20
t2 20 30
t3 30 40
t4 40 50
# Sample DataFrame
data = {
}
17
t1 10 50 90 A
t2 20 60 100 B
t3 30 70 110 C
t4 40 80 120 D
Evaluating Statements
o df.at['t3', 'Res'] fetches the value 'C' (row t3, column Res).
o Output: C 90
o df.iat[3, 2] fetches the value 120 (row index 3, column index 2).
o df.iat[2, 3] fetches the value 'C' (row index 2, column index 3).
o Output: 120 C
o Output:
mathematica
Copy code
t2 20 60 100 B
t3 30 70 110 C
t4 40 80 120 D
o Output:
css
Copy code
col3 Res
t1 90 A
t2 100 B
18
t3 110 C
t4 120 D
o Selects rows 't2' to 't3' (inclusive) and columns from 'col3' onwards.
o Output:
css
Copy code
col3 Res
t2 100 B
t3 110 C
o Selects the first two rows (:2) and columns starting from the 3rd column (2:).
o Output:
css
Copy code
col3 Res
t1 90 A
t2 100 B
o Selects rows starting from the 3rd row (2:) and the first three columns (:3).
o Output:
markdown
t3 30 70 110
t4 40 80 120
o Selects rows from index 1 to 2 (not including 3) and the 3rd column only
(2:3).
o Output:
markdown
Copy code
col3
t2 100
t3 110
19
import pandas as pd
d={
df = pd.DataFrame(d)
print(df)
print(df1)
1. df
df is created from the dictionary d. The DataFrame will align the indices (a, b, c, d) across
the Series in the dictionary, filling missing values with NaN.
Output of df:
20
css
Copy code
one two
a 1.0 1.0
b 2.0 2.0
c 3.0 3.0
d NaN 4.0
2. df1
df1 is created with a custom index ['d', 'b', 'a']. The rows are reordered based on this index.
If any index from the new list is not present in the original data, it will be filled with NaN.
Output of df1:
css
Copy code
one two
d NaN 4.0
b 2.0 2.0
a 1.0 1.0
3. df2
df2 is created with a custom index ['d', 'a'] and columns ['two', 'three'].
• The column 'three' does not exist in the original data, so it will be filled with NaN.
Output of df2:
Copy code
two three
d 4.0 NaN
a 1.0 NaN
Final Outputs:
print(df):
css
Copy code
one two
a 1.0 1.0
21
b 2.0 2.0
c 3.0 3.0
d NaN 4.0
print(df1):
css
Copy code
one two
d NaN 4.0
b 2.0 2.0
a 1.0 1.0
Q11) Write code to do the following: Plot the following data on line
chart: Runs in Overs 10 20 MI 110 224 RCB 85
210
# Data
plt.figure(figsize=(8, 6))
plt.xlabel("Overs", fontsize=12)
plt.ylabel("Runs", fontsize=12)
22
plt.legend(title="Teams", fontsize=10)
plt.xticks(overs)
plt.show()
Explanation:
1. Data Preparation:
o Runs scored by MI and RCB in these overs are [110, 224] and [85, 210],
respectively.
2. Plotting:
3. Customization:
Output:
The resulting line chart will show two lines, one for MI and one for RCB, plotting their runs
against overs.
Explanation:
• [::-3] is a slicing operation with a step of -3, which means:
o Start from the end of the series.
o Traverse the series backward, taking every third element.
When applying this step to the animal series:
1. Start from the last element (W: Wolf).
2. Move backward by three steps: T: Tiger.
3. Continue: No further valid elements (end of the series).
Output:
The result of animal[::-3] is:
W Wolf
T Tiger
dtype: object
24
25