DEV Community

Cover image for Unleashing the Power of Pandas: Calculating Portfolio Returns
Bahman Shadmehr
Bahman Shadmehr

Posted on

Unleashing the Power of Pandas: Calculating Portfolio Returns

As a data enthusiast, you're well aware of the importance of manipulating and analyzing financial data. In this blog post, we'll embark on a journey to calculate portfolio returns using Pandas—a versatile data manipulation library in Python. The challenge involves merging historical stock data for Apple Inc. (AAPL), Microsoft Corporation (MSFT), and Alphabet Inc. (GOOGL), and then computing and visualizing cumulative returns for each stock and the overall portfolio.

Step 1: Importing Libraries

import pandas as pd
import yfinance as yf
import matplotlib.pyplot as plt
Enter fullscreen mode Exit fullscreen mode

Step 2: Downloading and Exploring Data

Let's start by fetching historical stock data for the three companies.

ticker_aapl = "AAPL"
ticker_msft = "MSFT"
ticker_googl = "GOOGL"

data_aapl =, start="2020-01-01", end="2023-01-01")
data_msft =, start="2020-01-01", end="2023-01-01")
data_googl =, start="2020-01-01", end="2023-01-01")
Enter fullscreen mode Exit fullscreen mode

Step 3: Merging DataFrames

Combine the individual stock data into a single DataFrame.

portfolio_data = pd.merge(data_aapl, data_msft, left_index=True, right_index=True, suffixes=('_AAPL', '_MSFT'))
portfolio_data = pd.merge(portfolio_data, data_googl, left_index=True, right_index=True, suffixes=('', '_GOOGL'))
Enter fullscreen mode Exit fullscreen mode

Step 4: Calculating Returns

Now, let's compute daily and cumulative returns for each stock and the overall portfolio.

portfolio_data['Portfolio_Value'] = (portfolio_data['Close_AAPL'] + portfolio_data['Close_MSFT'] + portfolio_data['Close']) / 3

portfolio_data['Return_AAPL'] = portfolio_data['Close_AAPL'].pct_change()
portfolio_data['Return_MSFT'] = portfolio_data['Close_MSFT'].pct_change()
portfolio_data['Return_GOOGL'] = portfolio_data['Close'].pct_change()
portfolio_data['Return_Portfolio'] = portfolio_data['Portfolio_Value'].pct_change()

portfolio_data['Cumulative_Return_AAPL'] = (1 + portfolio_data['Return_AAPL']).cumprod() - 1
portfolio_data['Cumulative_Return_MSFT'] = (1 + portfolio_data['Return_MSFT']).cumprod() - 1
portfolio_data['Cumulative_Return_GOOGL'] = (1 + portfolio_data['Return_GOOGL']).cumprod() - 1
portfolio_data['Cumulative_Return_Portfolio'] = (1 + portfolio_data['Return_Portfolio']).cumprod() - 1
Enter fullscreen mode Exit fullscreen mode

Step 5: Visualizing the Results

Finally, let's visualize the cumulative returns.

plt.figure(figsize=(12, 6))
plt.plot(portfolio_data.index, portfolio_data['Cumulative_Return_AAPL'], label='AAPL')
plt.plot(portfolio_data.index, portfolio_data['Cumulative_Return_MSFT'], label='MSFT')
plt.plot(portfolio_data.index, portfolio_data['Cumulative_Return_GOOGL'], label='GOOGL')
plt.plot(portfolio_data.index, portfolio_data['Cumulative_Return_Portfolio'], label='Portfolio', linestyle='--', color='black')
plt.title('Cumulative Returns - Portfolio vs Individual Stocks')
plt.ylabel('Cumulative Return')
Enter fullscreen mode Exit fullscreen mode


Congratulations! You've successfully tackled the challenge of calculating portfolio returns using Pandas. This journey highlights the capabilities of Pandas in handling financial data, merging datasets, and performing complex calculations effortlessly.

Feel free to adapt and expand upon this example to suit your specific needs. Keep exploring the vast world of data analysis with Pandas, and happy coding!

Top comments (0)