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%.
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!