gpt4 book ai didi

python - 屏蔽 pandas DataFrame 中最大值之前出现的所有值

转载 作者:太空宇宙 更新时间:2023-11-03 12:35:47 26 4
gpt4 key购买 nike

我想从 pd.DataFrame.idxmax 中获取结果并使用它来更改具有最大值的索引之前的值。

如果我有df:

            Mule Creek  Saddle Mtn.  Calvert Creek
Date
2011-05-01 23.400000 35.599998 8.6
2011-05-02 23.400000 35.599998 8.0
2011-05-03 23.400000 35.700001 7.6
2011-05-04 23.400000 50.000000 7.1
2011-05-05 23.100000 35.799999 6.4
2011-05-06 23.000000 35.799999 5.7
2011-05-07 40.000000 35.900002 4.7
2011-05-08 23.100000 36.500000 12.0
2011-05-09 23.299999 37.500000 4.4
2011-05-10 23.200001 37.500000 3.6

我发现每列的最大值出现在:

max = df.idxmax()

我想在标识的最大值max all np.nan 之前制作值

期望的结果:

            Mule Creek  Saddle Mtn.  Calvert Creek
Date
2011-05-01 NaN NaN NaN
2011-05-02 NaN NaN NaN
2011-05-03 NaN NaN NaN
2011-05-04 NaN 50.000000 NaN
2011-05-05 NaN 35.799999 NaN
2011-05-06 NaN 35.799999 NaN
2011-05-07 40.000000 35.900002 NaN
2011-05-08 23.100000 36.500000 12.0
2011-05-09 23.299999 37.500000 4.4
2011-05-10 23.200001 37.500000 3.6

最佳答案

可以检查累积最大值与最大值相同的地方:

df.where(df.cummax() == df.max())

Mule Creek Saddle Mtn. Calvert Creek
Date
2011-05-01 NaN NaN NaN
2011-05-02 NaN NaN NaN
2011-05-03 NaN NaN NaN
2011-05-04 NaN 50.000000 NaN
2011-05-05 NaN 35.799999 NaN
2011-05-06 NaN 35.799999 NaN
2011-05-07 40.000000 35.900002 NaN
2011-05-08 23.100000 36.500000 12.0
2011-05-09 23.299999 37.500000 4.4
2011-05-10 23.200001 37.500000 3.6

关于python - 屏蔽 pandas DataFrame 中最大值之前出现的所有值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54409498/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com