Pandas的shift函數可以將DataFrame移動指定的位數。比如,
period參數指定移動的位數,axis參數指定軸向
import pandas as pd
df = pd.DataFrame({'a':[1,2,3], 'b':[4,5,6], 'c':[7,8,9]})
df.shift(periods=1, axis=0) # 將df整體向下移動一位
df.shift(periods=2, axis=1) # 將df整體向右移動兩位
計算每個小時的入口和出口人流量
# 這是從某時刻開始累積的人流量
entries_and_exits = pd.DataFrame({
'ENTRIESn': [3144312, 3144335, 3144353, 3144424, 3144594,
3144808, 3144895, 3144905, 3144941, 3145094],
'EXITSn': [1088151, 1088159, 1088177, 1088231, 1088275,
1088317, 1088328, 1088331, 1088420, 1088753]
})
# 計算每小時出口和入口的人流量
entries_and_exits - entries_and_exits.shift(1)