Introduction to Pandas/NumPy
Pandas and Numpy using Google Colab.
import pandas as pd
import numpy as np
import matplotlib as plt
url = "https://raw.githubusercontent.com/daddyawesome/PythonStat/master/Basics/data_test_loan.csv" #the url is where the file is being uploaded
df = pd.read_csv(url) #Reading the dataset in a dataframe using Pandas
df.head(10)
This should print 10 rows. Alternately, you can also look at more rows by printing the dataset. Next, you can look at summary of numerical fields by using describe()
function
df.describe()
df.describe() #get summary of numerical variables
describe()
function would provide count, mean, standard deviation (std), min, quartiles and max in its output
Here are a few inferences, you can draw by looking at the output of describe() function:
- LoanAmount has (614 – 592) 22 missing values.
- Loan_Amount_Term has (614 – 600) 14 missing values.
- Credit_History has (614 – 564) 50 missing values.
- We can also look that about 84% applicants have a credit_history. How? The mean of Credit_History field is 0.84 (Remember, Credit_History has value 1 for those who have a credit history and 0 otherwise)
- The ApplicantIncome distribution seems to be in line with expectation. Same with CoapplicantIncome
Please note that we can get an idea of a possible skew in the data by comparing the mean to the median, i.e. the 50% figure.
For the non-numerical values (e.g. Property_Area, Credit_History etc.), we can look at frequency distribution to understand whether they make sense or not. The frequency table can be printed by following command:
df['Property_Area'].value_counts()
Similarly, we can look at unique values of port of credit history. Note that dfname['column_name'] is a basic indexing technique to access a particular column of the dataframe. It can be a list of columns as well.
For more information, refer to the "10 Minutes to Pandas" resource shared above.
Distribution analysis
Now that we are familiar with basic data characteristics, let us study distribution of various variables. Let us start with numeric variables - namely ApplicantIncome and LoanAmount
Lets start by plotting the histogram of ApplicantIncome using the following commands:
df['ApplicantIncome'].hist(bins=50)
df['ApplicantIncome'].hist(bins=50)
Here we observe that there are few extreme values. This is also the reason why 50 bins are required to depict the distribution clearly. Next, we look at box plots to understand the distributions. Box plot for fare can be plotted by:
df.boxplot(column='ApplicantIncome')
df.boxplot(column='ApplicantIncome')
This confirms the presence of a lot of outliers/extreme values. This can be attributed to the income disparity in the society. Part of this can be driven by the fact that we are looking at people with different education levels. Let us segregate them by Education:
df.boxplot(column='ApplicantIncome', by = 'Education')
df.boxplot(column='ApplicantIncome', by = 'Education')
We can see that there is no substantial different between the mean income of graduate and non-graduates. But there are a higher number of graduates with very high incomes, which are appearing to be the outliers.
Now, Let's look at the histogram and boxplot of LoanAmount using the following command:
df['LoanAmount'].hist(bins=50)
df['LoanAmount'].hist(bins=50)
df.boxplot(column='LoanAmount')