---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#数据位移
#数据位移 import pandas as pd data=[7532, 3937, 9447, 8765, 4564] index=[1, 2, 3, 4, 5] df=pd. DataFrame (data=data, index=index, columns=['OPPO' ]) #print (df). df['销量差']=df['OPPO' ]-df['OPPO'].shift(periods=1) #shift() 默认值为1,向下移动以为 print (df) #print (df['OPPO'].shift())
OPPO 销量差 1 7532 NaN 2 3937 -3595.0 3 9447 5510.0 4 8765 -682.0 5 4564 -4201.0