Code Explanation:
Importing Required Libraries
import pandas as pd
import statistics as st
pandas (imported as pd) is used for handling tabular data in DataFrames (like an Excel sheet).
statistics (imported as st) provides mathematical functions for mean, median, etc.
Together, they let us work with data and perform simple statistical calculations.
Creating a DataFrame
df = pd.DataFrame({
"A": [10, 20, 30, 40],
"B": [2, 4, 6, 8]
})
A DataFrame is created with two columns — A and B.
Column A: [10, 20, 30, 40]
Column B: [2, 4, 6, 8]
So the DataFrame looks like this:
A B
0 10 2
1 20 4
2 30 6
3 40 8
Creating a New Column “C”
df["C"] = df["A"] / df["B"]
This divides each value in column A by the corresponding value in column B.
Row by row:
10 / 2 = 5.0
20 / 4 = 5.0
30 / 6 = 5.0
40 / 8 = 5.0
So column C becomes [5.0, 5.0, 5.0, 5.0].
Now the DataFrame looks like:
A B C
0 10 2 5.0
1 20 4 5.0
2 30 6 5.0
3 40 8 5.0
Calculating the Mean of Column “C”
avg = st.mean(df["C"])
st.mean() calculates the average (arithmetic mean) of all values in column C.
Since all values are 5.0,
mean=(5+5+5+5)/4=5.0
So, avg = 5.0.
Printing the Result
print(int(avg + df["C"].median()))
df["C"].median() returns the middle value in column C.
All values are 5.0, so median = 5.0.
Add mean and median: 5.0 + 5.0 = 10.0
Convert to integer: int(10.0) → 10
Finally, it prints 10.
Final Output
10
.png)

0 Comments:
Post a Comment