DEV Community

Cover image for A Developer's Guide to Visualizing Apple's 10-K Financial Data
Maciej Józefowicz for FinFeedAPI

Posted on

A Developer's Guide to Visualizing Apple's 10-K Financial Data

Analyzing a company's financial performance over time is a common task for investors and analysts. A company's annual 10-K report is a primary source for this information, but the data is often presented in large tables that can be hard to compare year-over-year.

This guide shows how to programmatically access SEC filings to extract and visualize financial data. Using Apple Inc. as an example, you will see how to retrieve several years of 10-K filings, extract the financial performance tables, and create a stacked bar chart to compare revenue from different product and service categories.

This guide covers:

  • Fetching metadata for a specific company's 10-K filings.
  • Extracting the content of a specific item from a filing.
  • Parsing HTML tables into a structured format using pandas.
  • Cleaning and preparing the data for visualization.
  • Creating a stacked bar chart to show financial trends.

What you need:

  • Python 3.x with pandas and matplotlib.
  • The api-bricks-sec-api-rest library.
  • Your personal FinFeedAPI key.

1. Environment Setup
First, install the FinFeedAPI client library.

pip install api-bricks-sec-api-rest
Enter fullscreen mode Exit fullscreen mode

Next, import the necessary libraries and configure the API client with your key.

# Import necessary libraries
import pandas as pd
import matplotlib.pyplot as plt
import api_bricks_sec_api_rest

# --- API Configuration ---
API_KEY = "YOUR_API_KEY_HERE"  # <--- REPLACE THIS WITH YOUR ACTUAL KEY!
api_client_config = api_bricks_sec_api_rest.Configuration()
api_client_config.api_key['Authorization'] = API_KEY
api_client = api_bricks_sec_api_rest.ApiClient(configuration=api_client_config)

# --- Plotting Configuration ---
plt.style.use('seaborn-v0_8-darkgrid')
plt.rcParams['figure.figsize'] = (14, 7)
Enter fullscreen mode Exit fullscreen mode

2. Fetching Filing Metadata
Start by retrieving the metadata for Apple's 10-K filings for the desired date range. This gives you the accession_number for each filing, which is needed to extract the content.

# Initialize the API client for filing metadata
filing_metadata_api = api_bricks_sec_api_rest.FilingMetadataApi(api_client)

# Fetch metadata for Apple's (CIK: 320193) 10-K filings
filings_data = filing_metadata_api.v1_filings_get(
    filling_date_start="2020-01-01",
    filling_date_end="2025-05-07",
    form_type="10-K",
    cik=320193
)

# Create and sort a DataFrame with the filing metadata
df_filings = pd.DataFrame.from_records(
    [vars(x) for x in filings_data], 
    columns=['accession_number', 'filing_date']
)
df_filings = df_filings.sort_values(by='filing_date', ascending=True)
Enter fullscreen mode Exit fullscreen mode

3. Extracting Content from a Filing
With the accession_number of a specific filing, you can extract the content of Item 7, "Management's Discussion and Analysis of Financial Condition and Results of Operations." This section typically contains the tables with financial performance data.

# Initialize the API client for content extraction
content_extraction_api = api_bricks_sec_api_rest.ContentExtractionApi(api_client)

# Get the accession number of the most recent filing
latest_accession_number = df_filings.iloc[-1]['accession_number']

# Extract the HTML content of Item 7
item7_html = content_extraction_api.v1_extractor_item_get(
    accession_number=latest_accession_number,
    item_number="7",
    type="html"
)
Enter fullscreen mode Exit fullscreen mode

4. Reading and Cleaning Financial Tables
The extracted content is in HTML format. You can use pandas' read_html function to automatically parse all the tables from this HTML into a list of DataFrames.

# Read all tables from the HTML string
tables = pd.read_html(item7_html)

# The second table is usually the main financial statement
financial_statements_raw = tables[1]

# Clean the DataFrame by removing unnecessary columns and empty rows
df_cleaned = financial_statements_raw.drop(financial_statements_raw.columns[[0, 1]], axis=1)
mask = (df_cleaned.iloc[1:, :].isna()).all(axis=0)
financial_statements = df_cleaned.drop(df_cleaned.columns[mask], axis=1).fillna('')

Enter fullscreen mode Exit fullscreen mode

5. Preparing Data for Visualization
The data needs to be rearranged to be suitable for a stacked bar chart. This involves selecting the correct rows and columns and then transposing the DataFrame so that the years become the index.

# Select the rows and columns with the revenue data
revenue_data = financial_statements.iloc[[1,2,3,4,5,6], [0,2,8,14]]

# Set the first row as the header
revenue_data.columns = revenue_data.iloc[0]
revenue_data = revenue_data[1:]

# Set the product/service categories as the index
revenue_data = revenue_data.set_index(revenue_data.columns[0])

# Transpose the DataFrame to have years as the index
df_plot = revenue_data.transpose()
Enter fullscreen mode Exit fullscreen mode

6. Visualizing Financial Performance
Finally, create a stacked bar chart to visualize the data. This chart will show the total net sales for each year, broken down by product and service category.

import matplotlib.ticker as ticker

# Create the stacked bar chart
ax = df_plot.plot(kind='bar', stacked=True, figsize=(10, 7))

# Set titles and labels
ax.set_title("Apple - Products and Services Performance", fontsize=16, fontweight='bold')
ax.set_xlabel('Year', fontsize=12)
ax.set_ylabel('Total Net Sales (in millions USD)', fontsize=12)
plt.xticks(rotation=0)

# Add a legend
ax.legend(title='Product/Service Category', loc='upper left')

# Add values to each bar segment
for container in ax.containers:
    ax.bar_label(container, labels=[f'{v:,.0f}' for v in container.datavalues], label_type='center', fontsize=8, color='white')

plt.tight_layout()
plt.show()
Enter fullscreen mode Exit fullscreen mode

A stacked bar chart titled

Final Thoughts
This guide showed a complete process for turning raw data from an SEC filing into a clear visualization of a company's financial performance. You can adapt this workflow to analyze other companies, different financial metrics, or other sections of the filings. By automating the data extraction and visualization process, you can more efficiently analyze financial reports and identify trends.

Top comments (0)