In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to reset the DataFrame index for Pandas. Xiaobian thinks it is quite practical, so share it with you for a reference. I hope you can gain something after reading this article.
import pandas as pdimport numpy as npdf = pd.DataFrame(np.arange(20).reshape((5, 4)),columns=['a', 'b', 'c', 'd'])#Get df: a b c d0 0 1 2 31 4 5 6 72 8 9 10 113 12 13 14 154 16 17 18 19#Rearrange them to get data in reverse index order df2 = df.sort_values ('a ', ascending=False)#Get df2: a b c d4 16 17 18 193 12 13 14 152 8 9 10 111 4 5 6 70 0 1 2 3
Let's index df2 so that its index starts at 0
Law 1:
Simple and crude:
df2.index = range(len(df2))#Output df2: a b c d0 16 17 18 191 12 13 14 152 8 9 10 113 4 5 6 74 0 1 2 3
Law 2:
df2 = df2.reset_index(drop=True) # drop=True means to delete the original index, otherwise a new column of 'index' data will be generated in the data table #Output df2: a b c d0 16 17 18 191 12 13 14 152 8 9 10 113 4 5 6 74 0 1 2 3
Law 3:
df2 = df2.reindex(labels=range(len(df)) #labels is the first parameter, you can omit #output df2 a b c d0 16 17 18 191 12 13 14 152 8 9 10 113 4 5 6 74 0 1 2 3#Note: df = df.reindex(index=[]), a new line on the original data structure (index is a new index, if the new data index exists in the original data, the original data will be referenced), filled with NaN by default (fill_value=0 is used to modify the fill value customization, here I set 0).# df = df.reindex(columns=[]), Create new columns on the original data structure, The method is the same as the new row
Method 4:
df2 = df2.set_index(keys=['a',' c']) #Use the data in columns a and c of the original data as indexes.# drop=True, Default, Delete the original data in the table after the data is used as an index # append=False, Default, Set the newly set index as the inner index, The original index is the outer index #Output df2, Note that columns a and c are indexes: b da c 16 18 17 1912 14 13 158 10 9 114 6 5 70 2 1 3
About "Pandas how to reset DataFrame index" This article is shared here, I hope the above content can be of some help to everyone, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.