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

Module2_Python_programs

The document provides Python code examples for various data visualization techniques using libraries such as pandas, matplotlib, and seaborn. It includes creating frequency distributions, bar charts, pie charts, histograms, scatter plots, boxplots, line charts, heatmaps, and calculating statistical measures like mean, median, and mode. Each section contains code snippets and descriptions for visualizing data from datasets like 'telco.xlsx' and 'Marks1.csv'.

Uploaded by

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

Module2_Python_programs

The document provides Python code examples for various data visualization techniques using libraries such as pandas, matplotlib, and seaborn. It includes creating frequency distributions, bar charts, pie charts, histograms, scatter plots, boxplots, line charts, heatmaps, and calculating statistical measures like mean, median, and mode. Each section contains code snippets and descriptions for visualizing data from datasets like 'telco.xlsx' and 'Marks1.csv'.

Uploaded by

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

Module-2 Python programs for Data Visualization:

Create Frequency distribution and Calculate Relative Frequency:

import pandas as pd

data = {'Grade': ['A', 'B', 'C', 'B', 'A', 'C', 'B', 'A', 'A', 'C',

'B', 'A', 'C', 'B', 'C', 'B', 'A', 'C', 'B', 'A']}

df = pd.DataFrame(data)

grade_frequency = df['Grade'].value_counts()

grade_relative_frequency = df['Grade'].value_counts(normalize=True)

print("Frequency Distribution of Grades:")

print(grade_frequency)

print("\nRelative Frequency of Grades:")

print(grade_relative_frequency)

Create Bar chart using Python:

import pandas as pd

from matplotlib import pyplot as plt

import seaborn as sns

telco=pd.read_excel("telco.xlsx")

telco.head()

plt.figure(figsize=(15, 8))

ed_plot = sns.countplot(x='ed', data=telco)

ed_plot.bar_label(ed_plot.containers[0])

plt.title("Distribution of Churn")

plt.show()
Create Pie chart using Python:

import pandas as pd

from matplotlib import pyplot as plt

import seaborn as sns

telco=pd.read_excel("telco.xlsx")

telco.head()

region_counts = telco['region'].value_counts()

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

plt.pie(region_counts, labels=region_counts.index, autopct='%1.1f%%', startangle=90,


colors=['blue', 'green','red'])

plt.title("Proportion of region")

plt.show()

Create Histogram using Python:

import pandas as pd

import matplotlib.pyplot as plt

import seaborn as sns

Marks1=pd.read_csv("Marks1.csv")

Marks1.head()

plt.hist(Marks1['Internal1'], bins=5, edgecolor='black')

plt.title('Distribution of Internal1 Marks')

plt.xlabel('Marks')

plt.ylabel('Frequency')

plt.show()
Create Scatter plot using Python:

import pandas as pd

import matplotlib.pyplot as plt

import seaborn as sns

Marks1=pd.read_csv("Marks1.csv")

Marks1.head()

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

sns.scatterplot(data=Marks1, x='Internal1', y='Internal2', color='green', s=100)

plt.title('Relationship b/w Internal1 v/s Internal2')

plt.xlabel('Internal1')

plt.ylabel('Internal2')

plt.show()

Create Boxplot using Python:

import pandas as pd

import matplotlib.pyplot as plt

import seaborn as sns

Marks1=pd.read_csv("Marks1.csv")

Marks1.head()

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

sns.boxplot(y='Internal2', data=Marks1, color='red')

plt.title('Boxplot of Internal1 in Marks1 dataset')

plt.ylabel('marks')

plt.show()
Create Line chart using Python:

import seaborn as sns

import matplotlib.pyplot as plt

Sales=[250,300,260,320,240,400,280,380,270,350,240,290]

months=['Jan','Feb','Mar','Apr','May','June','July','August','Sep','Oct','Nov','Dec']

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

sns.lineplot(x=months,y=Sales,marker='o',color='red')

plt.xlabel('Month')

plt.ylabel('Sales')

plt.title("Sales Over 12 months")

plt.show()

Create Heatmap using Python:

import numpy as np

import seaborn as sns

import matplotlib.pyplot as plt

data = np.random.rand(5, 5)

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

sns.heatmap(data, annot=True, cmap='coolwarm', linewidths=0.5)

plt.title("Heatmap Example")

plt.show()
Python Program to find mean,median and mode:

import pandas as pd

from scipy import stats

df = pd.read_excel("telco.xlsx")

df.head()

mean= df['age'].mean()

print("Mean:", mean)

median= df['age'].median()

print("Median:", median)

mode= df['age'].mode()[0]

print("Mode:", mode)

You might also like