python-3.x - 如何乘以数据框的特定列
问题描述
我正在尝试将我的 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:('优秀','高于平均水平','平均','低于平均水平','非常差')
解决方案
您需要使用双方括号来选择多列。然后你可以稍微缩短你的代码:
quality[['Excellent','Above average','Average','Below the average','Very poor']] *= 100
推荐阅读
- c++ - 在成员构造函数之后调用基类构造函数
- tensorflow - Tensorflow:将矩阵列乘以向量的元素并返回矩阵
- xamarin - 如何在 Xamarin Android 应用程序中添加对 Amazon Fire TV 远程控制硬件的支持?
- html - 如果输入字段为空,则隐藏数据列表选项
- android - 处理用户输入和网络的反应式流模式
- laravel - 如何使用 Laravel 5 从 POST 结果中正确转发参数并避免刷新(重新提交)?
- javascript - Highcharts 变量饼图legendItemClick 不隐藏或更新数据
- mysql - 将 wordpress 内容导入 phpmyadmin
- google-cloud-platform - 如果我导入 google.cloud.storage,则无法在 Google Composer 上部署 DAG
- regex - 使用正则表达式记事本++将单词添加到所有文件中每一行的末尾