[英]Fill subsequent values of python pandas dataframe with zeros when a condition is met
When a value in "Returns" column is less than "m"(say m=-0.5), I want the subsequent rows to be filled with zeros only till that month end.当“Returns”列中的值小于“m”(例如 m=-0.5)时,我希望后续行仅在该月底之前填充零。 How to accomplish this?
如何做到这一点? Thanks in advance.
提前致谢。
import pandas as pd
import numpy as np
from datetime import datetime, timedelta
date_today = datetime.now()
days = pd.date_range(date_today, date_today + timedelta(365), freq='D')
np.random.seed(seed=1111)
data = np.random.randint(1, high=100, size=len(days))
df = pd.DataFrame({'test': days, 'Price': data})
df = df.set_index('test')
df['Returns']=df['Price'].pct_change()
Use numpy.where
with mask for compare Returns
for less like m
and aggregate Series.cummax
with shift
for < m
per months by DatetimeIndex.to_period
:使用
numpy.where
和掩码进行比较Returns
不太像m
和聚合Series.cummax
,按DatetimeIndex.to_period
每月shift
< m
:
np.random.seed(123)
date_today = datetime.now()
days = pd.date_range(date_today, date_today + timedelta(365), freq='D')
np.random.seed(seed=1111)
data = np.random.randint(1, high=100, size=len(days))
df = pd.DataFrame({'test': days, 'Price': data})
df = df.set_index('test')
df['Returns']=df['Price'].pct_change()
m = -0.5
m = (df['Returns'].lt(m)
.groupby(df.index.to_period('m'))
.transform(lambda x: x.shift().cummax())
.fillna(False))
df['Returns'] = np.where(m, 0, df['Returns'])
print (df)
Price Returns
test
2021-11-22 14:32:35.550767 29 NaN
2021-11-23 14:32:35.550767 56 0.931034
2021-11-24 14:32:35.550767 82 0.464286
2021-11-25 14:32:35.550767 13 -0.841463
2021-11-26 14:32:35.550767 35 0.000000
... ...
2022-11-18 14:32:35.550767 47 0.000000
2022-11-19 14:32:35.550767 90 0.000000
2022-11-20 14:32:35.550767 20 0.000000
2022-11-21 14:32:35.550767 27 0.000000
2022-11-22 14:32:35.550767 51 0.000000
[366 rows x 2 columns]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.