[英]How to select data after date which is the index of the max value of columns for each group by pandas?
ts_code low high
2021-08-01 881105.TI 1485.0 1629.0
2021-08-01 885452.TI 2216.0 2391.0
2021-08-01 885525.TI 7427.0 8552.0
2021-08-01 885641.TI 621.0 671.0
2021-08-08 881105.TI 1496.0 1623.0
2021-08-08 885452.TI 2297.0 2406.0
2021-08-08 885525.TI 7300.0 7868.0
2021-08-08 885641.TI 668.0 691.0
2021-08-15 881105.TI 1606.0 1776.0
2021-08-15 885452.TI 2352.0 2459.0
2021-08-15 885525.TI 7525.0 8236.0
2021-08-15 885641.TI 685.0 719.0
2021-08-22 881105.TI 1656.0 1804.0
2021-08-22 885452.TI 2329.0 2415.0
2021-08-22 885525.TI 7400.0 8270.0
2021-08-22 885641.TI 691.0 720.0
The type of index is datetime64[ns]
.索引的类型是datetime64[ns]
。
Goal目标
high
column for ts_code
group. select 日期之后的数据,这是ts_code
组的high
列的最大值索引。Expected预期的
ts_code low high
2021-08-22 881105.TI 1656.0 1804.0
2021-08-15 885452.TI 2352.0 2459.0
2021-08-22 885452.TI 2329.0 2415.0
2021-08-01 885525.TI 7427.0 8552.0
2021-08-08 885525.TI 7300.0 7868.0
2021-08-15 885525.TI 7525.0 8236.0
2021-08-22 885525.TI 7400.0 8270.0
2021-08-22 885641.TI 691.0 720.0
For example, the max date of 881105.TI
is 2021-08-22
and 885525.TI
is 2021-08-01
.例如, 2021-08-22
881105.TI
885525.TI
是2021-08-01
。 The ouput for each ts_code
is after the related max date.每个ts_code
的输出都在相关的最大日期之后。
Try and ref尝试并参考
Let us try transform
with idxmax
让我们尝试使用idxmax
进行transform
df1 = df.reset_index()
df1 = df[df.index >= df.groupby('ts_code')['high'].transform('idxmax')]
out = df1[df1.groupby('ts_code').cumcount()<=1]
out
ts_code low high
2021-08-01 885525.TI 7427.0 8552.0
2021-08-08 885525.TI 7300.0 7868.0
2021-08-15 885452.TI 2352.0 2459.0
2021-08-22 881105.TI 1656.0 1804.0
2021-08-22 885452.TI 2329.0 2415.0
2021-08-22 885641.TI 691.0 720.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.