Another option is to combine concat with iloc:shift = 1X = pd.concat([X.iloc[-shift:], X.iloc[:-shift]])结果输出: A B C D E t4 26 82 13 14 34 50 34 54 56 0 78 11 12 87 78 23 12 22 78 35 0 72 31 33 84 25 14 56 0 4 时间使用以下设置来产生更大的DataFrame和计时功能:Using the following setup to produce a larger DataFrame and functions for timing:df = pd.concat([X]*10**5, ignore_index=True)def root1(df, shift): return df.reindex(np.roll(df.index, shift))def root2(df, shift): return pd.concat([df.iloc[-shift:], df.iloc[:-shift]])def ed_chum(df, num): return pd.DataFrame(np.roll(df, num, axis=0), np.roll(df.index, num), columns=df.columns)def divakar1(df, shift): return df.iloc[np.roll(np.arange(df.shape[0]), shift)]def divakar2(df, shift): idx = np.mod(np.arange(df.shape[0])-1,df.shape[0]) for _ in range(shift): df = df.iloc[idx] return df我得到以下计时:%timeit root1(df.copy(), 25)10 loops, best of 3: 61.3 ms per loop%timeit root2(df.copy(), 25)10 loops, best of 3: 26.4 ms per loop%timeit ed_chum(df.copy(), 25)10 loops, best of 3: 28.3 ms per loop%timeit divakar1(df.copy(), 25)10 loops, best of 3: 177 ms per loop%timeit divakar2(df.copy(), 25)1 loop, best of 3: 4.18 s per loop 这篇关于周期性地将 pandas 数据框下移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-02 17:03