下面是我编写的代码,用于计算 df.a 和 df.b 值的相对变化,而 df 是一个数据帧。基本上需要计算的是df["c"] = df.a/df.a.iloc[df.d].values
。 df.d 设置为等于 df.t 如果df.a/df.a.iloc[df.d].values
大于或小于df.b/df.b.iloc[df.d].values * (1+ tolerance)
问题是该代码目前带来以下错误代码:ValueError: ('The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().', u'occurred at index 2011-01-01 00:00:00')
我完全不知道为什么......
import pandas as pd
import numpy as np
import datetime
randn = np.random.randn
rng = pd.date_range('1/1/2011', periods=10, freq='D')
df = pd.DataFrame({'a': [1.1, 1.2, 2.3, 1.4, 1.5, 1.8, 0.7, 1.8, 1.9, 2.0], 'b': [1.1, 1.5, 1.3, 1.6, 1.5, 1.1, 1.5, 1.7, 2.1, 2.1],'c':[None] * 10},index=rng)
df["d"]= [0,0,0,0,0,0,0,0,0,0]
df["t"]= np.arange(len(df))
tolerance = 0.3
def set_t(x):
if df.a/df.a.iloc[df.d].values < df.b/df.b.iloc[df.d].values * (1+tolerance):
return df.iloc[df.index.get_loc(x.name) - 1]['d'] == df.t
elif df.a/df.a.iloc[df.d].values > df.b/df.b.iloc[df.d].values * (1+tolerance):
return df.iloc[df.index.get_loc(x.name) - 1]['d'] == df.t
#The conditions in part one are exactly the same as in part 2, only first it says smaller than, and in the second part is bigger than df.b/df.b.iloc[df.d].values * (1+tolerance)
df['d'] = df.apply(set_t, axis =1)
#df["d"]= [0,0,0,3,3,3,6,7,7,7] this should be the coutcome for d
df["c"] = df.a/df.a.iloc[df.d].values
应用(df.a/df.a.iloc[df.d].values).all() < (df.b/df.b.iloc[df.d].values).all()
or .any()
不会导致预期的结果,因为它只检查当前设置的数据何时为 TRUE 或 FALSE,但不会设置新值。
期望的结果如下所示:
a b c d t
2011-01-01 1.1 1.1 1.000000 0 0
2011-01-02 1.2 1.5 1.090909 0 1
2011-01-03 2.3 1.3 2.090909 0 2
2011-01-04 1.4 1.6 1.000000 3 3
2011-01-05 1.5 1.5 1.071429 3 4
2011-01-06 1.8 1.1 1.285714 3 5
2011-01-07 0.7 1.5 1.000000 6 6
2011-01-08 1.8 1.7 1.000000 7 7
2011-01-09 1.9 2.1 1.055556 7 8
2011-01-10 2.0 2.1 1.111111 7 9
有什么想法如何解决这个问题吗?