嵌套循环Python计算列表中的值

时间:2018-04-06 10:21:32

标签: python list pandas nested-loops

我想在嵌套循环中计算以下函数并保持这种方式

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(Pandas Version),但想使用我的嵌套方式。

更新:

我改变了我的功能并获得了适当的值,但仍然存在我多次而不是一次计算值的问题。我得到每个值b的值a,直到计算出正确的值,这是最后一个值。我只想保留最后一个值。

1.1275
1.1275
1.1275
1.1275
1.1275
1.1275
1.1275
1.1275
1.1275
1.1275
1.1275
1.1275
1.1275
1.1275
1.1275
1.1275
1.1274875 --> **Only this value should be shown**

代码如下所示:

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

1 个答案:

答案 0 :(得分:0)

这将是最小的:

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

(当然这些值与您问题中的值不同,因为数据集已被修改)