Python Libraries Explained
Python Libraries Explained
Explanation
# NumPy
* NumPy is a powerful Python library used for numerical computing, it provides a powerful n-dimensional array
object (ndarray) that is more efficient than native python lists for numerical tasks.
Explanation
## 2.Create a 3x3 NumPy array filled with random integers between 1 and 10.Calculate the sum, mean,and standard
import numpy as np
print(matrix)
# Calculate statistics
total = np.sum(matrix)
average = np.mean(matrix)
Explanation of Python Code (NumPy Example)
std_dev = np.std(matrix)
print("Sum:", total)
print("Mean:", average)
Function Meanings:
Explanation
import numpy as np
print(original_matrix)
reshaped_matrix = original_matrix.reshape(1, 9)
print("Reshaped Matrix.reshape(1x9):")
print(reshaped_matrix)
Function Meanings:
Explanation
# Scipy
### i.scipy.integrate
* Used to calculate areas under curves and solve differential equations-helpful in physics and engineering for
### ii.scipy.optimize
* Solves optimization problems like finding minimum or maximum values-commonly used in machine
### iii.scipy.linalg
* Handles advanced linear algebra tasks such as solving systems of equations-useful in simulations, data analysis,
### iv.scipy.stats
* Performs stastistical analysis, including probability distributions and tests-used in data science and research to
Explanation
def func(x):
Explanation
y_values = func(x_values)
plt.xlabel("x")
plt.ylabel("f(x)")
Explanation of Python Code (NumPy Example)
plt.legend()
plt.grid(True)
plt.show
Function Meanings:
Explanation
# Pandas
## 1.What are the two primary data structures in Pandas , and how do they differ?
### Difference:
* Series is for single-column data, while DataFrame holds multiple columns and rows.
Explanation
# 2.Load a sample CSV file (or create a DataFrame manually) containing columns: Name, Age, Salary.
import pandas as pd
#Sample data
data = {
}
Explanation of Python Code (NumPy Example)
df = pd.DataFrame(data)
print("DataFrame")
print(df)
Explanation
import pandas as pd
data = {
df = pd.DataFrame(data)
print(df)
print(high_salary_df)
Explanation of Python Code (NumPy Example)
Explanation
# 4.Group the data by Age and calculate the average salary for each age group.
import pandas as pd
data = {
#cCreate a DataFrame
df = pd.DataFrame(data)
print(df)
average_salary_by_age = df.groupby('Age')['Salary'].mean()
print(average_salary_by_age)
Explanation
# 5.Provide an example of how to handle missing values in a DataFrame using dropna() and fillna()
Explanation of Python Code (NumPy Example)
import pandas as pd
import numpy as np
data = {
df =pd.DataFrame(data)
print("Original:\n", df)
df_filled = df.copy()
df_filled['Age'] = df_filled['Age'].fillna(df_filled['Age'].mean())
df_filled['Salary'] = df_filled['Salary'].fillna(df_filled['Salary'].mean())
Explanation
# Matplotlib
## 1.What are the key features of Matplotlib that make it popular choice for creating plots?
## 2.Generate a line plot showing the population growth of the city of a city over 10 years .Add labels for the
x-axis(Years), Y-axis(population), and a title(City Population Growth).Customize the plot with gridlines, a legend, and a
population = [50000, 52000, 54000, 57000, 60000, 63000, 66000, 69000, 72000, 75000]
plt.xlabel("Years")
plt.ylabel("Population")
plt.grid(True)
plt.legend()
plt.show()
Explanation
# 2.Create a figure with two subplots: one displaying a bar chart of sales data and the other a pie chart showing market
share percentages.
#Bar Chart
axes[0].set_title("Sales Data")
# Pie Chart
axes[1].set_title("Market Share")
plt.tight_layout()
plt.show()