top of page

Mastering Data Manipulation with pandas: A Comprehensive Tutorial



I. Using merge_ordered()


1. Introduction to merge_ordered()


merge_ordered() is a powerful pandas function for merging ordered datasets. Imagine it as an ordered join, which merges data in order based on a specified field. This is slightly different from the standard merge method in pandas, which combines data based on common columns but does not take order into account.

import pandas as pd

# Define two ordered datasets
df1 = pd.DataFrame({"A": ["A0", "A1", "A2"],
                     "B": ["B0", "B1", "B2"],
                     "key": ["K0", "K1", "K2"]})

df2 = pd.DataFrame({"C": ["C0", "C1", "C2"],
                     "D": ["D0", "D1", "D2"],
                     "key": ["K0", "K2", "K3"]})

# Use merge_ordered()
df_ordered = pd.merge_ordered(df1, df2, fill_method="ffill", left_by="key")


2. Financial data use case


merge_ordered() is particularly useful when dealing with financial, macroeconomic, or stock market data where information is often sorted in chronological order.


For instance, let's merge the stock data of Apple and McDonald's. The datasets are aligned by the date, allowing for a clean, ordered merge that ensures all the data are in the correct time sequence.

# Merging two dataframes, apple and mcdonalds
merged_df = pd.merge_ordered(apple, mcdonalds, on='Date', suffixes=('_apl', '_mcd'))


3. Handling missing data with forward filling


One critical issue when dealing with time-series data is handling missing values. The 'fill_method' parameter allows for forward filling, where a missing value is filled with the last observed value. This is particularly helpful when dealing with stock data where today's price is a good stand-in for any missing price tomorrow.

merged_df_filled = pd.merge_ordered(apple, mcdonalds, on='Date', fill_method='ffill', suffixes=('_apl', '_mcd'))


4. When to use merge_ordered()


merge_ordered() is best used for ordered or time-series data. It provides a flexible way to merge dataframes with options for forward and backward filling to handle missing data effectively.


II. Using merge_asof()


1. Introduction to merge_asof()


merge_asof() is another robust pandas method for dealing with ordered data, particularly when the merge is not exactly aligned. It can be likened to an "asof" join, where we join on the closest key rather than equal keys.

# Using merge_asof()
merged_asof_df = pd.merge_asof(df1, df2, on='key')


2. Application on financial datasets


Let's demonstrate the use of merge_asof() using two financial datasets: Visa and IBM stock prices. One common use case for merge_asof() is when data is sampled irregularly.

merged_asof_df = pd.merge_asof(visa, ibm, on='Date', direction='nearest', suffixes=('_v', '_ibm'))


3. When to use merge_asof()


merge_asof() is best for data sampled irregularly, where standard merge methods might not be applicable. It's a powerful tool when dealing with non-aligned times in time-series data, making sure no future data "leaks" into the present.


III. Selecting Data with .query()


1. Introduction to the .query() method


The .query() method in pandas allows you to filter data using a string expression that can be easily readable and concise.

# Select rows where column 'A' is greater than column 'B'
df.query('A > B')


2. Querying based on single and multiple conditions


We can also apply multiple conditions using logical operators like "and" and "or".

# Select rows where 'A' is greater than 'B' and 'C' is less than 'D'
df.query('A > B and C < D')


3. Using .query() with text selection


The .query() method also allows selection based on string conditions.

# Select rows where 'Name' is 'John'
df.query('Name == "John"')


IV. Reshaping Data with .melt()


1. Introduction to data reshaping with .melt()


The .melt() function in pandas is used to transform or reshape data. It's useful when we have data in a "wide" format (lots of columns) and want to convert it into a "long" format (fewer columns but more rows).

# Wide dataset
df = pd.DataFrame({
   'A': ['foo', 'bar', 'foo', 'bar', 'foo', 'bar'],
   'B': ['one', 'one', 'two', 'two', 'one', 'one'],
   'C': [2.0, 2.0, 3.0, 3.0, 2.0, 2.0],
   'D': [1, 2, 3, 4, 5, 6]
})

# Use .melt() to reshape
melted_df = df.melt(id_vars=['A', 'B'], value_vars=['C', 'D'])


2. Application of .melt()


Let's use .melt() to reshape a financial metrics dataset from wide to long format.

# Financial dataset
financials = pd.DataFrame({
   'Company': ['Apple', 'Google', 'Microsoft'],
   'Revenue': [260.174, 161.857, 125.843],
   'Net Income': [55.256, 34.343, 39.240],
   'Market Cap': [2220, 1016, 1621]
})

# Reshaping with .melt()
financials_melted = financials.melt(id_vars=['Company'], value_vars=['Revenue', 'Net Income', 'Market Cap'])



3. Controlling columns to unpivot with .melt()


The .melt() function's 'value_vars' parameter allows you to specify the columns to unpivot into the new dataframe. This gives you control over the structure of your reshaped data.

# Only unpivot 'Revenue' and 'Market Cap'
financials_partly_melted = financials.melt(id_vars=['Company'], value_vars=['Revenue', 'Market Cap'])


4. Setting column names with .melt()


You can also customize the column names in the reshaped dataframe using the 'var_name' and 'value_name' arguments.

# Set custom column names
financials_named_melted = financials.melt(id_vars=['Company'], value_vars=['Revenue', 'Net Income', 'Market Cap'], var_name='Financial Metric', value_name='Value')


In conclusion, pandas offers various powerful tools for manipulating and reshaping data, such as merge_ordered(),


V. Working with MultiIndex


1. Introduction to MultiIndex


A MultiIndex, also known as a multi-level index, allows for complex data structures in pandas. It adds an extra dimension to your data, enabling you to store and manipulate data with an arbitrary number of dimensions in lower dimensional data structures like Series (1d) and DataFrame (2d).

# Create a MultiIndex
index = pd.MultiIndex.from_tuples([(i, j) for i in ['A','B'] for j in ['x', 'y', 'z']])
df = pd.DataFrame({'Data': range(6)}, index=index)


2. Selecting Data with MultiIndex


To select data from a DataFrame with MultiIndex, you can use the .loc[] accessor.

# Select data from 'A'
df.loc['A']

For more advanced indexing, you can pass a tuple.

# Select data from 'A' and 'x'
df.loc[('A', 'x')]


3. Slicing a MultiIndex DataFrame


You can also slice a MultiIndex DataFrame, but you'll need to sort the index first.

# Sort the index
df = df.sort_index()

# Slice data where the first level is between 'A' and 'B', and the second level is between 'x' and 'y'
df.loc[('A', 'x'):('B', 'y')]


4. Resetting and Setting Index


If you need to reset the MultiIndex in your DataFrame, you can use the .reset_index() method.

# Reset index
df_reset = df.reset_index()

Conversely, you can set a new MultiIndex with the .set_index() method.

# Set new MultiIndex
df_set = df.set_index(['NewIndex1', 'NewIndex2'])


VI. Manipulating DateTime Data


1. Introduction to DateTime in pandas


pandas provides a plethora of tools for working with dates, times, and time-indexed data.

# Create a date range
dates = pd.date_range('2023-01-01', periods=6)
df = pd.DataFrame({"Data": range(6)}, index=dates)


2. Selecting Date Ranges


You can use the .loc[] accessor to select data within a certain date range.

# Select data between January 2nd and January 4th
df.loc['2023-01-02':'2023-01-04']


3. Resampling Time-Series Data


One common operation for time-series data is resampling. The .resample() method in pandas is similar to the .groupby() method - it groups data into time intervals and allows you to apply aggregation functions to them.

# Resample data to a weekly mean
df_weekly = df.resample('W').mean()


4. Shifting and Lagging Time-Series Data


In some cases, you might need to shift or lag your time-series data for time-delayed features. pandas allows this through the .shift() method.

# Shift data 2 days into the future
df_shifted = df.shift(2)


In conclusion, the power of pandas lies in its ability to handle and manipulate complex, structured data. From ordered merges to reshaping data, from multi-level indexing to datetime operations, pandas is a comprehensive library that provides robust and flexible tools for data manipulation. Mastering these tools will allow you to extract more value from your data and streamline your data analysis processes.

bottom of page