[英]In pandas, how to convert the result of dividing two columns from a decimal to a percentage?
When using pandas, how do I convert a decimal to a percentage when the result obtained by dividing two columns as another column?使用 pandas 时,将两列作为另一列得到的结果如何将小数转换为百分比?
for example:例如:
df_income_structure['C'] = (df_income_structure['A']/df_income_structure['B'])
If the value of df_income_structure['C'] is a decimal, how to convert it to a percentage?如果 df_income_structure['C'] 的值为小数,如何将其转换为百分比?
Format it like this:像这样格式化它:
df_income_structure.style.format({'C': '{:,.2%}'.format})
Change the number depending on how many decimal places you'd like.根据您想要的小数位数更改数字。
Use basic pandas operators For example if we have a dataframe with columns name like column1, column2,column3, ...
so we can:使用基本的 pandas 运算符例如,如果我们有一个 dataframe 的列名,如column1, column2,column3, ...
所以我们可以:
Columns = [column1, column2,column3, ....].列 = [column1, column2,column3, ....]。
df[Columns] = df[Columns].div(df[Columns].sum(axis=1), axis=0).multiply(100)
I hope this answer has solved your problem.我希望这个答案已经解决了你的问题。
Good luck祝你好运
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.