Home >Backend Development >Python Tutorial >How to Calculate the Percentage of Sales per Office Within Each State in a Pandas DataFrame?

How to Calculate the Percentage of Sales per Office Within Each State in a Pandas DataFrame?

Barbara Streisand
Barbara StreisandOriginal
2024-12-11 08:36:11228browse

How to Calculate the Percentage of Sales per Office Within Each State in a Pandas DataFrame?

Calculating Percentage of Sales by Office Within a State

Problem:

In a DataFrame with data on sales, you want to determine the percentage of sales per office within each state, where the total of all percentages in each state sums up to 100%.

Solution:

To achieve this, you can utilize the groupby and transform functions to calculate the percentage of sales relative to the total sales in each state:

import pandas as pd

# Create the DataFrame
df = pd.DataFrame({'state': ['CA', 'WA', 'CO', 'AZ'] * 3,
                   'office_id': list(range(1, 7)) * 2,
                   'sales': [np.random.randint(100000, 999999)
                             for _ in range(12)]})

# Calculate the sum of sales for each state
total_sales_by_state = df.groupby('state')['sales'].transform('sum')

# Calculate the percentage of sales for each office
df['sales_percent'] = 100 * df['sales'] / total_sales_by_state

This will add a new column, sales_percent, to your DataFrame, which represents the percentage of sales for each office relative to the total sales in its respective state.

The above is the detailed content of How to Calculate the Percentage of Sales per Office Within Each State in a Pandas DataFrame?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn