简体   繁体   中英

Pandas: I want to create a column in a Time Series where the value depends on the previous row's value

I have a time series dataframe where I need to create a boolean-valued column, which is

  • True if the current value is more than 10% different from the previous row's value.
  • False otherwise

As you have not provided any sample data, so I am not 100% sure, what you exactly want. But here is what I think you need to do

df=pd.DataFrame([np.random.randn(10,1)],columns=['first'])
df['prev']=df.shift(1)
df['prev']-df['first']>df['first']*0.1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM