Python data frames are grouped by columns and create new columns with percentages

Python data frames are grouped by columns and create new columns with percentages … here is a solution to the problem.

Python data frames are grouped by columns and create new columns with percentages

I have a scenario that simulates a data frame as follows:

    Month  Amount  
1   Jan     260
2   Feb    179
3   Mar    153
4   Apr    142
5   May    128
6   Jun    116
7   Jul    71
8   Aug    56
9   Sep    49
10  Oct    17
11  Nov    0
12  Dec    0

I’m trying to get a new column by calculating the percentage of each row using a data frame group, and using a lambda function like this:

 df = pd. DataFrame(mylistofdict)
 df = df.groupby('Month')["Amount"].apply(lambda x: x / x.sum()*100)

But I don’t get the expected result that is only less than 2 columns :

    Month   Percentage
1   Jan         22%
2   Feb         15%
3   Mar         13%
4   Apr         12%
5   May         11%
6   Jun         10%
7   Jul         6%
8   Aug         5%
9   Sep         4%
10  Oct         1%
11  Nov         0
12  Dec         0

How can I modify my code, or what better way than to use a data frame.

Related Problems and Solutions