我有一个如下所示的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