首页 > 解决方案 > 如何使用 Pandas 查找具有条件的同一列中的第一个实例

问题描述

我有这样的股票价格数据

     ticker       date      close      volume    type  target_date
0     NVDA    1999-01-22    1.6086    18469934  STOCK          
1     NVDA    1999-01-25    1.6270     3477722  STOCK          
2     NVDA    1999-01-26    1.6822     2342848  STOCK          
3     NVDA    1999-01-27    1.5439     1678315  STOCK          
4     NVDA    1999-01-28    1.5349     1554613  STOCK  

我需要在“target_date”列中添加等于收盘价高于或等于收盘价 * 3 的第一个日期。我想找到收盘价变为三倍以上的第一个日期。我试过了:

df['target_date'] = df[df.close >= df.close * 3].drop_duplicates('ticker')['date']

但是在整列中得到了 NaT 值

Upd.1 我写的是

target_date = []
for i in df.itertuples():
    close = i.close
    date = i.date
    f1 = df.date > date
    f2 = df.close > close
    f = f1&f2
    result = df[f].drop_duplicates('ticker')['date']
    target_date.append(result.iloc[0])

并得到“IndexError:单个位置索引器超出范围”

UPD2 我想我做到了

target_date = []
for i in df.itertuples():
    close = i.close
    date = i.date
    f1 = df.date > date
    f2 = df.close > close
    f = f1&f2
    result = df[f].drop_duplicates('ticker')['date']
    try:
        target_date.append(result.iloc[0])
    except:
        target_date.append(pd.NaT)
df['target_date'] = target_date

但这是让它更优雅的方法吗?

标签: pythonpandasfiltering

解决方案


假设df数据框包含您的数据和一target_date列,则此代码应该可以解决问题:

for i, row in df.iterrows():
    rest = df.iloc[i+1:]  # the rest of the rows (next ones)
    x = rest[rest.close >= 3*row.close]
    df.loc[i, 'target_date'] = np.nan if len(x) == 0 else x.iloc[0].date

推荐阅读