I have a pivot_table, e.g.
WEEK w1 W2 ... Wn
col_1
A 1 2 ... n
B 1 2 ... n
C 1 2 ... n
...
I wonder if i can get the difference of Wn & Wn-1 at once?
WEEK w1 W2 ... Wn
col_1
A 0 1 ... 1
B 0 1 ... 1
C 0 1 ... 1
...
I found pandas.DataFrame.diff()
but don't know how to use it correctly. Thanks for any suggestions!
The function diff calculates the difference between all rows and their previous one and returns a dataframe with equal size to the applied one.
This leaves your first row NaN because there is no previous row of the first row.If you want to calculate the difference between columns simply set df.diff(axis=1)
.This will return a dataframe with the first column NaN.If you want to find the difference between two columns only,apply the diff function to these two columns
df[:,-2:].diff(axis=1)
This select the last two columns of your dataframe and returns a new table with 2 columns,one NaN and the other with the difference Wn - Wn-1