Expanding Each Row In A Dataframe
Consider this simple example data = pd.DataFrame({'mydate' : [pd.to_datetime('2016-06-06'), pd.to_datetime('2016-06-02')], 'va
Solution 1:
I modify little bit of your function
defexpand_onerow(df,ndaysback=2,nhdaysfwd=2):new_index=pd.date_range(pd.to_datetime(df.index[0])-pd.Timedelta(days=ndaysback),pd.to_datetime(df.index[0])+pd.Timedelta(days=nhdaysfwd),freq='D')newdf=df.reindex(index=new_index,method='nearest')#New df with expanded indexreturnnewdfpd.concat([expand_onerow(data.loc[[x],:],ndaysback=2,nhdaysfwd=2)forx,_indata.iterrows()])Out[455]:value2016-05-31 22016-06-01 22016-06-02 22016-06-03 22016-06-04 22016-06-04 12016-06-05 12016-06-06 12016-06-07 12016-06-08 1
More info
Basically that one line equal to
l=[]
for x ,_ in data.iterrows():
l.append(expand_onerow(data.loc[[x],:], ndaysback =2, nhdaysfwd =2))# query outeachrowbyusing their index(x is the index foreachrow) and append theninto a empty list
pd.concat(l)# concat the list toone df at the end
Post a Comment for "Expanding Each Row In A Dataframe"