pandas shift converts my column from integer to float.

后端 未结 5 1177
一整个雨季
一整个雨季 2020-12-10 12:14

shift converts my column from integer to float. It turns out that np.nan is float only. Is there any ways to keep shifted column as integer?

<
5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-10 13:05

    You can construct a numpy array by prepending a 0 to all but the last element of column a

    df.assign(b=np.append(0, df.a.values[:-1]))
    
       a  b
    0  0  0
    1  1  0
    2  2  1
    3  3  2
    4  4  3
    

提交回复
热议问题