7/23/25, 10:14 PM BCG_analysis.
ipynb - Colab
keyboard_arrow_down Loading Data from Google Drive into a Pandas DataFrame
This code snippet performs the essential task of connecting your Google Colab notebook to
your Google Drive, allowing you to access and load files stored there. Let's break down each
step.
1.Import Necessary Libraries
import pandas as pd: This line imports the pandas library, which is the standard tool in Python
for data manipulation and analysis. We give it the shorter alias pd by convention.
from google.colab import drive: This imports the drive module from the google.colab library.
This module contains the tools needed to authenticate and connect your Google Drive to the
Colab environment.
2. Mount Your Google Drive
drive.mount('/content/drive')
This is the key step for connecting to your files.
What it does: This command "mounts" your entire Google Drive to the file system of the Colab
virtual machine. You can think of it like plugging a USB drive into a computer.
Authentication: When you run this cell for the first time, Colab will prompt you to authorize
access. You will need to click a link, sign in to your Google account, and copy an authorization
code back into the Colab output box.
File Path: After successful mounting, your Google Drive files will be accessible under the
directory /content/drive/.
3. Load the CSV File
df = pd.read_csv('/content/drive/My Drive/BCG_data/BCG_data.csv')
This final line uses pandas to read your data file.
pd.read_csv(): This is the pandas function for reading comma-separated values (CSV) files.
File Path: The path '/content/drive/My Drive/BCG_data/BCG_data.csv' tells pandas exactly
where to find the file:
/content/drive/: The mount point you just created.
My Drive/: The root of your Google Drive.
BCG_data/BCG_data.csv: The specific folder and file you want to load.
df = ...: The data from the CSV is loaded into a pandas DataFrame named df. A DataFrame is a
powerful, two-dimensional table structure that you can use to analyze and manipulate your data.
https://colab.research.google.com/drive/1ZZ1EEZ8vcRbHb39sk1jxfMSIuH2WOHt7#scrollTo=ape10zrIHQR5&printMode=true 1/4
7/23/25, 10:14 PM BCG_analysis.ipynb - Colab
1 import pandas as pd
2 from google.colab import drive
3 drive.mount('/content/drive')
4
5
6 df = pd.read_csv('/content/drive/My Drive/BCG_data/BCG_data.csv')
7
Mounted at /content/drive
keyboard_arrow_down Calculating Year-over-Year Financial Growth
This code block first prepares the data and then calculates the annual percentage growth for
key financial metrics for each company.
1. Sorting the Data (Crucial First Step)
Python
df = df.sort_values(by=['Company', 'Fiscal Year'])
Before any calculations, we sort the DataFrame. This is the most important step. By sorting by
Company and then Fiscal Year (in ascending order), we ensure that the data for each company
is arranged chronologically (e.g., 2021, 2022, 2023). This is essential for calculating the growth
from one year to the next correctly.
2. Calculating Percentage Growth
Python
df['Revenue Growth (%)'] = df.groupby(['Company'])['Total Revenue (in billions
USD)'].pct_change() * 100
df['Net Income Growth (%)'] = df.groupby(['Company'])['Net Income (in billions
USD)'].pct_change() * 100
df.groupby(['Company']): This groups the data by company, so the growth for Apple is calculated
independently from Tesla and Microsoft.
.pct_change() * 100: This is the core calculation. For each company's group, it calculates the
percentage change from the previous year's value. We multiply by 100 to get a percentage.
3. Understanding the Output
Company Fiscal Year ... Revenue Growth (%) Net Income Growth (%) 8 Apple 2021 ... NaN
NaN 7 Apple 2022 ... 7.793206 5.407267 6 Apple 2023 ... -2.800000 -2.805611 2 Microsoft
2021 ... NaN NaN 1 Microsoft 2022 ... 17.954667 18.720418 0 Microsoft 2023 ... 6.884551
-0.522409 5 Tesla 2021 ... NaN NaN 4 Tesla 2022 ... 51.356373 127.536232 3 Tesla 2023
... 18.794494 19.187898
The final output now shows the two new columns with the correct year-over-year growth figures.
https://colab.research.google.com/drive/1ZZ1EEZ8vcRbHb39sk1jxfMSIuH2WOHt7#scrollTo=ape10zrIHQR5&printMode=true 2/4
7/23/25, 10:14 PM BCG_analysis.ipynb - Colab
NaN Values: The NaN (Not a Number) values for the 2021 fiscal year are expected. This is
because it's the earliest year in our dataset for each company, so there is no prior year to
calculate growth from.
Interpreting the Growth: You can now clearly see the growth patterns. For example, Tesla's
revenue grew by a massive 51.36% from 2021 to 2022, while Apple's revenue saw a slight
decline of -2.8% from 2022 to 2023.
1 df = df.sort_values(by=['Company', 'Fiscal Year'])
2
3 df['Revenue Growth (%)'] = df.groupby(['Company'])['Total Revenue (in billions USD)']
4 df['Net Income Growth (%)'] = df.groupby(['Company'])['Net Income (in billions USD)']
5
6 print(df)
7
Company Fiscal Year Total Revenue (in billions USD) \
8 Apple 2021 365.82
7 Apple 2022 394.33
6 Apple 2023 383.29
2 Microsoft 2021 168.09
1 Microsoft 2022 198.27
0 Microsoft 2023 211.92
5 Tesla 2021 53.82
4 Tesla 2022 81.46
3 Tesla 2023 96.77
Net Income (in billions USD) Total Assets (in billions USD) \
8 94.68 351.00
7 99.80 352.76
6 97.00 352.58
2 61.27 333.78
1 72.74 364.84
0 72.36 411.98
5 5.52 62.13
4 12.56 82.33
3 14.97 106.62
Total Liabilities (in billions USD) \
8 287.91
7 302.08
6 290.44
2 191.79
1 198.29
0 205.75
5 30.55
4 36.44
3 43.01
Cash Flow from Operating Activities (in billions USD) Revenue Growth (%) \
8 104.04 NaN
7 122.15 7.793450
6 110.54 -2.799686
2 76.74 NaN
1 89.03 17.954667
0 87.58 6.884551
5 11.50 NaN
https://colab.research.google.com/drive/1ZZ1EEZ8vcRbHb39sk1jxfMSIuH2WOHt7#scrollTo=ape10zrIHQR5&printMode=true 3/4
7/23/25, 10:14 PM BCG_analysis.ipynb - Colab
4 14.72 51.356373
3 13.26 18.794500
Net Income Growth (%)
8 NaN
7 5.407689
6 -2.805611
2 NaN
1 18.720418
0 -0.522409
5 NaN
4 127.536232
3 19.187898
https://colab.research.google.com/drive/1ZZ1EEZ8vcRbHb39sk1jxfMSIuH2WOHt7#scrollTo=ape10zrIHQR5&printMode=true 4/4