I want to calculate the following equation:
calc = value_a(2D) - (value_b(0D) + value_b(1D))/10000
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**
and the result should be
1.1274875
but somwhow I am not sure how to access the proper data within my loop? I would like to keep my structure of the code.
The code looks like the following:
import pandas as pd
data = pd.read_csv('C:/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():
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['year'] in {'0D','1D','2D'}:
print(row['value'])
The data looks like the following:
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
'type'relating to things here? It seems completely superfluous, or it's relationship to your equation isn't really obvious from the example you have given. EDIT: In other words, it looks like you are just selecting the values corresponding to the'year', for'2D'etc..value_b(2D)look like? Your example doesn't allow me to infer the logic relating things. What other values can your columns have? More exactly, what other combination of values, and what exactly is the purpose of type? Notice, I can easily get your numbers just by using the'value'that corresponds to the'year'