首页 > 解决方案 > 如何乘以数据框的特定列

问题描述

我正在尝试将我的 pandas 数据框中的某些列乘以 100。特别是那些位于 df 中间的列。

这是我试图在 Jupyter Notebook 中使用的代码:

quality = quality[['Question','Excellent','Above average','Average','Below the average','Very poor','Total']]

quality['Excellent','Above average','Average','Below the average','Very poor'] = quality['Excellent','Above average','Average','Below the average','Very poor']*100

quality

我预计单元格的输出应该是 0.1182 的 11.82%。

我得到的输出如下:

KeyError:('优秀','高于平均水平','平均','低于平均水平','非常差')

标签: python-3.xpandasdataframemultiple-columns

解决方案


您需要使用双方括号来选择多列。然后你可以稍微缩短你的代码:

quality[['Excellent','Above average','Average','Below the average','Very poor']] *= 100

推荐阅读