DataFrame的reset_index函数

来源:互联网 发布:数据库基础 编辑:程序博客网 时间:2024/05/18 16:19

notes-pandas-functions

reset_index函数

功能:

示例:

In [1]: dfOut[1]:          0         1         2         3         40 -0.127085 -0.538321  0.641609 -0.020957  0.0035031 -0.304994  0.157213  0.586962  0.251505  1.0224182 -0.239710  1.235562  0.917208 -0.964571 -1.1203313 -0.176416 -0.216204  0.433998 -0.366355 -0.261724# 删除若干行数据In [2]: df1 = df.loc[[0,2,3],:]In [3]: df1Out[3]:          0         1         2         3         40 -0.127085 -0.538321  0.641609 -0.020957  0.0035032 -0.239710  1.235562  0.917208 -0.964571 -1.1203313 -0.176416 -0.216204  0.433998 -0.366355 -0.261724# reset_index,原行索引作为一列保留,列名为indexIn [4]: df2 = df1.reset_index()In [5]: df2Out[5]:   index         0         1         2         3         40      0 -0.127085 -0.538321  0.641609 -0.020957  0.0035031      2 -0.239710  1.235562  0.917208 -0.964571 -1.1203312      3 -0.176416 -0.216204  0.433998 -0.366355 -0.261724# reset_index,通过函数 drop=True 删除原行索引In [6]: df3 = df1.reset_index(drop=True)In [7]: df3Out[7]:          0         1         2         3         40 -0.127085 -0.538321  0.641609 -0.020957  0.0035031 -0.239710  1.235562  0.917208 -0.964571 -1.1203312 -0.176416 -0.216204  0.433998 -0.366355 -0.261724
0 0