python - 如何使用 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
数据框包含您的数据和一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
推荐阅读
- javascript - 如何在 macOS 上使用水平滚动来模拟滑动删除?
- python - 警告:matplotlib.legend:没有找到带有标签的句柄放在图例中
- performance - 提高协同仿真 Adams/Matlab 的性能
- docusignapi - Docusign 连接 - 信封标志完成和 Docusign 触发 Https Post 请求之间的预期延迟时间是多少
- openshift - Openshift CLI:将 .kube 文件夹更改为不同的驱动器
- javascript - 需要讨论下一个 JS 浅选项的动态路由
- javascript - 使用 combineReducer 组合我的减速器后,动作未调度
- pypubsub - Laravel 框架发布订阅
- firebase - Flutter & Firebase:返回集合列表
- mysql - 如何获取外键列表以及该外键上的索引名称?