Pandas:if语句比较2个 Dataframe 中的数据,如果条件满足,则将行数据复制到新的 Dataframe

huwehgph  于 2023-01-19  发布在  其他
关注(0)|答案(1)|浏览(149)

我已经为这个问题挣扎了好几个小时,所以我想我应该寻求帮助。
我试图在.itertuples函数中运行if语句,在该函数中,我希望选择df_high Dataframe 中的行,在与df_today Dataframe (具有单行数据的df)中的数据进行比较时,该 Dataframe 在if语句中传递了两个条件。
我正在运行一个交易算法,试图看看今天的高点是否高于前一个高点,然后收盘低于它(因此在我的if语句中有两个条件)。
目前为止我所拥有的代码如下所示:

# Create dictonary of assets
assets = ['EURUSD']

# Connect to MarketData.db
conn = db.connect("MarketData.db")
c = conn.cursor()

# DAILY REJECTION CALC PROCESS (notes) - 
#         loop: for row in rows, if todayhigh > high AND todayclose < high = TRUE, else FALSE
#         If TRUE, save df row to list >>> print list at end 

def calculate():

    validsweep = pd.DataFrame({
        'asset': pd.Series(dtype='str'),
        'date': pd.Series(dtype='str'),
        'open': pd.Series(dtype='float'),
        'high': pd.Series(dtype='float'),
        'low': pd.Series(dtype='float'),
        'close': pd.Series(dtype='float'),
        'fractal_high': pd.Series(dtype='int'),
        'fractal_low': pd.Series(dtype='int')})

    for x in assets:
        c.execute(f'SELECT * FROM {x}')
        rows = c.fetchall()
        df = pd.DataFrame(rows)
        df = df.rename(columns={0: "date", 1: "open", 2: "high", 3: "low", 4: "close", 5: "fractal_high", 6: "fractal_low"})
        df.insert(0, 'asset', str(x))

        df_high = df[df['fractal_high'] == 1]
        df_low = df[df['fractal_low'] == 1]
        df_today = df[df['date'] == "2022-06-27"]

        for row in df_high.itertuples(index=False):
            if ((df_today['high'].iloc[0] > df_high['high']) & (df_today['close'].iloc[0] < df_high['high'])).any():
                validsweep.combine([validsweep, df_high.iloc[[row]]])
        
    return

calculate()

我收到以下错误代码:

Traceback (most recent call last):
  File "/Users/baker/Desktop/Swing Rejection Strategy/testbed.py", line 54, in <module>
    calculate()
  File "/Users/baker/Desktop/Swing Rejection Strategy/testbed.py", line 44, in calculate
    validsweep.combine([validsweep, df_high.iloc[[row]]])
                                    ~~~~~~~~~~~~^^^^^^^
  File "/Users/baker/Desktop/Swing Rejection Strategy/venv/lib/python3.11/site-packages/pandas/core/indexing.py", line 1073, in __getitem__
    return self._getitem_axis(maybe_callable, axis=axis)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/baker/Desktop/Swing Rejection Strategy/venv/lib/python3.11/site-packages/pandas/core/indexing.py", line 1616, in _getitem_axis
    return self._get_list_axis(key, axis=axis)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/baker/Desktop/Swing Rejection Strategy/venv/lib/python3.11/site-packages/pandas/core/indexing.py", line 1587, in _get_list_axis
    return self.obj._take_with_is_copy(key, axis=axis)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/baker/Desktop/Swing Rejection Strategy/venv/lib/python3.11/site-packages/pandas/core/generic.py", line 3902, in _take_with_is_copy
    result = self._take(indices=indices, axis=axis)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/baker/Desktop/Swing Rejection Strategy/venv/lib/python3.11/site-packages/pandas/core/generic.py", line 3886, in _take
    new_data = self._mgr.take(
               ^^^^^^^^^^^^^^^
  File "/Users/baker/Desktop/Swing Rejection Strategy/venv/lib/python3.11/site-packages/pandas/core/internals/managers.py", line 972, in take
    else np.asanyarray(indexer, dtype=np.intp)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: invalid literal for int() with base 10: 'EURUSD'

看起来某个地方返回了一个整数,但我不确定为什么。我检查了一下,我的validsweepdf_high Dataframe 的每一列都有相同的.dtypes
我假设if语句的答案被转换为TRUE/FALSE整数,但我不知道如何阻止这种情况发生。
有没有办法让它起作用?

kkbh8khc

kkbh8khc1#

try with index = True。因为你在for循环中有df_high.itertuples(index=False),所以只会产生命名的元组。没有index。iloc()函数需要index作为参数。

for row in df_high.itertuples(index=True):

相关问题