<jupyter_start><jupyter_text>Pandas InstructionsThis assignment will be done completely inside this Jupyter notebook with answers placed in the cell provided.All python imports that are needed shown.Follow all the instructions in this notebook to complete these tasks.    Make sure the CSV data files is in the same folder as this notebook - alumni.csv, groceries.csv<jupyter_code># Imports needed to complete this assignment<jupyter_output><empty_output><jupyter_text>Question 1 :  Import CSV file (1 Mark)Write code to load the alumni csv dataset into a Pandas DataFrame called 'alumni'.<jupyter_code>#q1 (1)
import pandas as pd

alumni= pd.read_csv('alumni.csv')
alumni = alumni.rename(columns={'Savings ()': 'S'})

alumni<jupyter_output><empty_output><jupyter_text>Question 2 :  Understand the data set (5 Marks)Use the following pandas commands to understand the data set: a) head, b) tail, c) dtypes, d) info, e) describe<jupyter_code>#a) (1)
alumni.head()
#b) (1)
alumni.tail()
#c) (1)
alumni.dtypes
#d) (1)
alumni.info()
#e) (1)
alumni.describe()<jupyter_output><empty_output><jupyter_text>Question 3 :  Cleaning the data set - part A (3 Marks)a) Use clean_currency method below to strip out commas and dollar signs from Savings ($) column and put into a new column called 'Savings'.<jupyter_code>alumni = alumni.rename(columns=lambda x: x.replace('$', ''))
alumni = alumni.rename(columns={'Savings ()': 'Savings'})
alumni['Savings'] = alumni['Savings'].str.replace(',', '')
alumni['Savings'] = alumni['Savings'].str.replace('$', '')
alumni['Savings'].astype(str).astype(int)
alumni['Savings'] = pd.to_numeric(alumni['Savings'])
alumni
alumni.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 88 entries, 0 to 87
Data columns (total 8 columns):
 #   Column          Non-Null Count  Dtype 
---  ------          --------------  ----- 
 0   Year Graduated  88 non-null     int64 
 1   Gender          88 non-null     object
 2   Marital Status  88 non-null     object
 3   Diploma Type    88 non-null     object
 4   Defaulted       88 non-null     object
 5   Salary          88 non-null     int64 
 6   Fee             88 non-null     int64 
 7   Savings         88 non-null     int64 
dtypes: int64(4), object(4)
memory usage: 5.6+ KB<jupyter_text>b) Uncomment 'alumni.dtypes.Savings' to check that the type change has occurred<jupyter_code>#b) (1)
alumni.dtypes.Savings<jupyter_output><empty_output><jupyter_text>Question 4 :  Cleaning the data set - part B (5 Marks)a) Run the 'alumni["Gender"].value_counts()' to see the incorrect 'M' fields that need to be converted to 'Male'<jupyter_code># a) (1)
alumni["Gender"].value_counts()<jupyter_output><empty_output><jupyter_text>b) Now use a '.str.replace' on the 'Gender' column to covert the incorrect 'M' fields. Hint: We must use ^...$ to restrict the pattern to match the whole string.<jupyter_code>#Using update works the same way to restrict and ensure there's isn't duplicate when converting 'M' to 'Male'
alumni.update(alumni["Gender"].replace({'M': 'Male'}))<jupyter_output><empty_output><jupyter_text>c) That didn't the set alumni["Gender"] column however. You will need to update the column when using the replace command 'alumni["Gender"]=', show how this is done below<jupyter_code># c) (1)
alumni['Gender'] = alumni['Gender'].replace(['M'],'Male')<jupyter_output><empty_output><jupyter_text>d) You can set it directly by using the df.loc command, show how this can be done by using the 'df.loc[row_indexer,col_indexer] = value' command to convert the 'M' to 'Male'<jupyter_code>#We find the index of 'M'
missing = alumni[alumni['Gender'].isin(['M'])]
missing
alumni.loc[[28, 35, 84],['Gender']]='Male'
alumni<jupyter_output><empty_output><jupyter_text>e) Now run the 'value_counts' for Gender again to see the correct columns - 'Male' and 'Female'<jupyter_code># e) (1)
alumni["Gender"].value_counts()<jupyter_output><empty_output><jupyter_text>Question 5 :  Working with the data set (4)a) get the median, b) mean and c) standard deviation for the 'Salary' column<jupyter_code># a)(1)
alumni['Salary'].mean()

# b)(1)
alumni['Salary'].median()
# c)(1)
alumni['Salary'].std()<jupyter_output><empty_output><jupyter_text>d) identify which alumni paid more than $15000 in fees, using the 'Fee' column<jupyter_code># d) (1)
alumni[alumni['Fee'] > 15000]<jupyter_output><empty_output><jupyter_text>Question 6 :  Visualise the data set (4 Marks)a) Using the 'Diploma Type' column, plot a bar chart and show its value counts.<jupyter_code>#a) (1)
import matplotlib.pyplot as plt
import seaborn as sns
alumni['Diploma Type'].value_counts().plot(kind='bar')<jupyter_output><empty_output><jupyter_text>b) Now create a box plot comparison between 'Savings' and 'Salary' columns<jupyter_code>#b) (1)

alumni.plot(x='Savings',y='Salary',kind='box')<jupyter_output><empty_output><jupyter_text>c) Generate a histogram with the 'Salary' column and use 12 bins.<jupyter_code>#c) (1)
histogram= alumni.hist(column='Savings')<jupyter_output><empty_output><jupyter_text>d) Generate a scatter plot comparing 'Salary' and 'Savings' columns.<jupyter_code>#d) (1)
import matplotlib.pyplot as plt
import pandas as pd
alumni.plot(kind='scatter',x='Salary',y='Savings',color='black')
plt.show
#alumni['Salary','Savings'].plot(kind='scatter')<jupyter_output><empty_output><jupyter_text>Question 7 :  Contingency Table (2 Marks)Using both the 'Martial Status' and 'Defaulted' create a contingency table. Hint: crosstab<jupyter_code># Q7 (2)
contigency_table=pd.crosstab(alumni['Marital Status'] ,alumni['Defaulted'])
contigency_table<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13450604122245913, 'fraction_numerical': 0.017235252309879174, 'mean_word_length': 5.606807511737089, 'pattern_counts': {'":': 0, '<': 69, '<?xml version=': 0, '>': 70, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '17', 'dir': 'jupyter-structured-clean-dedup', 'id': '286212', 'license': 'MIT', 'n_tokens_mistral': 2107, 'n_tokens_neox': 1923, 'n_words': 558, 'path': 'Pandas Assignment.thon.answer.ipynb', 'repo_name': 'ttwange/ADS-Assignment-1', 'size': '5628'}