繁体   English   中英

如何使用 Pandas 在同一列中找到第一个实例

[英]How to find the first instance in the same column with condition with 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

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

假设df dataframe 包含您的数据和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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM