<jupyter_start><jupyter_text>Moringa Data Science Prep Group Project: Week 5  Importing our Libraries<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
% matplotlib inline<jupyter_output><empty_output><jupyter_text>Reading Our Dataset From CSV<jupyter_code># load the dataset into csv
df = pd.read_csv('Life_Expectancy_Data.csv')
df<jupyter_output><empty_output><jupyter_text>Previewing our Dataset<jupyter_code># preview the first 5 rows of the dataset
df.head()<jupyter_output><empty_output><jupyter_text>Accessing Information About Our Dataset<jupyter_code># accessing the info
df.info()
df.describe()
#confirm if there are any null values
df.isnull().values.any()
#Identifying the cloumns with nulls values
df.isnull().sum()
#Calculating the total number of cells with nulls values
df.isnull().sum().sum()<jupyter_output><empty_output><jupyter_text>Cleaning the Dataset  Uniformity<jupyter_code># Procedure 1 
# Data cleaning actions :  Standardizing the columns by lowering the column names and removig the white spaces in the dataset columns
df.columns = df.columns.str.strip().str.lower().str.replace(' ','_').str.replace('(', '').str.replace(')','')
df.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 2938 entries, 0 to 2937
Data columns (total 22 columns):
 #   Column                           Non-Null Count  Dtype  
---  ------                           --------------  -----  
 0   country                          2938 non-null   object 
 1   year                             2938 non-null   int64  
 2   status                           2938 non-null   object 
 3   life_expectancy                  2928 non-null   float64
 4   adult_mortality                  2928 non-null   float64
 5   infant_deaths                    2938 non-null   int64  
 6   alcohol                          2744 non-null   float64
 7   percentage_expenditure           2938 non-null   float64
 8   hepatitis_b                      2385 non-null   float64
 9   measles                          2938 non-null   int64  
 10  bmi                              2904 non-null   float64
 11  under-five_deaths                2938 non-null   int64  
 12  polio               [...]<jupyter_text>Validity<jupyter_code># Procedure 1 
# Data cleaning actions : Dropping irrelevant attributes 

df.drop(['percentage_expenditure','hepatitis_b','under-five_deaths','total_expenditure','diphtheria','gdp','population','polio','thinness__1-19_years',
         'thinness_5-9_years','income_composition_of_resources','schooling'], axis=1, inplace=True)
df.head()

#finding the number of null values after dropping unnecessary columns
df.isnull().sum()<jupyter_output><empty_output><jupyter_text>Completeness<jupyter_code># checking for missing values
df.isnull().sum().sum()
# Procedure 1
# Data cleaning action: fillfoward all the empty cells
df1=df.fillna(0, inplace = True)
df1=df.fillna(method='ffill')
df1

# checking the size of the dataset
df1.shape
# Procedure 2 # tried to drop the rows entirely when I used country as column index
# Data cleaning action: Deleting rows that would skew our data
# Explanation: This dataset records attributes of each country in a span of 15 years. We delete countries that do not meet this condition
# 

df1a=df1.drop(df1.index[[624, 769, 1650, 1715, 1812, 1909, 1958, 2167, 2216, 2713]])
df1a
#finding the number of null values after dropping unnecessary columns and rows
df1a.isnull().sum()
# checking for duplicates
df1a.duplicated().head(10)<jupyter_output><empty_output><jupyter_text>Exporting the cleaned dataset<jupyter_code>df1a.to_csv('Clean_lifestyle')<jupyter_output><empty_output><jupyter_text>Data Analysis<jupyter_code># Question 1
# How does life expectancy compare between developed and developing?
#df2=df1.groupby(['life_expectancy'])['status'].value_counts().sort_values(ascending=False)
#df2

dfa = df1a[['status','life_expectancy']].groupby(['status'])['life_expectancy'].mean()
dfa
# Question 2
# Is the global yearly rate of life expectancy increasing or decreasing?
df3=df1a.groupby(['year'])['life_expectancy'].mean().sort_values(ascending=True)
df3
df3.plot.bar()
# Question 3
# Does life expectancy have a positive or negative correlation with alcohol consumption?
# Am not sure this is excatly how correlation should be done
#df3=df1.groupby(['life_expectancy'])['alcohol'].count().sort_values(ascending=False)
#df3

dfb = df1a['life_expectancy'].corr(df['alcohol'])
dfb
# Question 4
# Which are the top 10 countries with the highest 
# a) adult mortality 
df8=df1a.groupby(['country'])['adult_mortality'].mean().sort_values(ascending=False)
df8.head(10)
# Question 4
# Which are the top 10 countries with the highest
#b) Infant mortality rate?
df9=df1a.groupby(['country'])['infant_deaths'].mean().sort_values(ascending=False)
df9.head(10)
# Question 5a
# Which are the 10 most affected countries by measles disease? (Measles - number of reported cases per 1000 population)
df5a=df1a.groupby(['country'])['measles'].mean().sort_values(ascending=False).head(10)
df5a


df5a.plot.bar()
# Question 5b
# Which are the 10 most affected countries by HIV/AIDS diseases?(Deaths per 1 000 live births HIV/AIDS (0-4 years))
df5b=df1a.groupby(['country'])['hiv/aids'].mean().sort_values(ascending=False).head(10)
df5b
df5b.plot.bar()
# Question 6
# What is the relationship between the health of the citizens and their life expectancy?

df6 = df1a['life_expectancy'].corr(df['bmi'])
df6
# Question 7
#	Which countries have the lowest life expectancy rate?
df7=df1a.groupby(['country'])['life_expectancy'].mean().sort_values(ascending=True).head(15)
df7
df7.plot.bar()
# What was the lowest life expectancy in the developing countries?
df1a.nsmallest(2,'life_expectancy')
df1a.nlargest(2,'life_expectancy')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11139718503261242, 'fraction_numerical': 0.040336422931685545, 'mean_word_length': 4.174955595026643, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '405424', 'license': 'Unlicense', 'n_tokens_mistral': 2013, 'n_tokens_neox': 1826, 'n_words': 506, 'path': 'FINAL_Combined_Moringa_group4_project_Wk5.ipynb', 'repo_name': 'sharon15-netizen/Group4', 'size': '5826'}