函数reset_index():重置DataFrame 索引

Python基础知识 专栏收录该内容
65 篇文章 0 订阅

 

      在应用DataFrame的drop_duplicates()后,虽然删除了重复值,但索引未变,因此应用以下方法进行重置索引。

1、重置索引

      通过reset_index()方法我们可以重置索引,drop参数为True时,直接丢弃原来的索引,否则原来的索引新生成一列名为'index'的列:

df.reset_index(inplace=True,drop=True)

2、设置其他列为索引

      当然我们也可以用其他列为索引,通过set_index()方法:

df.set_index('id_new')

实例1:

import pandas as pd

aa=pd.DataFrame(np.random.rand(40).reshape(8,5)*100,columns=['w','w1','w2','w3','w4'])
aa.loc[2,'w'] = 5
aa.loc[2:3,'w'] = 8
aa.loc[5:6,'w'] = 8

aa
Out[1]: 
           w         w1         w2         w3         w4    2
0  53.180148   2.312015  90.797387  15.982018  60.222618  NaN
1  78.204511  26.337727  19.589254  97.695131  83.457314  NaN
2   8.000000  40.045206  52.661482  80.353082   4.183521  NaN
3   8.000000  55.443312  54.788416  62.092360  29.435788  5.0
4  89.641139  67.837247  85.974744  99.553476  31.426656  NaN
5   8.000000  68.345651  97.158906  26.325435  22.911433  NaN
6   8.000000  91.580530  24.930539   8.361266  27.962928  NaN
7  78.052998  39.045291  88.638165  34.327218  59.326833  NaN

aa=aa.drop([2],axis=1)

aa
Out[2]: 
           w         w1         w2         w3         w4
0  53.180148   2.312015  90.797387  15.982018  60.222618
1  78.204511  26.337727  19.589254  97.695131  83.457314
2   8.000000  40.045206  52.661482  80.353082   4.183521
3   8.000000  55.443312  54.788416  62.092360  29.435788
4  89.641139  67.837247  85.974744  99.553476  31.426656
5   8.000000  68.345651  97.158906  26.325435  22.911433
6   8.000000  91.580530  24.930539   8.361266  27.962928
7  78.052998  39.045291  88.638165  34.327218  59.326833


aad = aa.drop_duplicates('w',keep='first')

aad
Out[3]: 
           w         w1         w2         w3         w4
0  53.180148   2.312015  90.797387  15.982018  60.222618
1  78.204511  26.337727  19.589254  97.695131  83.457314
2   8.000000  40.045206  52.661482  80.353082   4.183521
4  89.641139  67.837247  85.974744  99.553476  31.426656
7  78.052998  39.045291  88.638165  34.327218  59.326833

aad.reset_index(inplace=True,drop=True)

aad
Out[4]: 
           w         w1         w2         w3         w4
0  53.180148   2.312015  90.797387  15.982018  60.222618
1  78.204511  26.337727  19.589254  97.695131  83.457314
2   8.000000  40.045206  52.661482  80.353082   4.183521
3  89.641139  67.837247  85.974744  99.553476  31.426656
4  78.052998  39.045291  88.638165  34.327218  59.326833

 实例2:

aad['new_index']=[5,6,7,8,9]

aad
Out[5]: 
           w         w1         w2         w3         w4  new_index
0  53.180148   2.312015  90.797387  15.982018  60.222618          5
1  78.204511  26.337727  19.589254  97.695131  83.457314          6
2   8.000000  40.045206  52.661482  80.353082   4.183521          7
3  89.641139  67.837247  85.974744  99.553476  31.426656          8
4  78.052998  39.045291  88.638165  34.327218  59.326833          9

aad.set_index('new_index')

Out[6]: 
                   w         w1         w2         w3         w4
new_index                                                       
5          53.180148   2.312015  90.797387  15.982018  60.222618
6          78.204511  26.337727  19.589254  97.695131  83.457314
7           8.000000  40.045206  52.661482  80.353082   4.183521
8          89.641139  67.837247  85.974744  99.553476  31.426656
9          78.052998  39.045291  88.638165  34.327218  59.326833

 

  • 7
    点赞
  • 1
    评论
  • 16
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值