ADS Unit 2 Notes
ADS Unit 2 Notes
Data cleaning is therefore the process of detecting and rectifying faults or inconsistencies in
dataset by scrapping or modifying them to fit the definition of quality data for analysis. It is
an essential activity in data preprocessing as it determines how the data will be used and
processed in other modeling processes.
∙ Improved data quality: It is therefore very important to clean the data as this reduces
the chances of errors, inconsistencies and missing values, which ultimately makes the
data to be more accurate and reliable in the analysis.
∙ Better decision-making: Consistent and clean data gives organization insight into
comprehensive and actual information and minimizes the way such organizations
make decisions on outdated and incomplete data.
∙ Increased efficiency: High quality data is efficient to analyze, model or report on it,
whereas clean data often avoids a lot of foreseen time and effort that goes into
handling poor data quality.
∙ Compliance and regulatory requirements: There are standard policies the industries
and various regulatory authorities set on data quality, and by data cleaning, one can
be able to conform with these standards to avoid penalties and legal endangers.
It is also relevant to mention that issues with the quality of data could be of various origins
including errors made by people, the failures of technical input and data merging issues
among others. Some common data quality issues include:Several common types of data
quality problem are:
∙ Missing values: Lack of some data or missing information can result in failure to make
the right conclusions and can or else lead to creating a biased result.
∙ Duplicate data: Duplicate or twofold variation could possibly result in different data
values and parameters within the set which might produce skewed results.
∙ Incorrect data types: Adjustment 2: Elimination of data fields with wrong data format
conversion Data fields containing values of the wrong data type (for instance string
data type in a numeric data type) can sometimes hamper analysis and cause
inaccuracies.
∙ Outliers and anomalies: Outliers simply refer to observations whose values are
unusually high or low compared to other observations in the same data set ‘outliers
can affect any analysis and some statistical results beyond recognition’.
∙ Inconsistent formats: It is also important to note that data discrepancies like date
formats, capital first letter etc may present challenges when bringing together data.
∙ Spelling and typographical errors: This is due to the reason that the result is depended
on text fields and the misspellings and the typos of the keys are often misinterpreted
or categorized wrongly.
Data cleaning involves several key tasks, each aimed at addressing specific issues within a
dataset. Here are some of the most common tasks involved in data cleaning:
Missing data is a common problem in datasets. Strategies to handle missing data include:
∙ Removing Records: Deleting rows with missing values if they are relatively few and
insignificant.
∙ Imputing Values: Replacing missing values with estimated ones, such as the mean,
median, or mode of the dataset.
2. Removing Duplicates
Duplicates can skew analyses and lead to inaccurate results. Identifying and removing
duplicate records ensures that each data point is unique and accurately represented.
3. Correcting Inaccuracies
Data entry errors, such as typos or incorrect values, need to be identified and corrected. This
can involve cross-referencing with other data sources or using validation rules to ensure data
accuracy.
4. Standardizing Formats
Data may be entered in various formats, making it difficult to analyze. Standardizing formats,
such as dates, addresses, and phone numbers, ensures consistency and makes the data
easier to work with.
Outliers can distort analyses and lead to misleading results. Identifying and addressing
outliers, either by removing them or transforming the data, helps maintain the integrity of
the dataset.
The first step in data cleaning is to assess the quality of your data. This involves checking for:
∙ Missing Values: Identify any blank or null values in the dataset. Missing values can be
due to various reasons such as incomplete data collection, data entry errors, or data
loss during transmission.
∙ Incorrect Values: Check for values that are outside the expected range or are
inconsistent with the data type. For example, a date field with an invalid date or a
numeric field with non-numeric characters.
∙ Inconsistencies in Data Format: Verify that the data format is consistent throughout
the dataset. For instance, ensure that dates are in the same format (e.g., YYYY-MM
DD) and that categorical variables have consistent labels.
By identifying these issues early, you can determine the extent of cleaning required and plan
your approach accordingly.
For example,
2. Missing Values: Row 7 has a missing value in the "Name" column, which could affect
analysis and interpretation.
3. Inconsistent Date Format: The "Date" column contains dates in the format "YYYY-MM
DD", which is consistent, but it's important to ensure consistency across all date
entries.
4. Possible Outlier: The score of 100 in row 7 could be considered as an outlier,
depending on the context of the data and the scoring system used.
Duplicate records can skew analysis results and lead to incorrect conclusions. Deduplication
involves:
∙ Removing Duplicate Records: Once duplicates are identified, remove them from the
dataset to ensure that each data point is unique and accurately represented.
∙ Identifying Redundant Observations: Look for duplicate or identical records that do not
add any new information.
∙ Eliminating Irrelevant Information: Remove any variables or columns that are not
relevant to the analysis or do not provide any useful insights.
Irrelevant data can clutter your dataset and lead to inaccurate analysis. Removing data that
does not contribute meaningfully to your analysis helps streamline the dataset and improve
its overall quality. This step involves:
In the deduplicated DataFrame Rows 1 and 6, which were duplicates, have been removed
from the DataFrame.
∙ Standardizing Data Formats: Ensure that dates, times, and other data types are
consistently formatted throughout the dataset.
The "Date" column has been standardized to the format "YYYY-MM-DD" across all entries.
This ensures consistency in the date format.
Missing data can introduce biases and affect the integrity of your analysis. There are several
strategies to handle missing data:
Imputing Missing Values: Use statistical methods such as mean, median, or mode to fill in
missing values.
Removing Records with Missing Values: If the missing values are extensive or cannot be
imputed accurately, remove the records with missing values.
Choosing the right strategy depends on the nature of your data and the analysis
requirements.
Missing Value Handled: The missing value in the "Name" column (row 7) has been replaced
with "Unknown" to signify that the name is unknown or not available. This helps to maintain
data integrity and completeness.
6. Normalize Data
Data normalization involves organizing data to reduce redundancy and improve storage
efficiency. This typically involves:
∙ Splitting Data into Multiple Tables: Divide the data into separate tables, each storing
specific types of information.
∙ Ensuring Data Consistency: Verify that data is structured in a way that facilitates
efficient querying and analysis.
∙ Remove Outliers: If the outliers are due to data entry errors or are not representative
of the population, remove them from the dataset.
∙ Transform Outliers: If the outliers are valid but extreme, transform them to minimize
their impact on the analysis.
Managing outliers is crucial for obtaining accurate and reliable insights from the data.
∙ Microsoft Excel: Offers basic data cleaning functions such as removing duplicates,
handling missing values, and standardizing formats.
∙ OpenRefine: An open-source tool designed specifically for data cleaning and
transformation.
∙ Python Libraries: Libraries like Pandas and NumPy provide powerful functions for data
cleaning and manipulation.
∙ R: The R programming language offers robust packages for data cleaning, such as dplyr
and tidyr.
Techniques
∙ Data Profiling: Involves examining data to understand its structure, content, and
quality.
∙ Volume of Data: Large datasets can be challenging to clean due to their sheer size.
Efficient techniques and tools are necessary to handle big data cleaning tasks.
∙ Complexity of Data: Data from diverse sources may have different structures and
formats, making it difficult to clean and integrate.
∙ Continuous Process: Data cleaning is not a one-time task but an ongoing process. As
new data is collected, it needs to be continually cleaned and maintained.
To ensure effective and efficient data cleaning, it is recommended to follow these best
practices:To ensure effective and efficient data cleaning, it is recommended to follow these
best practices:
∙ Understand the data: As part of the data cleaning process, one needs to have the
knowledge about the origin of the data, the type of structures that hold or store this
data and the characteristics of the particular domain within which this data resides in
order to be in a good position to determine where potential quality problems could
be arising and the correct type of action that should be taken on them.
∙ Document the process: It is also crucial to keep records of the approaches and
decisions made that form the foundation of cleaning including the steps and
regulations adopted as well as any assumptions made in the process.
∙ Prioritize critical issues: First of all, one should concentrate on the main deliberate
quality problems that might have a systemic effect on the case analysis or decision
making.
∙ Monitor and maintain: Ensure that there is long-term tracking and control of data
quality and that, at certain moments suitable for it, cleaning occurs.
Data integration, data blending, and data joining all start at the same step: combining
multiple sources of data. These techniques differ in the level of standardization in definitions
and nomenclature and where in the process transformations occur. When deciding which
method to use, ask questions such as, Is the extracted data set close to your internal
standards, or does it require a lot of transformation? Does the source regularly produce
duplicates or other issues that need data cleaning?
By understanding the quality of your data sources, your organization will be able to move
closer to your data goals.
Data integration, data joining, and data blending are three terms often used in the modern IT
vernacular. They’re also often confused because differences between them are subtle. A few
factors that are universal: Whether data transforms happen before or after loading into a
repository, it’s often the most cumbersome and effort-intensive step in the process, so
automate what you can.
Here are basic definitions and how they compare with each other.
∙ Data blending: The process of combining multiple data sets into a single data set for
analysis. However, unlike data integration, blended data often combines native data—
that is, data that has not been transformed or cleansed—from multiple sources. For
example, an HR team will use blended data if they combine the current quarter’s
internal hiring metrics with an open-source data set from the government on hiring
trends. Unlike data integration, this example requires the user to clean and
standardize the data after it’s blended.
∙ Data joining: Like data blending, data joining involves combining multiple data sets. The
biggest difference from data blending is that data joining requires that data sets
come from the same source, or at least have some overlap between columns and
definitions. To expand on the HR example above, data joining occurs when the HR
professional takes government-sourced national hiring metrics from the current
quarter, then also downloads the quarterly data from four years ago for comparison.
These data sets come from the same system and use the same format but need to be
joined based on specific data ranges. This joined data can then be further blended
into an internal HR data set, which then requires cleaning and standardizing.
Key Takeaways
∙ Vet data sources with your goals in mind. While you can’t always control the quality of
sources in a big data world, there are steps you can take to make integration easier.
∙ Automate as much of the process as possible. When data will be pulled from sources at
high frequency, tools and scripts will greatly benefit your organization’s integration
effort.
∙ To decide which data integration method works best for your organization, map out all
of the variables involved—sources, hardware, volume.
∙ Continually refine your workflow and standards. Successful data integration requires
continuous process improvement.
Tools evolve, data sources expand, and capabilities improve. That adds up to a constant flow
of opportunities to refine data integration workflows and introduce more robust and
efficient processes.
While every organization has its own specific needs, data integration generally follows a
standard process.
1. A need for combined data is identified, either from an end user request or a decision
made by the organization. This often includes parameters such as date ranges and
other limiting factors.
2. Relevant sources are identified, along with the specific data needed from those
sources.
3. Data is extracted from the sources in native format and combined into a data set.
At this stage, the combined data set can be made available for the requestor to manually
cleanse and analyze, or the data may be normalized by a data scientist or data curator before
it’s provided to the business. Regardless of how it gets there, data sets normally require
additional processes to ensure consistent nomenclature among columns, removal of
duplicate data, correction of inaccurate or erroneous data, fixing incomplete records, and
other tasks.
When these tasks are complete, the data is ready to be loaded into analytics applications,
business intelligence systems, or even simply Excel for the end user to analyze and
manipulate for insights and visualizations.
A goal for IT departments should be to maximize the efficiency of this process. This requires
planning to build automated transformations that minimize manual labor. How organizations
get here, though, depends on many variables: where the data sources come from, whether
those sources are vetted, which fields are prioritized, whether established data rules exist,
and what types of workflows are in place.
Automating as much of the data cleansing process as possible may be the most important
part when using multiple data sources as it enables a self-service environment that gets data
into users’ hands faster.
If it seems like a lot of effort to build a data integration process, that’s because it is. From
vetting sources to crafting and refining a data cleaning workflow, a smooth data integration
process takes care and planning. However, the value quickly becomes apparent.
In business, time has always equaled money. However, in the era of big data, where real-time
information flows in from suppliers and customers across the globe, the importance of that
simple formula has grown exponentially. Circumstances change quickly, and the ups and
downs of business can often be unpredictable. When data is in silos, lines of business looking
to analyze new information or explore innovation opportunities can often feel like they’re
several steps behind. In truth, it feels that way because they are. When business units must
rely on other teams for data extracts and analytics reports, things slow down.
Integrating data from multiple sources removes many manual hurdles. In turn, it opens the
door to a wider range of data sources to uncover hidden insights and make truly data-driven
decisions. This increases both capabilities and efficiency for employees, which in turn drives
innovation and opportunities for the organization. Ultimately, integrating multiple data
sources allows organizations to find new ideas and solutions, pivot quickly, and stay ahead of
the competition.
Successful data integration keeps organizations ahead of the competition, both now and in
the future as data possibilities expand. Getting there, however, requires a combination of
technical configuration and understanding from an organizational perspective. By addressing
these challenges, organizations stand to change the way decisions are made in operations,
sales, finance, manufacturing, and nearly every other department.
Here are some benefits—and hurdles—that need to be overcome for successful data
integration.
Benefits
∙ Unified data. By bringing data together into a single repository, the overall data
acquisition process is simplified and sped up. Instead of different groups working with
disparate data sources, a single unified view creates better organizational alignment
while reducing the resources involved in procuring and processing the data.
∙ Improved collaboration. Because of how data is traditionally stored, various groups may
be working with outdated or slightly different versions of a data set. Use of different
definitions or nomenclature can introduce confusion or lead to erroneous
conclusions. Unifying data allows groups to all work with the same information.
∙ Streamlined operations. When data sharing happens only with manual requests and
preparation, work slows down. Operations teams will benefit from streamlined
processes, centralized data, and fewer manual steps.
∙ Reduced manual errors. Removing manual steps from processes adds efficiency, but it
also reduces overall risk. Fewer manual steps equal fewer opportunities for errors,
such as sending the wrong data set or missing records when copying/pasting.
∙ Improved predictive analytics. The more data sources available to analytics platforms,
the better. Data source consolidation widens analysis possibilities, empowering
creativity and innovation. This creates the immediate benefit of more users taking
control of business analytics and the long-term benefit of building a data-driven
culture.
Challenges
∙ Data compatibility. Data coming in from different sources will almost certainly use
different definitions and nomenclature. The process to clean that up is known as data
transformation, and depending on the state of the original sources, can be unwieldy
and complex unless systemic processes are in place.
∙ Data silos. Groups, including as sales, marketing, finance, and HR, all track data for their
own internal needs. When data is siloed off like this, groups must make manual
requests for access, and even when they receive it, nomenclature and definitions may
vary, creating further hurdles to compatibility.
∙ Legacy systems. Data is generated by a wide range of systems, including legacy tools.
Integrating these sources cleanly into a consolidated repository requires assessing the
state of legacy system output, then figuring out how to make it compatible. Don’t skip
this step; these older systems often contain nuggets of information unique to the
organization.
The work of integrating data from multiple sources involves several steps. Throughout the
process, however, it is important to keep data quality and integrity top of mind, along with
relevant data security and privacy regulations. And, once data is integrated, ensure you have
regular monitoring and maintenance in place to ensure data quality and integrity over time.
Data sources come in many different formats and reside in many locations. Every
organization will have a unique combination of data sources, such as the following:
∙ Flat files: Most databases export data sets in flat files, which format as two dimensional
tables that provide standalone context without any needed reference to
other tables. Popular export formats include CSV and delimited and are generally
easy to transform as needed.
∙ XML and JSON: XML and JSON are common standards for modern data transmission,
particularly for the web and web-based applications. In technical terms, JSON is a
data format, while XML is a language. These differences bring their own specific
considerations such as how XML is structured while JSON parses data faster. For the
purposes of data integration, the most important thing to know is that you'll likely
encounter both if you're taking in data from websites or web-based applications.
∙ APIs: Application programming interfaces (APIs) connect different systems and retrieve
data from multiple sources. APIs allow for real-time data integration and can be
customized to meet specialized integration requirements.
∙ Cloud-based data sources: Some data sets are openly available and updated via cloud.
These types of metrics often come from governmental, educational, or research
sources, where data is made available for researchers to examine further
downstream.
∙ Internet of Things (IoT) devices: IoT devices are constantly collecting information,
sometimes thousands of data points daily. Examples of IoT devices include medical
devices continuously transmitting patient data, smart appliances in your home, and
industrial IoT (IioT) devices that control factories and smart cities. Data from IoT
devices is often uploaded to the cloud to be used by other systems.
Regardless of format and other variables, the most important thing is to identify and select
data sources that contribute to business goals, then examine the best way to integrate them.
Once you’ve identified your data sources, it’s time to see how their data sets are formatted
and defined. There are two major preparation steps.
∙ Data cleaning: Data sets may contain incomplete or duplicate records, corrupt sections,
or other issues. Data cleaning is the process of scrubbing the data set to gain a
complete set of workable records.
∙ Standardization: While data cleaning removes problematic records, it does not address
the issue of standardization. When integrating data, the process goes smoothest and
results are best when standards rules—including date formatting, taxonomy, and
metadata fields—are defined and applied. Before proceeding with integration, have
records conform to standards as much as possible. That will minimize downstream
work while increasing timeliness and accuracy.
∙ Transformation techniques: You can use a range of techniques and practices for data
transformation. These include data smoothing, algorithmically reducing noise within
a data set; data normalization, scaling data within a workable range; data
generalization,
establishing a hierarchy among fields; and data manipulation, identifying patterns to
create workable formats.
What works best depends on the state of the individual data sets and your organizational
goals. But one universal truth is that cleaning and standardization work best when processes
are automated. By using tools to assist in data preparation, the entire process can be hands
off. Now, IT staff can focus on flagged events rather than manual efforts to address every
data set as it comes in. Low-code and no-code tools can drive simplified transformation,
while custom scripting and coding can bring more flexibility to the process.
Your integration method will play a big part in defining your overall IT structure for data. This
is why it’s critical to align your resources and your business goals with the chosen method,
including whether you want to build a system with continuous integration or periodic
refreshes set at intervals. The following are some of the most common data integration
methods:
∙ Manual: Manual data integration does not mean someone is physically clicking through
every data field. However, it does require someone to write code to handle each step
of the process. While it’s cumbersome and time-consuming, there are some
situations where manual integration is the most viable option due to the quality of
sources or organizational resource realities.
∙ Change data capture (CDC): CDC is a process that reduces resource use while keeping
data sets updated. CDC ingests changes to a record and makes updates in near real
time rather than refreshing the entire data set at periodic intervals. Because updates
occur individually and in small, quick bursts, CDC does not impact database uptime or
result in resource usage spikes.
∙ Data replication: Data replication keeps an original version of the data at its source and
creates a copy (replica) for use by groups. This copy may be only a fraction of the
record, such as select columns or some other subset used for manipulation.
Replication can bog down resources if too many versions need to be kept over the
long run.
∙ Data virtualization: With data virtualization, all data sets remain in their original
databases. Transformation and manipulation occur in a virtual layer using federation
to point to individual records without actually pulling them into a new file.
∙ Stream data integration (SDI): SDI works as a real-time version of ELT processing.
Streams of data are sent from sources and transformed in real time before being sent
to a repository. This has two major benefits. First, by continuously updating records,
data sets are always kept up to date. Second, this removes the need for refreshing
data sets at scale, stabilizing resource usage. However, SDI also creates an
infrastructure challenge to functionally support the process and qualify data as it
comes in.
The process starts by identifying your data elements and data sources, then mapping the
relationships among them. What cleanly overlaps? Where are columns and definitions
different? And what needs to be done to align them?
From here, you’ll build a model for data transformation. You might use custom scripts,
prebuilt industry tools, or a combination, depending on your needs and available resources.
The goal is to transform and merge data into a common format and resolve any conflicts
between data sources, preferably in a systemic way to make the process repeatable and limit
the work data consumers need to do.
During this process, a range of integration tools and technologies are available to data
curators and data engineers. These include ETL tools that work in three main stages.
∙ Extract data from data sources, such as smartphone apps, databases, web applications,
and software applications.
∙ Transform data from data sources to meet internal standards for definitions,
nomenclature, and taxonomy.
∙ Load transformed data into a data warehouse, data lake, or other repository accessible
by tools such as business intelligence or self service analytics.
A range of ETL tools are available across formats and platforms. In addition to traditional ETL
software applications, cloud based ETL tools allow for flexible access because these tools can
more easily connect disparate sources and repositories. Assuming you have the right IT
expertise, open source ETL tools can provide robust features for a low upfront cost. However,
they may not have the same level of feature development, security, or quality assurance as
commercial products, and that can require a further investment of resources down the road.
Custom ETL tools are available, though they often require a steep upfront investment.
How do you know which ETL tool is right for your organization? Factors to consider include
the types of connectors supported, the level of customization available, performance and
resource requirements, and the full costs including expertise and supporting infrastructure.
Maybe most importantly, ETL tools should be assessed for automation capabilities, because
automation is a critical part of the systemic data transformations that ultimately lead to self
service data analytics.
A data set’s quality refers to its completeness, accuracy, timeliness, and conformity with
standards. It’s difficult to overstate the importance of data quality in integrated data. A high
quality data set requires far less effort to make it ready for integration. While this is
important from a resource perspective, data quality also significantly impacts the output. For
example, if an organization uses four significant digits in its calculations but an outside
source provides data with only two significant digits, that data does not meet the expected
quality level. If it’s used, the resulting analysis may contain flawed insights.
How to measure and maintain data quality: A number of methods are helpful to ensure high
data quality.
∙ Data cleansing: Cleaning a data set to correct and remove duplicate, empty, inaccurate,
or corrupt entries so data sets are ready for processing.
∙ Data matching: This involves matching records across different data sets to verify they
reflect the same subject while also flagging duplicate records for removal.
∙ Data validation: Verifying the accuracy and quality of data by checking that it works
within given rules through a series of checks and parameters.
∙ Data governance: The process of monitoring data to ensure that storage, security,
acquisition, and other tasks meet standards and principles set by the organization as
well as any regulations that might apply.
∙ Continual monitoring: The use of various tools to continuously check the health of data
sets based on internal standards and governance criteria.
Data Transformation: Encoding, Binning, Aggregation
Encoding
Encoding is the process of converting data, especially categorical variables, into numerical
formats that algorithms can interpret. Many machine learning models require numerical
input.
∙ Label Encoding: Assigns a unique integer to each category of an ordinal variable while
maintaining the order.
∙ One-Hot Encoding: Creates a new binary column for each category, where a 1 indicates
the presence of that category and a 0 indicates its absence. This is often used for
nominal (unordered) data to prevent the model from assuming an inherent order.
Binning
∙ Equal-Width Binning: Divides the range of the data into a specified number of bins of
equal size.
∙ Equal-Frequency Binning: Divides the data so that each bin contains roughly the same
number of data points.
Aggregation
Data imputation techniques are methods used to fill in missing values in a dataset. They
range from simple, basic approaches to more sophisticated, model-based methods like
K-Nearest Neighbors (KNN) and iterative imputation.
Simple Imputation
Simple imputation involves replacing missing values with a basic statistical measure from the
existing data.
∙ Mode/Most Frequent Imputation: Missing values (especially for categorical data) are
replaced with the mode, which is the most frequently occurring value in the column.
∙ Constant Value Imputation: Missing data points are replaced with a predefined
constant (e.g., 0 or "Unknown").
Pros:
∙ Doesn't introduce undue bias if the number of missing values is very small.
Cons:
∙ Can lead to an underestimate of errors and reduces the variance of the data, potentially
biasing the model.
KNN imputation is a more advanced, multivariate technique that leverages the similarity
between data points to estimate missing values.
∙ How it works: For each data point with a missing value, the algorithm finds the k most
similar data points (neighbors) based on a distance metric (commonly Euclidean
distance) using the other available features. The missing value is then imputed using
the mean (for numerical data) or the majority vote (for categorical data) of its k
nearest neighbors.
Pros:
∙ Preserves relationships between features and considers the inherent structure of the
dataset.
∙ Often more accurate than simple methods, especially when there is a strong correlation
between features.
Cons:
∙ Can be computationally expensive for large datasets due to the need to calculate
distances between all data points.
∙ Performance depends heavily on the choice of the k parameter and the distance metric
used.
Iterative Imputation
∙ How it works:
1. Missing values are first filled using a simple method (e.g., mean imputation).
2. The algorithm then cycles through each feature with missing data. For a given
feature, it treats that feature as the target variable and uses the other features
as predictors to build a model (e.g., Bayesian Ridge regression, by default in
scikit-learn).
3. The missing values in the target feature are replaced with predictions from the
model.
4. Steps 2 and 3 are repeated for all features, and the entire process is repeated
for several cycles (iterations) until the imputed values converge (become
stable).
Pros:
∙ Superior for handling complex relationships between variables and generally provides
more accurate imputations, especially with many missing values.
∙ Reduces uncertainty by generating multiple plausible datasets (in the full MICE
implementation).
Cons:
∙ More computationally expensive and complex to apply correctly than simple or KNN
methods.
∙ Requires expertise to configure parameters, such as the estimator type and number of
iterations.
Here’s a basic code example using Python and the popular KNNImputer from scikit-learn:
import pandas as pd
# Sample dataset
df = [Link](data)
imputer = KNNImputer(n_neighbors=2)
print(df_imputed)
This code fills in missing values by finding the two closest rows based on Euclidean distance
and taking an average of their values. You can also experiment with parameters like
n_neighbors (number of neighbors to consider) and weights (uniform or distance-based).
essential data wrangling tasks using the core Python libraries: pandas, NumPy, and
missingno.
Prerequisites
To follow this guide, you should have Python installed along with the necessary libraries:
bash
The first step in any data-wrangling workflow is loading and inspecting your dataset. We'll
use pandas for this.
python
import pandas as pd
try:
df = pd.read_csv('[Link]')
except FileNotFoundError:
# If the file is not available locally, we can load a sample from an online source
data_url = '[Link]
df = pd.read_csv(data_url)
# Display the first few rows
print([Link]())
print("\nDataFrame Info:")
[Link]()
The [Link]() output is crucial for identifying missing data and incorrect data types (e.g., a
numerical column read as a string).
The missingno library provides a visually intuitive way to understand the distribution and
patterns of missing data in your dataset.
python
[Link](df)
[Link]()
[Link](df)
[Link]()
∙ [Link](df): This matrix plot displays where data is present (dark lines) and
where it is missing (white gaps), which can reveal patterns like entire rows or
columns being incomplete.
∙ [Link](df): This bar chart gives a simple count of non-null entries for each
column.
If the amount of missing data is small or random, you can drop rows or columns
entirely. python
df_dropped_rows = [Link]()
# Drop columns where most values are missing (e.g., dropping columns with more than 70%
missing data is better handled with thresholding)
# Example: Drop the 'Cabin' column as it has many missing values in the Titanic
print(df_no_cabin.info())
Often, removing data is too aggressive. You can fill missing values using a calculated statistic
(mean, median, mode) or a static value. This often involves NumPy functions.
python
import numpy as np
# Use the mean to fill missing 'Age' values
age_mean = df['Age'].mean()
df['Age'] = df['Age'].fillna(age_mean)
with Pandas
Ensure columns are the correct type (e.g., converting a numeric ID stored as a string to an
integer).
python
# The 'Pclass' (Passenger Class) is better treated as a categorical variable than a pure integer
df['Pclass'] = df['Pclass'].astype('category')
print(df['Pclass'].dtype)
Create new features from existing ones or apply mathematical transformations using NumPy
operations directly on pandas columns.
python