0% found this document useful (0 votes)
8 views

IDML Lab Programs

Uploaded by

Priya Yannam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

IDML Lab Programs

Uploaded by

Priya Yannam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

Cleaning Data in Pandas and Visualizing with Matplotlib

# Import necessary libraries

import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

# Create a sample DataFrame with missing values

data = {

'Name': ['John', 'Anna', 'Peter', 'Linda', np.nan],

'Age': [28, 22, np.nan, 32, 40],

'Score': [85, 90, np.nan, 78, 95]

df = pd.DataFrame(data)

# Data Cleaning: Remove rows with missing values

df_cleaned = df.dropna()

# Visualize the cleaned data using Matplotlib

plt.figure(figsize=(6, 4))

df_cleaned.plot(kind='bar', x='Name', y='Score', legend=False)

plt.title('Scores of Students')

plt.xlabel('Student Name')

plt.ylabel('Score')

plt.show()

2. Analyzing Data in Pandas


# Import pandas and numpy libraries

import pandas as pd

import numpy as np

# Create a sample DataFrame

data = {

'Name': ['John', 'Anna', 'Peter', 'Linda', np.nan],

'Age': [28, 22, np.nan, 32, 40],

'Score': [85, 90, np.nan, 78, 95]

df = pd.DataFrame(data)

# Descriptive statistics

print("Descriptive statistics:\n", df.describe())

# Calculate mean, median, and standard deviation

mean_age = df['Age'].mean()

median_score = df['Score'].median()

std_age = df['Age'].std()

print("\nMean Age:", mean_age)

print("Median Score:", median_score)

print("Standard Deviation of Age:", std_age)

# Grouping by 'Name' and aggregating 'Score'

grouped_scores = df.groupby('Name')['Score'].mean()
print("\nGrouped Scores (by Name):\n", grouped_scores)

# Filter rows where 'Age' is greater than 30

filtered_df = df[df['Age'] > 30]

print("\nFiltered DataFrame (Age > 30):\n", filtered_df)

3. Creating a Simple Pandas DataFrame

# Import pandas library

import pandas as pd

# Create a simple DataFrame

data = {

'Name': ['Alice', 'Bob', 'Charlie'],

'Age': [25, 30, 35],

'Score': [88, 92, 85]

df = pd.DataFrame(data)

# Display the DataFrame

print(df)

4. Creating a Filter Array for Even Elements

# Import numpy library

import numpy as np

# Original array

arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])


# Filter even elements

even_elements = arr[arr % 2 == 0]

# Display the even elements

print("Even elements in the array:", even_elements)

5. Normal Distribution and Poisson Distribution

# Import numpy and matplotlib libraries

import numpy as np

import matplotlib.pyplot as plt

# Generate a normal distribution

mean, std_dev = 0, 1 # Mean and standard deviation

normal_data = np.random.normal(mean, std_dev, 1000)

# Plot normal distribution

plt.figure(figsize=(6, 4))

plt.hist(normal_data, bins=30, density=True)

plt.title('Normal Distribution')

plt.xlabel('Value')

plt.ylabel('Density')

plt.show()

# Generate a Poisson distribution

lambda_val = 5 # Rate of occurrence


poisson_data = np.random.poisson(lambda_val, 1000)

# Plot Poisson distribution

plt.figure(figsize=(6, 4))

plt.hist(poisson_data, bins=15, density=True)

plt.title('Poisson Distribution')

plt.xlabel('Occurrences')

plt.ylabel('Probability')

plt.show()

6. Convert 1D Array to 3D Array

# Import numpy library

import numpy as np

# 1D array with 8 elements

arr_1d = np.array([1, 2, 3, 4, 5, 6, 7, 8])

# Reshape to a 3D array with shape 2x2x2

arr_3d = arr_1d.reshape(2, 2, 2)

# Display the 3D array

print("3D Array:\n", arr_3d)

You might also like