python-3.x - 同时写入数据框中的多个列
问题描述
我有这段代码,我正在写入数据框中的多个列。有没有办法简化它,使它发生在一行代码而不是多行代码中?
tempSummary.at[name,'Wins'] = wins
tempSummary.at[name,'Singles'] = singles
tempSummary.at[name,'Doubles'] = dubs
tempSummary.at[name,'Triples'] = trips
tempSummary.at[name,'Quads'] = quads
tempSummary.at[name,'Raw Score'] = rawScore
tempSummary.at[name,'Total Score'] = tempTotal
解决方案
使用.loc
而不是.at
which 允许列表。
一般解决方案:
df.loc[<row name>, [<list of col names>]] = [<list of vals>]
您的具体解决方案:
tempSummary.loc[name, ['Wins', 'Singles', 'Doubles', 'Triples', 'Quads', 'Raw Score', 'Total Score']] = [wins, singles, dubs, trips, quads, rawScore, tempTotal]
任何时候.at
都使用它几乎总是更好用.loc
推荐阅读
- php - 尝试使用 Laravel 7 获取财产
- batch-file - 如何创建与该文件夹中的文件名同名的文件夹?
- c - gcc -I 标志有问题
- gauge - 千分尺没有出现在普罗米修斯端点上
- arrays - 在文件的某些行上添加文本,添加的文本取决于从该行获取子字符串的命令的输出
- node.js - 如何使用 Mongoose 在 MongoDB 中的多个集合中进行搜索
- optaplanner - optaplanners toList ConstraintCollector 有错误吗?
- angular - Angular 中的双向数据绑定
- python - 为什么我的命令显示“没有在此处编辑消息”当我编辑消息并尝试命令但没有工作 discord.py
- pine-script - Pine-Script:有没有办法在 X 年前结束,而不涉及向后循环 X 年?