1. 程式人生 > >重建索引報錯-python資料分析

重建索引報錯-python資料分析

obj3 = pd.Series(['blue', 'purple', 'yellow'], index=['0', '2', '4'])
obj3.reindex(range(6), method='ffill')

此時會爆出一大堆錯誤。

出錯原因是:之前 obj3 的索引是字串型別,重新索引是 range,int 型別。這樣資料型別不一樣,導致出錯

修改後:把最開始的 obj3 的 index 中的引號去掉,str 型變成 int 型。

obj3 = pd.Series(['blue', 'purple', 'yellow'], index=[0, 2, 4])
obj3.reindex(range(
6), method='ffill')