add percentage column pandas 1

add percentage column pandas

df['%'] = ((df['Code Lines'] / df['Code Lines'].sum())*100).round(2).astype(str) + '%'

Here is what the above code is Doing:
1. We’re creating a new column called ‘%’
2. We’re dividing the ‘Code Lines’ column by the sum of the ‘Code Lines’ column
3. We’re multiplying the result by 100 to get the percentage
4. We’re rounding the result to 2 decimal places
5. We’re converting the result to a string
6. We’re adding a ‘%’ sign to the end of the string
7. We’re assigning the result to the ‘%’ column

Similar Posts