我有一个Pandas Dataframe,我试图添加一个新列,其中新列中的值取决于现有Dataframe中的某些条件。我拥有的数据框如下:
约会时间 | 打开 | 高 | 低 | 关 | 卷 |
---|---|---|---|---|---|
2020-06-02 16:30:00 | 1.25566 | 1.25696 | 1.25439 | 1.25634 | 2720 |
2020-06-02 17:00:00 | 1.25638 | 1.25683 | 1.25532 | 1.25614 | 2800 |
2020-06-02 17:30:00 | 1.25615 | 1.25699 | 1.25520 | 1.25565 | 2827 |
2020-06-02 18:00:00 | 1.25565 | 1.25598 | 1.25334 | 1.25341 | 2993 |
2020-06-02 18:30:00 | 1.25341 | 1.25385 | 1.25272 | 1.25287 | 1899年 |
2020-07-03 07:00:00 | 1.24651 | 1.24673 | 1.24596 | 1.24603 | 600 |
2020-07-03 07:30:00 | 1.24601 | 1.24641 | 1.24568 | 1.24594 | 487 |
2020-07-03 08:00:00 | 1.24593 | 1.24618 | 1.24580 | 1.24612 | 455 |
2020-07-03 08:30:00 | 1.24612 | 1.24667 | 1.24603 | 1.24666 | 552 |
2020-07-03 09:00:00 | 1.24666 | 1.24785 | 1.24623 | 1.24765 | 922 |
我想添加一个称为“ Signal”的新列,该列取决于以下if和else语句。我已经将它们写成一个函数:
def BullEngulf(df):
if (df['Open'] <= df['Close'].shift(1)) and (df['Close'] > df['Open'].shift(1)) and (df['Close'].shift(1) < df['Open'].shift(1)):
return 'Open'
else:
return '0'
然后,我尝试将功能应用如下:
df['Signal'] = df.apply(BullEngulf)
df
当我运行代码时,会出现以下错误消息:
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-62-f87af322a959> in <module>
5 return '0'
6
----> 7 df['Signal'] = df.apply(BullEngulf)
8 df
~\anaconda3\lib\site-packages\pandas\core\frame.py in apply(self, func, axis, raw, result_type, args, **kwds)
6876 kwds=kwds,
6877 )
-> 6878 return op.get_result()
6879
6880 def applymap(self, func) -> "DataFrame":
~\anaconda3\lib\site-packages\pandas\core\apply.py in get_result(self)
184 return self.apply_raw()
185
--> 186 return self.apply_standard()
187
188 def apply_empty_result(self):
~\anaconda3\lib\site-packages\pandas\core\apply.py in apply_standard(self)
293
294 try:
--> 295 result = libreduction.compute_reduction(
296 values, self.f, axis=self.axis, dummy=dummy, labels=labels
297 )
pandas\_libs\reduction.pyx in pandas._libs.reduction.compute_reduction()
pandas\_libs\reduction.pyx in pandas._libs.reduction.Reducer.get_result()
<ipython-input-62-f87af322a959> in BullEngulf(df)
1 def BullEngulf(df):
----> 2 if (df['Open'] <= df['Close'].shift(1)) and (df['Close'] > df['Open'].shift(1)) and (df['Close'].shift(1) < df['Open'].shift(1)):
3 return 'Open'
4 else:
5 return '0'
~\anaconda3\lib\site-packages\pandas\core\series.py in __getitem__(self, key)
869 key = com.apply_if_callable(key, self)
870 try:
--> 871 result = self.index.get_value(self, key)
872
873 if not is_scalar(result):
~\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_value(self, series, key)
4403 k = self._convert_scalar_indexer(k, kind="getitem")
4404 try:
-> 4405 return self._engine.get_value(s, k, tz=getattr(series.dtype, "tz", None))
4406 except KeyError as e1:
4407 if len(self) > 0 and (self.holds_integer() or self.is_boolean()):
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_value()
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_value()
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas\_libs\index_class_helper.pxi in pandas._libs.index.Int64Engine._check_type()
KeyError: 'Open'
请有人可以解释上面的代码有什么问题吗?
谢谢
尝试:
df['Signal'] = np.where((df['Open'] <= df['Close'].shift(1)) &
(df['Close'] > df['Open'].shift(1)) &
(df['Close'].shift(1) < df['Open'].shift(1)),
'Open', '0')
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句