(Pandas) Add a percentage symbol to the percentage
Question:
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
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%
Answers:
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%
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
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%
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%