1. 程式人生 > 實用技巧 >python-dataframe重設索引,reset_index

python-dataframe重設索引,reset_index

 1 In [4]: testset
 2 Out[4]: 
 3        uid  iid  rating  timestamp
 4 2       22  377       1  878887116
 5 4      166  346       1  886397596
 6 8      305  451       3  886324817
 7 15     303  785       3  879485318
 8 23     291  118       2  874833878
 9 ...    ...  ...     ...        ...
10 99980  864  685       4  888891900
11
99982 279 64 1 875308510 12 99983 646 750 3 888528902 13 99990 806 421 4 882388897 14 99999 12 203 3 879959583 15 16 [20000 rows x 4 columns] 17 #reset_index()使dataframe的索引重新從0開始自增排列 18 In [5]: testset.reset_index() 19 Out[5]: 20 index uid iid rating timestamp
21 0 2 22 377 1 878887116 22 1 4 166 346 1 886397596 23 2 8 305 451 3 886324817 24 3 15 303 785 3 879485318 25 4 23 291 118 2 874833878 26 ... ... ... ... ... ... 27 19995 99980 864 685 4 888891900 28
19996 99982 279 64 1 875308510 29 19997 99983 646 750 3 888528902 30 19998 99990 806 421 4 882388897 31 19999 99999 12 203 3 879959583 32 33 [20000 rows x 5 columns]