Real life examples of Panda in Python

Pandas, the highly acclaimed data manipulation library in Python, excels in addressing intricate data tasks with finesse and utmost efficiency.

In the following article, we will explain more advanced scenarios to demonstrate the inherent prowess of Pandas in effortlessly handling complex data manipulation challenges.

Example 1: Merging and Joining Datasets

Often, data comes from various sources and needs to be merged for comprehensive analysis.

Let’s consider two datasets: one containing sales data and the other with customer information. We want to merge these datasets based on a common key, such as customer ID:

import pandas as pd

# Load sales and customer datasets
sales_data = pd.read_csv('sales_data.csv')
customer_data = pd.read_csv('customer_data.csv')

# Merge datasets based on 'customer_id'
merged_data = pd.merge(sales_data, customer_data, on='customer_id')

print(merged_data.head())

Example 2: Reshaping Data

Pandas excels in reshaping data, which is crucial for different analysis needs.

Let’s say we have a dataset with multiple columns representing different months of sales, and we want to reshape it into a tidy format:

import pandas as pd

# Load sales data
sales_data = pd.read_csv('monthly_sales.csv')

# Reshape data using melt function
melted_data = pd.melt(sales_data, id_vars=['product'], var_name='month', value_name='sales')

print(melted_data.head())

Example 3: Handling Missing Data

Data often arrives with missing values that need careful handling. Suppose we have a dataset with missing values in the ‘price’ column.

We want to fill in these missing values based on the mean price for each product:

import pandas as pd

# Load data with missing values
data = pd.read_csv('data_with_missing.csv')

# Calculate mean price for each product
mean_prices = data.groupby('product')['price'].mean()

# Fill missing values based on mean prices
data['price'].fillna(data['product'].map(mean_prices), inplace=True)

print(data.head())

Example 4: Advanced Grouping and Aggregation

Pandas’ grouping and aggregation capabilities extend to more complex scenarios. Let’s assume we have data about customer transactions and we want to find the top-spending customers for each product category:

import pandas as pd

# Load customer transactions data
transactions = pd.read_csv('customer_transactions.csv')

# Group by product category and find top spenders
top_spenders = transactions.groupby(['product_category'])['amount'].nlargest(3)

print(top_spenders)

Example 5: Time Series Resampling

Time series data often requires resampling for different time frequencies. Suppose we have hourly stock price data and we want to resample it to a daily frequency:

import pandas as pd

# Load hourly stock price data
stock_data = pd.read_csv('stock_price_hourly.csv', parse_dates=['timestamp'])
stock_data.set_index('timestamp', inplace=True)

# Resample to daily frequency and calculate daily average
daily_avg_price = stock_data['price'].resample('D').mean()

print(daily_avg_price.head())

Conclusion

Pandas’ versatility and power shine through in these complex examples. Whether it’s merging datasets, reshaping data, handling missing values, performing advanced grouping and aggregation, or handling time series data, Pandas remains a reliable companion for data professionals and analysts alike.

As you venture into more intricate data manipulation tasks, Pandas’ array of functions and capabilities will continue to impress. By mastering these advanced techniques, you can unlock the full potential of Pandas and efficiently navigate complex data analysis challenges. Happy coding and data wrangling!

Check how to use Python in AWS Lambda

You Might Also Like

Leave a Reply