How to print rolling window equation process from pandas dataframe in python?

后端 未结 2 963
眼角桃花
眼角桃花 2021-01-23 20:26

I created a pandas dataframe sample and it tried to sum for every 3 rows:

import pandas as pd
import numpy as np

d={\'A\':[100,110,120,175,164,169,155,153,156,20         


        
2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-23 21:06

    d={'A':[100,110,120,175,164,169,155,153,156,200]}
    df=pd.DataFrame(d)
    for ind in df.index:
            if ind == 0 or ind == 1:
                print("NaN")
            else:
                sumOf3Numbers = df["A"][ind-2]+df["A"][ind-1]+df["A"][ind]
                print("{}={}+{}+{}".format(sumOf3Numbers,df["A"][ind-2],df["A"][ind-1],df["A"][ind]))
    

提交回复
热议问题