1.diff() function df['1].diff()
The first result is NAN.
2.shift() function :df['1'].shift(-1) - df['1'] Represents the next line minus the previous line .
The last line of the result is NAN.
Series.shift(periods=1, freq=None, axis=0, fill_value=None)
shift() function : Default down (axis=0) To the right (axis=1).
If you want to move up and left , You need to set the parameters periods=-1, That is, move up :data.shift(periods=-1, axis=0)
Move to the left :data.shift(periods=-1, axis=1)
3.pandas Single column 、 Multiple columns for calculation
Perform formula calculation for multiple columns :
https://blog.csdn.net/zwhooo/article/details/79696558
4.python Middle up and down 、 Operation between columns
Change the line to the previous line :
df['B'] = math.sqrt(df['A'] * df['A'].shift(periods=-1, axis=0))
Report errors :
terms of settlement :https://stackoverflow.com/questions/42988348/typeerror-cannot-convert-the-series-to-class-float
Change the code to :
df["B"] = (df["A"] * df["A"].shift(periods=-1, axis=0)).apply(lambda x: math.sqrt(x))
5. Calculate the cumulative sum
cumsum() function : Seek before n Cumulative value of elements
df["B"] = df['A'].cumsum(axis=0) # Calculated by line