def data_to_category_counts(df):
    """
    Extracts opportunity category counts for each NAICS code.
    """
    return (
        df.groupby(['Opportunity_NAICS', 'Opportunity__r.Category__c'])
        .count()
        .iloc[:,0]
        .to_frame()
        .rename(columns={df.columns[0]: 'Count'})
        .reset_index()
        .pivot(index='Opportunity_NAICS', columns='Opportunity__r.Category__c', values='Count')
        .fillna(0)
        .rename(columns={
            'Commodity': 'Commodity Count', 
            'Construction': 'Construction Count', 
            'Personal Services': 'Personal Services Count'
        })
        .reset_index()
    )