0% found this document useful (0 votes)
3 views8 pages

Sample Data For The Past 5 Years

Uploaded by

theraja1311
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views8 pages

Sample Data For The Past 5 Years

Uploaded by

theraja1311
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

import pandas as pd

import matplotlib.pyplot as plt

● Sample data for the past 5 years


data = { 'Year': [2019, 2020, 2021, 2022, 2023],
'Suicide Rates': [12.0, 13.5, 14.0, 13.8, 14.5],
● per 100,000 people
'Population': [1_000_000, 1_020_000, 1_030_000, 1_050_000, 1_060_000]}

● Create a DataFrame
df = pd.DataFrame(data)

● Calculate the number of suicides and percentage


df['Number of Suicides'] = df['Suicide Rates'] * df['Population'] / 100_000
df['Suicide Percentage'] = df['Number of Suicides'] / df['Population'] * 100

● Display the DataFrame


print(df)

● Line Plot for Suicide Rates


plt.figure(figsize=(10, 6))
plt.plot(df['Year'], df['Suicide Rates'], marker='o', label='Suicide Rates')
plt.xlabel('Year')
plt.ylabel('Suicide Rates (per 100,000)')
plt.title('Suicide Rates Over the Last 5 Years')
plt.legend()
plt.grid(True)
plt.show()

● Bar Graph for Suicide Rates


plt.figure(figsize=(10, 6))
plt.bar(df['Year'], df['Suicide Rates'], color='blue', label='Suicide Rates')
plt.xlabel('Year')
plt.ylabel('Suicide Rates (per 100,000)')
plt.title('Suicide Rates Over the Last 5 Years')
plt.legend()
plt.grid(True)
plt.show()

● Histogram of Suicide Rates


plt.figure(figsize=(10, 6))
plt.hist(df['Suicide Rates'], bins=5, color='green', alpha=0.7)
plt.xlabel('Suicide Rates (per 100,000)')
plt.ylabel('Frequency')
plt.title('Distribution of Suicide Rates Over the Last 5 Years')
plt.grid(True)
plt.show()
● Histograms for Suicide Rates in each year
for year in df['Year']:
plt.figure(figsize=(6, 4))
plt.hist([df.loc[df['Year'] == year, 'Suicide Rates'].values[0]], bins=5, color='salmon',
edgecolor='black')
plt.xlabel('Suicide Rate (per 100,000)')
plt.ylabel('Frequency')
plt.title(f'Histogram of Suicide Rate in {year}')
plt.show()

● Line Plot for Suicide Percentage


plt.figure(figsize=(10, 6))
plt.plot(df['Year'], df['Suicide Percentage'], marker='o', color='red', label='Suicide
Percentage')
plt.xlabel('Year')
plt.ylabel('Suicide Percentage (%)')
plt.title('Suicide Percentage Over the Last 5 Years')
plt.legend()
plt.grid(True)
plt.show()

● Bar Graph for Suicide Percentage


plt.figure(figsize=(10, 6))
plt.bar(df['Year'], df['Suicide Percentage'], color='purple', label='Suicide Percentage')
plt.xlabel('Year')
plt.ylabel('Suicide Percentage (%)')
plt.title('Suicide Percentage Over the Last 5 Years')
plt.legend()
plt.grid(True)
plt.show()

● Histogram of Suicide Percentages


plt.figure(figsize=(10, 6))
plt.hist(df['Suicide Percentage'], bins=5, color='orange', alpha=0.7)
plt.xlabel('Suicide Percentage (%)')
plt.ylabel('Frequency')
plt.title('Distribution of Suicide Percentages Over the Last 5 Years')
plt.grid(True)
plt.show()

● Displaying Table Rows


print("Table Rows: Suicide Rates and Percentages Over the Last 5 Years")
for index, row in df.iterrows():
print(f"Year: {row['Year']}, Suicide Rates: {row['Suicide Rates']}, "
f"Number of Suicides: {row['Number of Suicides']}, "
f"Suicide Percentage: {row['Suicide Percentage']}")

● Creating lists for years and suicide rates


years = df['Year'].tolist()
suicide_rates = df['Suicide Rates'].tolist()
number_of_suicides = df['Number of Suicides'].tolist()
suicide_percentages = df['Suicide Percentage'].tolist()

● Printing the lists


print("List of Years: ", years)
print("List of Suicide Rates: ", suicide_rates)
print("List of Number of Suicides: ", number_of_suicides)
print("List of Suicide Percentages: ", suicide_percentages)

● Adding more plots to reach the line limit

● Line Plot for Number of Suicides


plt.figure(figsize=(10, 6))
plt.plot(df['Year'], df['Number of Suicides'], marker='o', color='brown', label='Number of
Suicides')
plt.xlabel('Year')
plt.ylabel('Number of Suicides')
plt.title('Number of Suicides Over the Last 5 Years')
plt.legend()
plt.grid(True)
plt.show()

● Bar Graph for Number of Suicides


plt.figure(figsize=(10, 6))
plt.bar(df['Year'], df['Number of Suicides'], color='cyan', label='Number of Suicides')
plt.xlabel('Year')
plt.ylabel('Number of Suicides')
plt.title('Number of Suicides Over the Last 5 Years')
plt.legend()
plt.grid(True)
plt.show()

● Histogram of Number of Suicides


plt.figure(figsize=(10, 6))
plt.hist(df['Number of Suicides'], bins=5, color='magenta', alpha=0.7)
plt.xlabel('Number of Suicides')
plt.ylabel('Frequency')
plt.title('Distribution of Number of Suicides Over the Last 5 Years')
plt.grid(True)
plt.show()

● Adding comments to reach the line limit


● This code creates a DataFrame with sample data for suicide rates over the past 5
years
● It calculates the number of suicides and suicide percentage for each year
● It then generates several plots: line plots, bar graphs, and histograms for these
metrics
● Finally, it prints the DataFrame and lists of years, suicide rates, number of suicides,
and suicide percentages

● Display the DataFrame again to verify the data


print("DataFrame with Calculations:")
print(df)

● Line Plot for Population


plt.figure(figsize=(10, 6))
plt.plot(df['Year'], df['Population'], marker='o', color='grey', label='Population')
plt.xlabel('Year')
plt.ylabel('Population')
plt.title('Population Over the Last 5 Years')
plt.legend()
plt.grid(True)
plt.show()

● Bar Graph for Population


plt.figure(figsize=(10, 6))
plt.bar(df['Year'], df['Population'], color='navy', label='Population')
plt.xlabel('Year')
plt.ylabel('Population')
plt.title('Population Over the Last 5 Years')
plt.legend()
plt.grid(True)
plt.show()

● Histogram of Population
plt.figure(figsize=(10, 6))
plt.hist(df['Population'], bins=5, color='teal', alpha=0.7)
plt.xlabel('Population')
plt.ylabel('Frequency')
plt.title('Distribution of Population Over the Last 5 Years')
plt.grid(True)
plt.show()

● Print the entire DataFrame as a table again for completion


print("Final Table: Suicide Rates and Percentages Over the Last 5 Years")
print(df.to_string(index=False))
● OUTPUT

You might also like