Perhaps this is a mistake, you can use np.roll to achieve this:
In[11]: x.apply(lambda x: np.roll(x, 2), axis=1) Out[11]: col0 col1 col2 col3 col4 col5 col6 col7 0 NaN NaN 6.0 5.0 1.0 5.0 2.0 4.0 1 NaN NaN 8.0 8.0 9.0 6.0 7.0 2.0 2 NaN NaN 8.0 3.0 9.0 6.0 6.0 6.0 3 NaN NaN 8.0 4.0 4.0 4.0 8.0 9.0 4 NaN NaN 5.0 3.0 4.0 3.0 8.0 7.0
Speedwise, most likely, it’s faster to build df and reuse existing columns and pass the result to np.roll , since the data leads the constructor to a DataFrame :
In[12]: x = pd.DataFrame(np.roll(x, 2, axis=1), columns = x.columns) x Out[12]: col0 col1 col2 col3 col4 col5 col6 col7 0 NaN NaN 6.0 5.0 1.0 5.0 2.0 4.0 1 NaN NaN 8.0 8.0 9.0 6.0 7.0 2.0 2 NaN NaN 8.0 3.0 9.0 6.0 6.0 6.0 3 NaN NaN 8.0 4.0 4.0 4.0 8.0 9.0 4 NaN NaN 5.0 3.0 4.0 3.0 8.0 7.0
<strong> timings
In[13]: %timeit pd.DataFrame(np.roll(x, 2, axis=1), columns = x.columns) %timeit x.fillna(0).astype(int).shift(2, axis=1) 10000 loops, best of 3: 117 µs per loop 1000 loops, best of 3: 418 µs per loop
Therefore, building a new df with the result np.roll is faster than filling in NaN values other than int , and then shift ing.
source share