通过迄今观察到的最大值归一化熊猫数据框列

问题描述 投票:0回答:1

我有一个带有时间索引的pandas数据框,并希望通过对该日期和时间观察到的最大值对一列的每一行进行归一化。

# an example input df
rng = pd.date_range('2020-01-01', periods=8)
a_lst = [2, 4, 3, 8, 2, 4, 10, 2]
df = pd.DataFrame({'date': rng, 'A': a_lst})
df.set_index('date', inplace=True, drop=True)

enter image description here

((一种可能的解决方案是遍历行,对过去的行进行子集,然后除以最大值[123],但效率不高)]

python pandas dataframe date normalization
1个回答
1
投票

您正在查看cummax

df['A_normalized'] = df['A']/df['A'].cummax()

输出:

             A  A_normalized
date                        
2020-01-01   2          1.00
2020-01-02   4          1.00
2020-01-03   3          0.75
2020-01-04   8          1.00
2020-01-05   2          0.25
2020-01-06   4          0.50
2020-01-07  10          1.00
2020-01-08   2          0.20
© www.soinside.com 2019 - 2024. All rights reserved.