![](/img/trans.png)
[英]How to find Date of 52 Week High and date of 52 Week low using pandas dataframe (Python)?
[英]Get the date when it was 52 week low and high
我在下面有这样的年度数据,包括日期、sc_code 和价格
2021-08-16,542942,1.36
2021-08-16,542948,0.07
2021-08-16,542963,12.09
2021-08-16,542967,17.35
2021-08-16,542969,9.86
2021-08-16,543079,9.76
2021-08-16,543150,5.99
2021-08-16,543144,0.03
2021-08-16,543092,0.09
2021-08-16,936967,1149.88
2021-08-16,936720,1199.93
2021-08-16,937499,945.00
2021-08-16,935862,1182.10
2021-08-16,935319,1870.00
我使用下面的高值和低值
df_temp = df_data.groupby(['sc_code'], sort=False)['close'].agg([('L','min'),('H', 'max')]).add_prefix('close').reset_index().copy()
我想知道高低的日期。
你能帮忙吗
通过以下方式使用DataFrameGroupBy.idxmax
DataFrameGroupBy.idxmin
作为索引:
df_temp = (df_data.groupby(['sc_code'], sort=False).agg(Lmin=('close','min'),
Hmax=('close','max'),
Ldate=('close','idxmin'),
Hdate=('close','idxmax'))
.reset_index()
.copy())
编辑:老歌 pandas 版本的解决方案:
df_temp = (df_data.groupby(['sc_code'], sort=False)['close'].agg([('Lmin','min'),
('Hmax','max'),
('Ldate','idxmin'),
('Hdate','idxmax')])
.reset_index()
.copy())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.