pandas中的reset_index()


數據清洗時,會將帶空值的行刪除,此時DataFrame或Series類型的數據不再是連續的索引,可以使用reset_index()重置索引

import pandas as pd
import numpy as np
df = pd.DataFrame(np.arange(20).reshape(5,4),index=[1,3,4,6,8])
print(df)

 reset_index()重置索引:

print(df.reset_index())

在獲得新的index,原來的index變成數據列,保留下來。

不想保留原來的index,使用參數 drop=True,默認 False。

print(df.reset_index(drop=True))
    0   1   2   3
0   0   1   2   3
1   4   5   6   7
2   8   9  10  11
3  12  13  14  15
4  16  17  18  19

 

 

來自:https://blog.csdn.net/qq_36523839/article/details/80640139


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM