bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/Data Science/Data Science
Data Science•Data Science

Data Science - Data Preparation

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind Data Science - Data Preparation?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

___ pandas as pd
3Order

Put the learning moves in the order that makes the concept easiest to apply.

Before analyzing data, a Data Scientist must extract the data, and make it clean and valuable.
Extract and Read Data With Pandas
Data Science - Data Preparation
4Data move

Before charting or modeling a dataset, which move should come first?

Before analyzing data, a Data Scientist must extract the data, and make it clean and valuable.

Extract and Read Data With Pandas

Before data can be analyzed, it must be imported/extracted.

In the example below, we show you how to import data using Pandas in Python.

We use the read_csv() function to import a CSV file with the health data:

Example

import pandas as pd
health_data = pd.read_csv("data.csv", header=0, sep=",")

print(health_data)

Example Explained

  • Import the Pandas library
  • Name the data frame as health_data .
  • header=0 means that the headers for the variable names are to be found in the first row (note that 0 means the first row in Python)
  • sep="," means that "," is used as the separator between the values. This is because we are using the file type .csv (comma separated values)

Tip

If you have a large CSV file, you can use the head() function to only show the top 5rows:

Example

import pandas as pd
health_data = pd.read_csv("data.csv", header=0, sep=",")

print(health_data.head())

Data Cleaning

Look at the imported data. As you can see, the data are "dirty" with wrongly or unregistered values:

  • There are some blank fields
  • Average pulse of 9 000 is not possible
  • 9 000 will be treated as non-numeric, because of the space separator
  • One observation of max pulse is denoted as "AF", which does not make sense

So, we must clean the data in order to perform the analysis.

Remove Blank Rows

We see that the non-numeric values (9 000 and AF) are in the same rows with missing values.

Solution: We can remove the rows with missing observations to fix this problem.

When we load a data set using Pandas, all blank cells are automatically converted into "NaN" values.

So, removing the NaN cells gives us a clean data set that can be analyzed.

We can use the dropna() function to remove the NaNs. axis=0 means that we want to remove all rows that have a NaN value:

Example

health_data.dropna(axis=0,inplace=True)
print(health_data)

The result is a data set without NaN rows:

Data Categories

To analyze data, we also need to know the types of data we are dealing with.

Data can be split into two main categories:

  • Quantitative Data - Can be expressed as a number or can be quantified. Can be divided into two sub-categories: Discrete data : Numbers are counted as "whole", e.g. number of students in a class, number of goals in a soccer game Continuous data : Numbers can be of infinite precision. e.g. weight of a person, shoe size, temperature
  • Qualitative Data - Cannot be expressed as a number and cannot be quantified. Can be divided into two sub-categories: Nominal data : Example: gender, hair color, ethnicity Ordinal data : Example: school grades (A, B, C), economic status (low, middle, high)
  • Discrete data : Numbers are counted as "whole", e.g. number of students in a class, number of goals in a soccer game
  • Continuous data : Numbers can be of infinite precision. e.g. weight of a person, shoe size, temperature
  • Nominal data : Example: gender, hair color, ethnicity
  • Ordinal data : Example: school grades (A, B, C), economic status (low, middle, high)

By knowing the type of your data, you will be able to know what technique to use when analyzing them.

Data Types

We can use the info() function to list the data types within our data set:

Example

print(health_data.info())

Result

We see that this data set has two different types of data:

  • Float64
  • Object

We cannot use objects to calculate and perform analysis here. We must convert the type object to float64 (float64 is a number with a decimal in Python).

We can use the astype() function to convert the data into float64.

The following example converts "Average_Pulse" and "Max_Pulse" into data type float64 (the other variables are already of data type float64):

Example

health_data["Average_Pulse"]
= health_data['Average_Pulse'].astype(float)
health_data["Max_Pulse"] =
health_data["Max_Pulse"].astype(float)
print
(health_data.info())

Result

Now, the data set has only float64 data types.

Analyze the Data

When we have cleaned the data set, we can start analyzing the data.

We can use the describe() function in Python to summarize data:

Example

print(health_data.describe())

Result

DurationAverage_PulseMax_PulseCalorie_BurnageHours_WorkHours_Sleep
Count10.010.010.010.010.010.0
Mean51.0102.5137.0285.06.67.5
Std10.4915.411.3530.283.630.53
Min30.080.0120.0240.00.07.0
25%45.091.25130.0262.57.07.0
50%52.5102.5140.0285.08.07.5
75%60.0113.75145.0307.58.08.0
Max60.0125.0150.0330.010.08.0
  • Count - Counts the number of observations
  • Mean - The average value
  • Std - Standard deviation (explained in the statistics chapter)
  • Min - The lowest value
  • 25% , 50% and 75% are percentiles (explained in the statistics chapter)
  • Max - The highest value

Previous

Data Science Functions

Next chapter

DS Math

Start with Data Science - Linear Functions