Develop a program to create a DataFrame from a NumPy array with custom column
names.
import numpy as np
import pandas as pd
# Create a NumPy array
data = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
# Define custom column names
columns = ['Column A', 'Column B', 'Column C']
# Create a DataFrame
df = pd.DataFrame(data, columns=columns)
print("DataFrame created from NumPy array:")
print(df)
2. Drawing a Bar Plot and Scatter Plot using Matplotlib
import matplotlib.pyplot as plt
# Data for plots
categories = ['A', 'B', 'C', 'D']
values = [4, 7, 1, 8]
x = [1, 2, 3, 4]
y = [10, 20, 25, 30]
# Bar Plot
plt.figure(figsize=(8, 4))
plt.bar(categories, values, color='skyblue')
plt.title("Bar Plot")
plt.xlabel("Categories")
plt.ylabel("Values")
plt.show()
# Scatter Plot
plt.figure(figsize=(8, 4))
plt.scatter(x, y, color='red', label='Points')
plt.title("Scatter Plot")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.legend()
plt.show()
Explanation:
DataFrame Creation:
- The program uses
np.array
to create a data matrix. - Custom column names are passed to
pd.DataFrame
to create the DataFrame.
- The program uses
Bar Plot:
- A bar plot is drawn using
plt.bar
, with labels for categories and values.
- A bar plot is drawn using
Scatter Plot:
- A scatter plot is created using
plt.scatter
, withx
andy
as input points.
- A scatter plot is created using
Let me know if you'd like to expand or modify these examples!
No comments:
Post a Comment