python pandas convert series to percent 1

python pandas convert series to percent

df[['PercentageSplit']] = ( 100 * df[['PercentageSplit']] ).round(2).astype(str) + "%" # The rounding is optional

Here is what the above code is Doing:
1. Create a new column called ‘PercentageSplit’
2. Assign the value of the ‘PercentageSplit’ column to be the value of the ‘Split’ column divided by the value of the ‘Total’ column
3. Multiply the value of the ‘PercentageSplit’ column by 100
4. Round the value of the ‘PercentageSplit’ column to 2 decimal places
5. Convert the value of the ‘PercentageSplit’ column to a string
6. Add a “%” sign to the end of the value of the ‘PercentageSplit’ column
7. Convert the value of the ‘PercentageSplit’ column to a string

Similar Posts