python - Use Panda's diff() against first column/row of data.frame -
assume have dataframe first column date , consecutive columns values adjusted on time. f.x. prognosis of wind specific date changes on time new information available.
my task compute difference in regards first column. principle similar pandas.dataframe.diff reference value not preceding column first.
so assuming dataframe looks this
date forecast1 forecast2 forecast3 1/1/15 5 3 7
i want result this:
date forecast1 forecast2 forecast3 1/1/15 nan -2 2
i hope explanation clear.
thank efforts.
just use pd.dataframe.sub :
in [108]: df=pd.dataframe(np.random.randint(0,6,(3,3)), columns=['forecast'+str(i) in range(1,4)], index=pd.date_range('2016/1/1',periods=3)) in [109]: df out[109]: forecast1 forecast2 forecast3 2016-01-01 5 5 5 2016-01-02 0 3 0 2016-01-03 2 4 2 in [110]: df.sub(df.forecast1,axis=0) out[110]: forecast1 forecast2 forecast3 2016-01-01 0 0 0 2016-01-02 0 3 0 2016-01-03 0 2 0
Comments
Post a Comment