1 In [16]: testset 2 Out[16]: 3 index uid iid rating timestamp 4 0 2 22 377 1 878887116 5 1 4 166 346 1 886397596 6 2 8 305 451 3 886324817 7 3 15 303 785 3 879485318 8 4 23 291 118 2 874833878 9 ... ... ... ... ... ... 10 19995 99980 864 685 4 888891900 11 19996 99982 279 64 1 875308510 12 19997 99983 646 750 3 888528902 13 19998 99990 806 421 4 882388897 14 19999 99999 12 203 3 879959583 15 16 [20000 rows x 5 columns] 17 #reset_index():從0開始重新設置dataframe的索引 18 In [17]: testset.reset_index() 19 Out[17]: 20 level_0 index uid iid rating timestamp 21 0 0 2 22 377 1 878887116 22 1 1 4 166 346 1 886397596 23 2 2 8 305 451 3 886324817 24 3 3 15 303 785 3 879485318 25 4 4 23 291 118 2 874833878 26 ... ... ... ... ... ... ... 27 19995 19995 99980 864 685 4 888891900 28 19996 19996 99982 279 64 1 875308510 29 19997 19997 99983 646 750 3 888528902 30 19998 19998 99990 806 421 4 882388897 31 19999 19999 99999 12 203 3 879959583 32 33 [20000 rows x 6 columns] 34 # drop屬性:默認值為False,不刪除原來的索引列 35 In [18]: testset.reset_index(drop=True) 36 Out[18]: 37 index uid iid rating timestamp 38 0 2 22 377 1 878887116 39 1 4 166 346 1 886397596 40 2 8 305 451 3 886324817 41 3 15 303 785 3 879485318 42 4 23 291 118 2 874833878 43 ... ... ... ... ... ... 44 19995 99980 864 685 4 888891900 45 19996 99982 279 64 1 875308510 46 19997 99983 646 750 3 888528902 47 19998 99990 806 421 4 882388897 48 19999 99999 12 203 3 879959583 49 50 [20000 rows x 5 columns]