Use value_counts with normalize=True:

df['gender'].value_counts(normalize=True) * 100

The result is a fraction in range (0, 1]. We multiply by 100 here in order to get the %.

Answer from coldspeed95 on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-calculate-the-percentage-of-a-column-in-pandas
How to Calculate the Percentage of a Column in Pandas - GeeksforGeeks
July 15, 2025 - # Import required libraries import ... columns = ['Name', 'Math_score']) # Calculating Percentage df1['percent'] = (df1['Math_score'] / df1['Math_score'].sum()) * 100 # Show the dataframe df1 ......
Discussions

python - Pandas percentage of total with groupby - Stack Overflow
This is obviously simple, but as a numpy newbe I'm getting stuck. I have a CSV file that contains 3 columns, the State, the Office ID, and the Sales for that office. I want to calculate the percent... More on stackoverflow.com
🌐 stackoverflow.com
python - Compute percentage for each row in pandas dataframe - Stack Overflow
1 Change each cell in a pandas data frame to percentages · -1 How to convert survey results as a percentage of the total respondents in python pandas? 18 How to calculate percentage with Pandas' DataFrame · 1 Calculate percent value across a row in a dataframe · 1 how to calculate percentage for particular rows for given columns ... More on stackoverflow.com
🌐 stackoverflow.com
May 22, 2017
python - How to calculate percentage with Pandas' DataFrame - Stack Overflow
How to add another column to Pandas' DataFrame with percentage? The dict can change on size. >>> import pandas as pd >>> a = {'Test 1': 4, 'Test 2': 1, 'Test 3': 1, 'Test 4': 9} ... More on stackoverflow.com
🌐 stackoverflow.com
Pandas, loop through each row of a column and append string to next column previous row

Can't test right now but something like this should work:

df = pd.read_excel('yourfile.xlsx')
for index, element in enumerate(df['PartID']):
try:
int(element)
except ValueError:
df['PartID'][index] = index + 1
df['Notes'][index - 1] += element

Here we iterate over the elements of the first column while also yielding their index in the column. The try block tests wether a given element is a number; if it's not, we change the element in that position to the index + 1 (since index starts from 0 but your column starts from 1), and add the element to the previous row (index - 1) of the Notes column.

More on reddit.com
🌐 r/learnpython
7
1
September 7, 2018
🌐
Saturn Cloud
saturncloud.io › blog › how-to-calculate-percentage-with-pandas-dataframe
How to Calculate Percentage with Pandas DataFrame | Saturn Cloud Blog
December 7, 2023 - The result is a Pandas' Series with the percentage for each row. Suppose we have a Pandas' DataFrame df with multiple columns A, B, and C representing the number of apples, bananas, and cherries sold in a store. We want to calculate the percentage of each fruit sold per day.
🌐
Statology
statology.org › home › pandas: how to represent value_counts as percentage
Pandas: How to Represent value_counts as Percentage
December 1, 2022 - To represent the values as percentages, you can use one of the following methods: Method 1: Represent Value Counts as Percentages (Formatted as Decimals) ... counts = df.my_col.value_counts() percs = df.my_col.value_counts(normalize=True) ...
🌐
Spark By {Examples}
sparkbyexamples.com › home › pandas › pandas percentage total with groupby
Pandas Percentage Total With Groupby - Spark By {Examples}
December 2, 2024 - To calculate the percentage of a column’s total for each group in a Pandas DataFrame, you can use the groupby function in combination with transform to compute the percentage of the total within each group.
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-calculate-the-percentage-of-a-column-in-pandas
How to calculate the Percentage of a column in Pandas ? - GeeksforGeeks
September 29, 2023 - A Percentage is calculated by the mathematical formula of dividing the value by the sum of all the values and then multiplying the sum by 100. This is also applicable in Pandas Dataframes.
🌐
Skytowner
skytowner.com › explore › calculating_the_percentage_of_each_value_in_each_group_in_pandas
Calculating the percentage of each value in each group in Pandas
the my_df.sum() returns a Series containing the sum of each column of my_df. In this case, for group a, my_df.sum() would evaluate to a Series holding values [5,13].
Find elsewhere
🌐
CodeSpeedy
codespeedy.com › home › calculate percentage of a column in pandas python
Calculate percentage of a column in Pandas Python - CodeSpeedy
December 12, 2022 - # import the library import pandas as pd # make the raw dataframe dataframe={'Employee':['Vivek Kaira','Nimish Adhikari','Nikunj Jatayu','John Writer','Kale Neesham','Teddy Oscar'], 'Designation':['Senior Executive','Manager','Gamer','Software Engineer','Developer','Tea Seller'], 'Salary':[100000000,100000,100000,1000000,10000000,10000], 'Workers':[500,100,10,200,300,0], 'Profit Made':[2000000000,3000000,120000,100000,4000000,100000]} # Create the pandas dataframe df=pd.DataFrame(dataframe) # formulate the percentage of salary each employee gets df['Percentage_sal']=(df['Salary']/df['Salary'].sum())*100 # Formulate the percentage of workers df['Percentage_worker']=(df['Workers']/df['Workers'].sum())*100 # Formulate the percentage of profit made df['Percentage_profit']=(df['Profit Made']/df['Profit Made'].sum())*100 # display the dataframe display(df)
🌐
DataScience Made Simple
datasciencemadesimple.com › home › get the percentage of a column in pandas python
Get the percentage of a column in pandas python - DataScience Made Simple
February 5, 2023 - import pandas as pd import numpy ...'Mathematics_score']) print(df1) ... Percentage of a column in pandas dataframe is computed using sum() function and stored in a new column namely percentage as shown below...
🌐
IncludeHelp
includehelp.com › python › pandas-get-frequency-of-item-occurrences-in-a-column-as-percentage.aspx
Pandas get frequency of item occurrences in a column as percentage
September 25, 2023 - We will first use value_count which will return the count of total occurrences of each value and then we will divide each value by the total length of the dataFrame. Finally, we will multiply this value by 100 to get the percentage. ... # Importing pandas package import pandas as pd # Creating ...
Top answer
1 of 16
395

Update 2022-03

This answer by caner using transform looks much better than my original answer!

df['sales'] / df.groupby('state')['sales'].transform('sum')

Thanks to this comment by Paul Rougieux for surfacing it.

Original Answer (2014)

Paul H's answer is right that you will have to make a second groupby object, but you can calculate the percentage in a simpler way -- just groupby the state_office and divide the sales column by its sum. Copying the beginning of Paul H's answer:

# From Paul H
import numpy as np
import pandas as pd
np.random.seed(0)
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)]})
state_office = df.groupby(['state', 'office_id']).agg({'sales': 'sum'})
# Change: groupby state_office and divide by sum
state_pcts = state_office.groupby(level=0).apply(lambda x:
                                                 100 * x / float(x.sum()))

Returns:

                     sales
state office_id           
AZ    2          16.981365
      4          19.250033
      6          63.768601
CA    1          19.331879
      3          33.858747
      5          46.809373
CO    1          36.851857
      3          19.874290
      5          43.273852
WA    2          34.707233
      4          35.511259
      6          29.781508
2 of 16
102

(This solution is inspired from this article https://pbpython.com/pandas_transform.html)

I find the following solution to be the simplest(and probably the fastest) using transformation:

Transformation: While aggregation must return a reduced version of the data, transformation can return some transformed version of the full data to recombine. For such a transformation, the output is the same shape as the input.

So using transformation, the solution is 1-liner:

df['%'] = 100 * df['sales'] / df.groupby('state')['sales'].transform('sum')

And if you print:

print(df.sort_values(['state', 'office_id']).reset_index(drop=True))

   state  office_id   sales          %
0     AZ          2  195197   9.844309
1     AZ          4  877890  44.274352
2     AZ          6  909754  45.881339
3     CA          1  614752  50.415708
4     CA          3  395340  32.421767
5     CA          5  209274  17.162525
6     CO          1  549430  42.659629
7     CO          3  457514  35.522956
8     CO          5  280995  21.817415
9     WA          2  828238  35.696929
10    WA          4  719366  31.004563
11    WA          6  772590  33.298509
🌐
Softhints
softhints.com › pandas-count-percentage-value-column
Pandas count and percentage by value for a column - Softhints
February 10, 2022 - from tabula import read_pdf import pandas as pd df = read_pdf("http://www.uncledavesenterprise.com/file/health/Food Calories List.pdf", pages=3, pandas_options={'header': None}) df.columns = ['food', 'Portion size ', 'per 100 grams', 'energy'] df.head() Then we are going to calculate the count and percent: s = df.keywords counts = s.value_counts() percent = s.value_counts(normalize=True) percent100 = s.value_counts(normalize=True).mul(100).round(1).astype(str) + '%' pd.DataFrame({'counts': counts, 'per': percent, 'per100': percent100})
🌐
Medium
medium.com › @shivamkaus › how-to-calculate-the-percentage-of-a-column-in-pandas-pythonpandas-com-a196e23e33de
How to calculate the Percentage of a column in Pandas ? — PythonPandas.com | by Shivam Kau | Medium
August 16, 2023 - The lambda function divides each value by the sum of the B column and multiplies it by 100 to get the percentage. Finally, we create a new column B_Percentage and assign the calculated percentages to it.
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.pct_change.html
pandas.DataFrame.pct_change — pandas 3.0.1 documentation
Shift the index by some number of periods. ... See the percentage change in a Series where filling NAs with last valid observation forward to next valid. >>> s = pd.Series([90, 91, None, 85]) >>> s 0 90.0 1 91.0 2 NaN 3 85.0 dtype: float64 · >>> s.ffill().pct_change() 0 NaN 1 0.011111 2 0.000000 3 -0.065934 dtype: float64 ... Percentage change in French franc, Deutsche Mark, and Italian lira from 1980-01-01 to 1980-03-01.
🌐
Statology
statology.org › home › pandas: how to calculate percentage of total within group
Pandas: How to Calculate Percentage of Total Within Group
June 11, 2022 - You can use the following syntax to calculate the percentage of a total within groups in pandas: df['values_var'] / df.groupby('group_var')['values_var'].transform('sum')
Top answer
1 of 2
17

You can get the percentages of each column using a lambda function as follows:

>>> df.iloc[:, 3:].apply(lambda x: x / x.sum())
       y191      y192      y193      y194      y195
0  0.527231  0.508411  0.490517  0.500544  0.480236
1  0.013305  0.014088  0.013463  0.013631  0.013713
2  0.316116  0.324405  0.341373  0.319164  0.323259
3  0.002006  0.002263  0.002678  0.003206  0.002872
4  0.141342  0.150833  0.151969  0.163455  0.179920

Your example does not have any duplicate values for val_code, so I'm unsure how you want your data to appear (i.e. show percent of total in column vs. total for each vval_code group.)

2 of 2
3

Ge the total for all the columns of interest and then add the percentage column:

In [35]:
total = np.sum(df.ix[:,'y191':].values)
df['percent'] = df.ix[:,'y191':].sum(axis=1)/total * 100
df

Out[35]:
               country_name  country_code  val_code      y191      y192  \
0  United States of America           231         1  47052179  43361966   
1  United States of America           231         1   1187385   1201557   
2  United States of America           231         1  28211467  27668273   
3  United States of America           231         1    179000    193000   
4  United States of America           231         1  12613922  12864425   

       y193      y194      y195    percent  
0  42736682  43196916  41751928  50.149471  
1   1172941   1176366   1192173   1.363631  
2  29742374  27543836  28104317  32.483447  
3    233338    276639    249688   0.260213  
4  13240395  14106139  15642337  15.743237  

So np.sum will sum all the values:

In [32]:
total = np.sum(df.ix[:,'y191':].values)
total

Out[32]:
434899243

We then call .sum(axis=1)/total * 100 on the cols of interest to sum row-wise, divide by the total and multiply by 100 to get a percentage.

🌐
GitHub
github.com › softhints › python › blob › master › notebooks › Pandas count and percentage by value for a column.ipynb
python/notebooks/Pandas count and percentage by value for a column.ipynb at master · softhints/python
Jupyter notebooks and datasets for the interesting pandas/python/data science video series. - python/notebooks/Pandas count and percentage by value for a column.ipynb at master · softhints/python
Author   softhints
🌐
Data Science Parichay
datascienceparichay.com › home › blog › pandas – percentage of missing values in each column
Pandas - Percentage of Missing Values in Each Column - Data Science Parichay
March 6, 2022 - Instead of applying the isnull() function to a single column, apply it to the entire dataframe. Let’s see it in action. # percentage missing values in the dataframe df.isnull().sum()/len(df) ...
🌐
Arab Psychology
scales.arabpsychology.com › home › stats › how to calculate and display percentages with pandas value counts
How To Represent Value_counts As Percentage In Pandas?
November 22, 2025 - Using the first method, we instruct Pandas to calculate the relative frequency of each team. The resulting output is a Series of floating-point numbers, representing the proportion of records belonging to each team category. Since there are 8 total records, the sum of these proportions will equal 1.0. The following code shows how to count the occurrence of each value in the team column and represent the occurrences as a percentage of the total, formatted as a decimal: