简体   繁体   中英

Shrink down row numbers in a Pandas Dataframe when removing rows in Python

Essentially this is the same question as in this link: How to automatically shrink down row numbers in R data frame when removing rows in R . However, I want to do this with a pandas dataframe. How would I go about doing so? There seems to be nothing similar to the rownames method of R dataframes in the Pandas library...Any ideas?

What you call "row number" is part of the index in pandas-speak, in this case a integer index. You can rebuild the index using

df = df.reset_index(drop=True)

还有另一种方法,它不会使用旧索引生成新列:

df.index=range(len(df.index))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM