DEV Community

BoT
BoT

Posted on

Data Cleaning and Visualization with Pandas

Hello everyone, My name is Badal Meher, and I work at Luxoft as a software developer. In this article, we'll explore the importance and method to clean, and visualize the data using the popular pandas library in python programming language.

Introduction

Data is the backbone of decision-making in today’s information-driven world. However, the data must be carefully analyzed and purified before meaningful insights can be reached. The goal of this article is to provide a deeper understanding of data analysis and cleaning using the powerful Pandas library in Python. We’ll explore the importance of clean data and guide you through the entire process, from getting started with pandas to exploring advanced real-world trends.

Data analysis and cleaning

Data analysis is the process of analyzing, editing, transforming, and modeling data to extract useful information, draw conclusions, and support decision making. Data cleaning is an important step in this process, ensuring that the data is accurate, reliable, and ready for analysis.

Importance of clean data for successful analysis

Clean data is essential for accurate insights. Inaccurate or incomplete data can lead to flawed analysis, inaccurate conclusions, and poor decision-making. Thus, data cleansing is the foundation of any successful data analytics project.

Start with pandas

An introduction to the Pandas library in Python

Pandas is a powerful open-source data manipulation and analysis library for Python. Let’s start by setting up Pandas:

Pip installation panda
Enter fullscreen mode Exit fullscreen mode

Now, let’s explore some of the basic functions of pandas:

import panda as pd

# Create a DataFrame
data = { 'name': ['Alice', 'Bob', 'Charlie'],
        ‘Age’: [25, 30, 22], .
        'Salary': [50000, 60000, 45000]}

df = pd.DataFrame(Data) 1.1.
Print(df) .
Enter fullscreen mode Exit fullscreen mode

Reading data from files

Learn how to import data into Pandas DataFrames from files such as CSV, Excel, and SQL.

# Reading data from a CSV file
csv_data = pd.read_csv('employeedata.csv');

# Reading data from an Excel file
excel_data = pd.read_excel('employeedata.xlsx');

# Reading data from a SQL database
sql_data = pd.read_sql('SELECT * FROM employee table', connection);
Enter fullscreen mode Exit fullscreen mode

DataFrame Structure Logic

A detailed description of the DataFrame structure, covering rows, columns and indices. Understanding these patterns is important for effective data processing.

# Access to column rows
# To get the 'Name' column
print (df['Name']) .
# To get the first row
print (df.iloc[0]) .
Enter fullscreen mode Exit fullscreen mode

Find a data structure

Identify the methods necessary to gain insight into your data set, including head(), tail(), describe(), and info().

# Displaying the first 5 rows of the DataFrame
Print(df.of()) .

# Displays summary statistics
print (df.description()) .

# Checking for missing data types and values
Print (df.info()) .
Enter fullscreen mode Exit fullscreen mode

Check for missing values ​​and outliers

# Checked for missing values
print (df.isnull().sum()) .

# Remote objects using box models
df.boxplot(column = 'Salary') .
Enter fullscreen mode Exit fullscreen mode

Data cleaning techniques

Advanced methods for handling missing data, including imputation, extraction, and understanding the impact of analysis.

# Handle missing values ​​using average imputation
df['salary'].fillna(df['salary'].show(), inplace = true);

# Removing rows with missing values
df.dropna(inplace=true) .
Enter fullscreen mode Exit fullscreen mode

Remove duplicate images

Look for ways to identify and eliminate duplicate records to ensure data integrity.

# Duplicate identification is removed
df.drop_duplicates(set = true);
Enter fullscreen mode Exit fullscreen mode

Resolving the data type

Guidance on how to identify and resolve inconsistent data sets for smooth analytics.

# Resolving the data type
df['years'] = df['years'].astype(str)
Enter fullscreen mode Exit fullscreen mode

Advanced data cleaning techniques

Handling data inconsistencies

Methods for dealing with inconsistent data, including standardization and normalization methods.

# Standardization of inconsistent data
df['name'] = df['name'].str.ase()
Enter fullscreen mode Exit fullscreen mode

TextDataCleanup

Techniques for preparing and pre-processing notes, an important skill for working with unstructured data.

# Text data cleanup
df['details'] = df['details'].add(lambda x:re.sub(r'\W', ' ', x))
Enter fullscreen mode Exit fullscreen mode

Combined DataFrames

Guidance on how to combine and combine multiple DataFrames to combine and analyze data from different sources.

# Combined DataFrames
merged_df = pd.merge(df1, df2, on = 'comb_column', how = 'middle');

# DataFrames connector
concatenated_df = pd.concat([df1, df2], arrow = 0);
Enter fullscreen mode Exit fullscreen mode

Addressing common integration issues

Address common challenges when merging datasets, and ensure the integrity of the merged data.

# Handle duplicate colors after merging
merged_df = pd.merge(df1, df2, on = 'common_column', as = 'middle', background = ('_left', '_right'))
Enter fullscreen mode Exit fullscreen mode

Data analysis using pandas

An introduction to statistical analysis using pandas, including measures of centrality, dispersion, and correlation.

# Numbers are in mean, median, standard deviation
Print (df.display()) .
print (df.center()) .
print (df.std()) .

# Calculation of correlation matrix
print (df.corr()) .
Enter fullscreen mode Exit fullscreen mode

Data groups and aggregates

# Groups with 'name' and average salary calculations
grouped_df = df.groupby('name')['reward']. mean ()
Enter fullscreen mode Exit fullscreen mode

Visualizing data with pandas

Using Pandas for basic data visualization

Learn how to create meaningful plots and charts with pandas, increasing your ability to effectively communicate data insights.

# Creating a bar plot showing the list of average salaries
df.groupby('name')['right']. show ( ) plot (attribute = 'bar') .
Enter fullscreen mode Exit fullscreen mode

Creating meaningful plots and charts

In-depth guidance on creating graphical representations, including line graphs, bar plots, and scatter plots.


# Make a scatter plot of age and salary
df.plot.scatter(x='years', y='wages');
Enter fullscreen mode Exit fullscreen mode

Top comments (0)