I am trying to dynamically create a column name with some suffix added. I can do it in for loop but I think it will be inefficient. Is there a way to do it dynamically instead.
from pandas import Timestamp
import pandas as pd
df = pd.DataFrame({'B': range(1,6),'A':['A','A','A','B','B'],'D':[2,3,4,5,6]})
df['C'] = [Timestamp('20130101 09:00:00'),
Timestamp('20130101 09:00:02'),
Timestamp('20130102 09:00:03'),
Timestamp('20130101 09:00:05'),
Timestamp('20130101 09:00:06')]
I can do it in multiple step like :
df['D_2days']=df.groupby('A').rolling('4d',on='C')['D'].sum().values
df['B_2days']=df.groupby('A').rolling('4d',on='C')['B'].sum().values
Is there a way to do it in 1 go. I have more than 1000 columns that needs to be summed and having a loop might not be a great idea.
Thank you, Sam