我想在嵌套循环中计算以下函数,并将其保持为
Calc_Value = value_a(2D) - (values_b(0D) + values_b(1D))/10000
其结果是:
1.1274875
其定义为:
value_a(2D) corresponds to type **a**, year **2D** and value **1.1275**
value_b(0D) corresponds to type **b**, year **0D** and value **0**
value_b(1D) corresponds to type **b**, year **1D** and value **0.125**
不知何故,我不知道如何定义下面的函数,以便为b值使用适当的两个值,如上面所示?
有一种方法可以直接和熊猫(Pandas Version)一起做,但是想用我的嵌套方式。
更新:
我计算了我的函数,得到了适当的值,但仍然有一个问题,就是我把值计算了几次而不是一次。我得到每个值b的值a,直到计算出适当的值,也就是最后一个值。我只想保留最后的价值。
1.1275 1.1275 1.1275 1.1274875 ->**仅显示此值**
代码如下所示:
import pandas as pd
def CalcValue(year, a, b):
normalization = 0.0
value_0D = 0.0
value_1D = 0.0
Calc = 0.0
normalization = 10000.0
if year == "0D":
value_0D = b
elif year == "1D":
value_1D = b
Calc = a-(value_0D+value_1D)/normalization
return Calc
data = pd.read_csv('C:/Users/mcm/Desktop/Book1.csv').fillna('')
pd_date = pd.DatetimeIndex(data['date'].values)
data['date'] = pd_date
index_data = data.set_index('date')
for current_date in index_data.index.unique():
print('calculating date: ' + str(current_date))
for index, row in index_data.iterrows():
if index == current_date:
for index2, row2 in index_data.iterrows():
if index2 == current_date:
if row['type'] in {'a', 'b'} and row2['type'] in {'a', 'b'}:
if row['type'] != row2['type'] and row['type'] != 'a' and row2['type'] != 'b':
test = CalcValue(row['year'], row2['value'],row['value'])
print(test)
数据如下所示:
date type year value
2015-02-09 a 2D 1.1275
2015-02-09 b 10M 58.125
2015-02-09 b 11M 68.375
2015-02-09 b 1M 3.345
2015-02-09 b 1W 0.89
2015-02-09 b 1Y 79.375
2015-02-09 b 2M 7.535
2015-02-09 b 2W 1.8
2015-02-09 b 3M 11.61
2015-02-09 b 3W 2.48
2015-02-09 b 4M 16.2
2015-02-09 b 5M 21.65
2015-02-09 b 6M 27.1
2015-02-09 b 7M 33.625
2015-02-09 b 8M 41.375
2015-02-09 b 9M 49.5
2015-02-09 b 0D 0
2015-02-09 b 1D 0.125
2015-02-09 c 2Y -28.5
2015-02-09 c 3Y -28.75
2015-02-09 c 4Y -28
2015-02-09 c 5Y -27.5
2015-02-09 c 6Y -27
2015-02-09 c 7Y -26.75
2015-02-09 c 8Y -26.25
2015-02-09 c 9Y -25.5
2015-02-09 c 10Y -25
2015-02-10 a 2D 1.1297
2015-02-10 b 10M 60.5
2015-02-10 b 11M 70.375
2015-02-10 b 1M 3.32
2015-02-10 b 1W 0.84
2015-02-10 b 1Y 81.625
2015-02-10 b 2M 7.54
2015-02-10 b 2W 1.74
2015-02-10 b 3M 11.745
2015-02-10 b 3W 2.45
2015-02-10 b 4M 16.4
2015-02-10 b 5M 22.05
2015-02-10 b 6M 28.1
2015-02-10 b 7M 35.375
2015-02-10 b 8M 42.625
2015-02-10 b 9M 51
2015-02-10 b 0D 0.105
2015-02-10 b 1D 0.11
2015-02-10 c 2Y -29.5
2015-02-10 c 3Y -29.75
2015-02-10 c 4Y -29.5
2015-02-10 c 5Y -29
2015-02-10 c 6Y -28.5
2015-02-10 c 7Y -28
2015-02-10 c 8Y -27.5
2015-02-10 c 9Y -26.75
2015-02-10 c 10Y -26.25
发布于 2018-04-09 03:53:15
这是最低限度的:
for i in range(20):
print(i)
#print(i)
这包括一个条件:
for i in range(20):
if True:
test = i
print(test)
#print(test)
您的问题只是从循环中获得最后一个计算值--您已经实现了这一点,但是您正在打印循环中的每个计算值,即在满足条件的每个迭代中。快速的解决方案是在循环之后打印它(交换上面print()上的注释)。
当你找到你想要的东西时,你通常会停止循环。否则,您将使用另一个变量来存储结果:
keepitthatway = None
for i in range(42):
if i==7:
keepitthatway = i
# keep on iterating if necessary or
#break
print(i)
print(keepitthatway)
有很多Python编程教程,例如https://wiki.python.org/moin/ForLoop。看一看;-)
对于Minimal, Complete, and Verifiable example,这也是可行的。虽然不是很小,但它类似于您的代码结构:
for current_date in range(2):
print('calculating something...')
for index, row in enumerate(range(20)):
if index == current_date:
for index2, row2 in enumerate(range(20)):
if True:
if True:
if True:
test = index
print(test)
#print(test)
仅仅限制您的数据集已经是一个改进:
import pandas as pd
# pretend to be the csv file
import io
csv = io.StringIO("""date\ttype\tyear\tvalue
2015-02-09\ta\t2D\t1.1275
2015-02-09\tb\t10M\t58.125
2015-02-09\tc\t2Y\t-28.5
2015-02-10\ta\t2D\t1.1297
2015-02-10\tb\t10M\t60.5
2015-02-10\tc\t2Y\t-29.5
2015-02-10\tc\t10Y\t-26.25
""")
def CalcValue(year, a, b):
normalization = 0.0
value_0D = 0.0
value_1D = 0.0
Calc = 0.0
normalization = 10000.0
if year == "0D":
value_0D = b
elif year == "1D":
value_1D = b
Calc = a-(value_0D+value_1D)/normalization
return Calc
data = pd.read_csv(csv, sep="\t").fillna('')
#print(data)
#import sys;sys.exit()
pd_date = pd.DatetimeIndex(data['date'].values)
data['date'] = pd_date
index_data = data.set_index('date')
for current_date in index_data.index.unique():
print('calculating date: ' + str(current_date))
for index, row in index_data.iterrows():
if index == current_date:
for index2, row2 in index_data.iterrows():
if index2 == current_date:
if row['type'] in {'a', 'b'} and row2['type'] in {'a', 'b'}:
if row['type'] != row2['type'] and row['type'] != 'a' and row2['type'] != 'b':
test = CalcValue(row['year'], row2['value'],row['value'])
print(test)
打印
calculating date: 2015-02-09 00:00:00
1.1275
calculating date: 2015-02-10 00:00:00
1.1297
(当然,这些值与问题中的值不同,因为数据集被修改了)
https://stackoverflow.com/questions/49690613
复制相似问题