Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

(Pandas) Add a percentage symbol to the percentage

For example, I have a dataframe that looks like this

category  numbers
a         100
b         200
c         200

And I want to add a column that present their percentages(with the percentage symbol). So this is what I’ve tried

df['percentage'] = str(100 * df['numbers']/df['numbers'].sum()) + '%'

However, this would return a list of number

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

category  numbers  percentage
a         100      0 20.00 1 40.00 2 40.00 Name: numbers, dtype: float64%
b         200      0 20.00 1 40.00 2 40.00 Name: numbers, dtype: float64%
c         200      0 20.00 1 40.00 2 40.00 Name: numbers, dtype: float64%

What could I do to let it become 20% 40% 40%

>Solution :

As already commented by mozway, the key is to use .astype(str) which turns the value of each individual cell into a string, while str() gives you the string representation of the series as a whole.

>>> df = pd.DataFrame({"numbers": [100, 200, 200]})
>>> tmp = 100 * df.numbers / df.numbers.sum()
>>> tmp
0    20.0
1    40.0
2    40.0
Name: numbers, dtype: float64
>>> df["percentage"] = tmp.astype(str) + "%"
>>> df
   numbers percentage
0      100      20.0%
1      200      40.0%
2      200      40.0%
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading