There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
You can also find further discussion and get answers to your questions over in Language Help.
Agree with a comment or answer? Like () to up-vote the contribution!
Hello! Opal here~ While looking at the syntax I noticed there seemed to be a section for average age, but it didn’t seem like there was anything in place to visualize the data. I just wanted to know if there was a way to visualize it? (I tried to use a codebyte but I couldn’t figure out what language I was using). I figured it might be best to apply the same bell curve like the one found in Numerical Variable, but I had trouble making it work.
### Average
ages = churn_data.groupby(churn_data["Exited"])["Age"].mean()
print("Looking at statistics \n")
print("The average age of someone who left the bank is", round(ages[0], 2), "years old. \n")
print("The average age of someone who is still with the bank is", round(ages[1], 2), "years old. \n")
### Visualizing a Numerical Variable
sns.displot(churn_data["CreditScore"], kde=False)
plt.title("Analysis of a Numerical Variable")
plt.show()
plt.clf()
This is the whole data set:
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import codecademylib3
# Loading in and showing the data
churn_data = pd.read_csv("churn.csv")
print(churn_data.head())
### Checks for any missing data
print(churn_data.isnull().any())
### Remove columns we do not need
churn_data.drop(["RowNumber","CustomerId","Surname"], axis = 1 , inplace = True)
# show data with removed columns
print(churn_data.head())
### Visualizing a Categorical Variable
sns.countplot(churn_data["Geography"])
plt.title("Analysis of a Categorical Variable")
plt.show()
plt.clf()
### Visualizing a Numerical Variable
sns.displot(churn_data["CreditScore"], kde=False)
plt.title("Analysis of a Numerical Variable")
plt.show()
plt.clf()
### Visualizing the Effect of a Categorical Variable on the Exited Variable
sns.countplot(y = churn_data["Gender"], hue = churn_data["Exited"])
plt.title("Effect of a Categorical Variable on the Exited Variable")
plt.show()
plt.clf()
### Average
ages = churn_data.groupby(churn_data["Exited"])["Age"].mean()
print("Looking at statistics \n")
print("The average age of someone who left the bank is", round(ages[0], 2), "years old. \n")
print("The average age of someone who is still with the bank is", round(ages[1], 2), "years old. \n")
### Creating a Correlation Matrix
var_list = ["CreditScore","Age","Tenure","Balance","NumOfProducts","EstimatedSalary","Exited"]
sns.heatmap(churn_data[var_list].corr(), annot = True)
plt.title("Correlation Matrix")
plt.tight_layout()
plt.show()
plt.clf()