python - Python 在将函数输出写入同一行中的列的数据帧行上应用函数
问题描述
我有一个如下所示的 DataFrame,我想应用一个函数来计算具有“值”的内容,并根据结果写入其中一列:A、B、C、D 关于哪个结果的信息写入哪一列在字典中
问题是该函数没有将想要的数据写入数据帧。(使用 df.iterrows 它工作但花了很长时间)
date flow A B C D
0 2017-01-01 00:00:00 7185.0
1 2017-01-01 00:15:00 7145.0
2 2017-01-01 00:30:00 7135.0
3 2017-01-01 00:45:00 7122.0
4 2017-01-01 01:00:00 7142.0
def my_func(row):
low, high = nearest_neighbours(row)
if row['Flow'] > max(xp):
col = [k for k, val in dict.iteritems() if value == max(dict.values())]
row[col] = 0.25
elif row['Flow'] == 0:
row['A'] = 0.25
else:
col = [k for k, val in dict.iteritems() if value == low]
row[col] = (high - row['Flow']) / (4 * (high - low))
col = [k for k, val in dict.iteritems() if value == high]
row[col] = (row['Flow'] - low) / (4 * (high - low))
df.apply(my_func,axis=1)
从评论中复制:
我有一个看起来像的字典:
{'A':3000, 'B':6000, 'C':9000 etc.}
我想A-D
根据'Flow'
值相对于字典的位置在列中写入一些值。
例如:
if Flow ==7500:
# write some values in C and D
解决方案
推荐阅读
- windows-installer - 如何使执行别名在 Windows Server 2019 上工作?
- c# - KeyAvailable 不会重置为 false
- nginx - 从另一个网络(nginx/gunicorn)访问在家庭服务器上运行的烧瓶应用程序
- grails - Grails - 选择和保存之间的不同日期
- html - 没有 Javascript 的汉堡包下拉菜单
- java - 如何重新绘制所有 JTabbedPane 面板
- gpu - 如何在 spyder (anaconda3) 中使用我的 GPU 运行我的 python 代码?
- sqlite - SQLite - 将 3 个不同的数据库连接到一个数据库中
- python - 在 macOS 11.3.1 上从 cv2.VideoCapture 读取失败
- docker - 这是什么意思?“需要 gitlab docker runner,通过“pass-thru”到主机 docker 套接字。”