Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Dataframe 带loc的Python循环_Dataframe_Loops - Fatal编程技术网

Dataframe 带loc的Python循环

Dataframe 带loc的Python循环,dataframe,loops,Dataframe,Loops,在运行执行循环操作的代码时,我遇到了以下错误。有人能指出我犯的错误吗?这样我就可以改正了 代码的目的是尝试查看下一行“股息”是否等于零,如果不等于零,则下一行“调整因子”将执行等式右侧的操作。我真的不知道该怎么修。请给我一些帮助,非常感谢 for i in range(data.shape[0]): if i == (data.shape[0]-1): continue data.loc[data['Dividend'].iloc[i+1] != 0, data['Ad

在运行执行循环操作的代码时,我遇到了以下错误。有人能指出我犯的错误吗?这样我就可以改正了

代码的目的是尝试查看下一行“股息”是否等于零,如果不等于零,则下一行“调整因子”将执行等式右侧的操作。我真的不知道该怎么修。请给我一些帮助,非常感谢

for i in range(data.shape[0]):
    if i == (data.shape[0]-1): 
    continue
    data.loc[data['Dividend'].iloc[i+1] != 0, data['Adjusting Factor'].iloc[i+1]] = (data['EQIX US EQUITY'].iloc[i] - data['Dividend'].iloc[i]) 
    data['Adjusted Premium'].iloc[i] = data['Put'].iloc[i] * data['Adjusting Factor']
keyrerror回溯(最近一次调用)
get\u loc中的~\Anaconda3\lib\site packages\pandas\core\index\base.py(self、key、method、tolerance)
2896尝试:
->2897自动返回发动机。获取位置(钥匙)
2898除按键错误外:
pandas/_libs/index.pyx在pandas中。_libs.index.DatetimeEngine.get_loc()
pandas/_libs/index.pyx在pandas中。_libs.index.DatetimeEngine.get_loc()
pandas/_libs/hashtable_class_helper.pxi在pandas._libs.hashtable.Int64HashTable.get_item()中
pandas/_libs/hashtable_class_helper.pxi在pandas._libs.hashtable.Int64HashTable.get_item()中
关键错误:1
在处理上述异常期间,发生了另一个异常:
KeyError回溯(最近一次呼叫最后一次)
get\u loc中的~\Anaconda3\lib\site packages\pandas\core\index\datetimes.py(self、key、method、tolerance)
1056尝试:
->1057返回索引。获取位置(自身、键、方法、公差)
1058除外(KeyError、ValueError、TypeError):
get\u loc中的~\Anaconda3\lib\site packages\pandas\core\index\base.py(self、key、method、tolerance)
2898除按键错误外:
->2899返回self.\u引擎。获取self.\u loc(self.\u可能\u cast\u索引器(键))
2900 indexer=self.get_indexer([key],method=method,tolerance=tolerance)
pandas/_libs/index.pyx在pandas中。_libs.index.DatetimeEngine.get_loc()
pandas/_libs/index.pyx在pandas中。_libs.index.DatetimeEngine.get_loc()
pandas/_libs/hashtable_class_helper.pxi在pandas._libs.hashtable.Int64HashTable.get_item()中
pandas/_libs/hashtable_class_helper.pxi在pandas._libs.hashtable.Int64HashTable.get_item()中
关键错误:1
在处理上述异常期间,发生了另一个异常:
TypeError回溯(最近一次调用上次)
在里面
12如果i==(data.shape[0]-1):#跳过最后一次运行以避免出现错误
13继续
--->14 data.loc[data['股息].iloc[i+1]!=0,data['调整系数].iloc[i+1]=(data['EQIX美国股票].iloc[i]-data['股息].iloc[i])/data['EQIX美国股票].iloc[i]
15数据['调整后的溢价].iloc[i]=数据['投入].iloc[i]*数据['调整系数']
16 data.loc[data['Adjust Factor'].iloc[i]!=data['Adjust Factor'].iloc[i-1],'Adjusted Contract Multiplier']=(data['Adjusted Contract Multiplier'].iloc[i-1])/data['adjustment Factor'].iloc[i]
~\Anaconda3\lib\site packages\pandas\core\index.py in\uuuuuu setitem\uuuuuuuu(self、key、value)
202其他:
203 key=com.apply\u如果可调用(key,self.obj)
-->204 indexer=self.\u get\u setitem\u索引器(键)
205带有索引器(索引器,值)的self.\u setitem\u
206
~\Anaconda3\lib\site packages\pandas\core\index.py在\u get\u setitem\u索引器中(self,key)
180如果isinstance(键、元组):
181试试看:
-->182返回self.\u convert\u元组(键,is\u setter=True)
183除索引错误外:
184通行证
~\Anaconda3\lib\site packages\pandas\core\index.py in\u convert\u元组(self、key、is\u setter)
260如果i>=self.obj.ndim:
261 raise IndexingError(“索引器太多”)
-->262 idx=self.\u将\u转换为\u索引器(k,轴=i,is\u setter=is\u setter)
263 keyidx.append(idx)
264返回元组(keyidx)
~\Anaconda3\lib\site packages\pandas\core\index.py in\u convert\u to\u indexer(self、obj、axis、is\u setter、raise\u缺失)
1286其他:
1287尝试:
->1288返回标签。获取位置(obj)
1289除LookupError外:
1290#仅当我们是setter时才允许找不到密钥
get\u loc中的~\Anaconda3\lib\site packages\pandas\core\index\datetimes.py(self、key、method、tolerance)
1063
1064尝试:
->1065邮票=时间戳(钥匙)
1066如果stamp.tzinfo不是None,self.tz不是None:
1067 stamp=stamp.tz_转换(self.tz)
pandas/_-libs/tslibs/timestaps.pyx在pandas中。_-libs.tslibs.timestaps.Timestamp.uu新的uu()
pandas/_-libs/tslibs/conversion.pyx在pandas中。_-libs.tslibs.conversion.conversion_-to_-tsobject()
TypeError:无法将类型的输入[True]转换为时间戳
好的,我不确定“调整系数”函数的计算是否正确,因为我不知道公式。 如果你写这个公式,我会修正的

但方法是:
您将diffident向前移动一行并计算。
我过去常常申请,因为这很容易

data['Dividend_befor']=data['Dividend'].shift(1).fillna(0)
def Adjusted_Premium_dividend(row):
    if(row['Dividend_befor']!=0):
        Adjusted_factor=row['EQIX US EQUITY'] - row['Dividend']
        Adjusted_Premium = row['Put'] * Adjusted_factor
        return Adjusted_Premium
    else:
        return 0
def adjustment_factor_dividend(row):
    if(row['Dividend_befor']!=0):
        Adjusted_factor=row['EQIX US EQUITY'] - row['Dividend']
        return Adjusted_factor
    else:
        return 0
data['Adjusted_factor']  = data.apply(adjustment_factor_dividend,axis=1)
data['Adjusted_Premium']  = data.apply(adjustment_factor_dividend,axis=1)
data

有人能在这里帮忙吗?你能展示你的df部分吗?数据框有太多的列和行。我担心这可能会令人困惑。然而,需要注意的是,它们都是数值。这个提示有用吗?谢谢你。你的方法解决了我的问题。
data['Dividend_befor']=data['Dividend'].shift(1).fillna(0)
def Adjusted_Premium_dividend(row):
    if(row['Dividend_befor']!=0):
        Adjusted_factor=row['EQIX US EQUITY'] - row['Dividend']
        Adjusted_Premium = row['Put'] * Adjusted_factor
        return Adjusted_Premium
    else:
        return 0
def adjustment_factor_dividend(row):
    if(row['Dividend_befor']!=0):
        Adjusted_factor=row['EQIX US EQUITY'] - row['Dividend']
        return Adjusted_factor
    else:
        return 0
data['Adjusted_factor']  = data.apply(adjustment_factor_dividend,axis=1)
data['Adjusted_Premium']  = data.apply(adjustment_factor_dividend,axis=1)
data