top of page
  • Writer's pictureHarini Mallawaarachchi

Introduction to Pandas

In the world of data science and analysis, being able to efficiently manipulate and analyze data is a fundamental skill. This is where Pandas, a Python library, comes to the rescue. Whether you're just starting your data journey or looking to level up your skills, this beginner's guide to Pandas will equip you with the essential knowledge you need to effectively manipulate and analyze your data.


What is Pandas?

Pandas is an open-source library built on top of the Python programming language. It provides fast, flexible, and easy-to-use data structures and tools for efficient data manipulation and analysis. With Pandas, you can effortlessly handle various data formats, clean, messy data, perform basic statistical analysis, and create insightful visualizations.



Getting Started: Importing Pandas

Before you can start using Pandas, you need to import it into your Python environment. The conventional way to do this is as follows:

import pandas as pd

Now, let's dive into some of the fundamental concepts and code snippets that will set you on the path to mastering data manipulation with Pandas.



Loading Data

The first step in any data analysis project is loading your data into Pandas. Whether it's a CSV file, an Excel spreadsheet, or even data from a SQL database, Pandas has you covered.

# Load data from a CSV file
data = pd.read_csv('data.csv')

# Load data from an Excel file
data = pd.read_excel('data.xlsx')

# Load data from a SQL databaseimport sqlite3
conn = sqlite3.connect('database.db')
data = pd.read_sql_query('SELECT * FROM table_name', conn)


Exploring Your Data

Understanding your dataset is crucial before diving into any analysis. Pandas offers various methods to help you get a quick overview of your data.

# Display the first few rows of the DataFrame
data.head()

# Display basic statistics of numerical columns
data.describe()

# Check for missing values in the DataFrame
data.isnull().sum()



Grouping and Aggregating Data

Pandas excels at grouping data based on specific columns and performing aggregations.

# Group by a column and calculate the mean
grouped_data = data.groupby('category')['value'].mean()

# Apply multiple aggregation functions
agg_results = data.groupby('category')['value'].agg(['mean', 'max', 'min'])



Selecting and Filtering Data

Pandas makes it easy to select specific columns and filter rows based on certain conditions.

# Select a single column
column_data = data[['column_name']]

# Select multiple columns
subset = data[['column1', 'column2']]

# Filter rows based on a condition
filtered_data = data[data['column'] > 50]



Adding and Modifying Data

You can create new columns and modify existing ones using Pandas.

# Create a new column based on existing data
data['new_column'] = data['old_column'] * 2

# Modify data in a specific column
data['age'] = data['age'] + 1



Using Anti-Join Logic

This line of code filters the customers DataFrame to exclude rows where the 'id' column matches any of the values in the 'customerId' column of the orders DataFrame. The isin() function is used to check for membership, and the ~ operator negates the condition to select rows that are not present in the 'customerId' column of the orders DataFrame. This effectively filters out customers who have made orders.

df = customers[~customers['id'].isin(orders['customerId'])]

Rename column name

df = df[['name']].rename(columns={'name': 'Customers'})


Sorting

# This sorts in ascending order
df = df.sort_values(by='id')

# This sorts in descending order
df = df.sort_values(by='id', ascending=False)


Lambda functions

Lambda functions, also known as anonymous functions, are compact, one-line functions that can be defined without a formal def statement. These functions are often used for simple operations and are particularly useful when you need a quick function definition within another function or method.

The syntax of a lambda function is as follows:

lambda arguments: expression


Why Use Lambda Functions in Pandas?

Lambda functions in Pandas can be applied to Series, DataFrames, and various data manipulation operations. They are especially handy when you need to perform quick computations or transformations on your data without defining a separate named function.


Applying Lambda Functions in Pandas

Let's take a look at a few practical examples of using lambda functions within Pandas.

Example 1: Element-wise Transformation

import pandas as pd

data = pd.DataFrame({'values': [10, 20, 30, 40]})
data['values_squared'] = data['values'].apply(lambda x: x ** 2)

Example 2: Conditional Transformation

data['status'] = data['values'].apply(lambda x: 'High' if x > 30 else 'Low')

Example 3: Filtering Rows

filtered_data = data[data['values'].apply(lambda x: x > 20)]

Example 4: Custom Aggregation

agg_results = data.groupby('status')['values'].agg(lambda x: x.mean() - x.min())


Example 5: Applying Custom Logic: Adding a 'Bonus' Column

Let's take a moment to explore a practical example of applying custom logic using Pandas. Suppose you have a DataFrame containing employee information, including their salary, employee ID, and name. You want to create a 'bonus' column that assigns a bonus amount based on specific conditions. Here's how you can do it:

data['bonus'] = data.apply(lambda row: row['salary'] if row['employee_id'] % 2 == 1 and not row['name'].startswith('M') else 0, axis=1)

In this example, the lambda function checks if the employee's ID is odd and if their name doesn't start with 'M'. If both conditions are met, the bonus will be equal to their salary; otherwise, the bonus will be 0.


String Methods


# Filtering valid email addresses using a regular expression pattern

valid_emails_df = users[users['mail'].str.match(r'^[A-Za-z][A-Za-z0-9_\.\-]*@gmail(\?com)?\.com$')]


# Filtering rows containing specific conditions in a column using a regular expression

filtered_df = df[df['conditions'].str.contains(r'^DIAB1| DIAB1', case=False, regex=True)]



Conclusion

Pandas is a versatile and indispensable tool for data engineers. With its robust data manipulation capabilities and intuitive syntax, Pandas simplifies various data engineering tasks, from data cleaning and transformation to aggregation and integration. This cheat sheet should serve as a handy reference as you embark on your data engineering endeavors using Pandas.

2 views0 comments

コメント


bottom of page