今天我们来讨论 Pandas 中的 reset_index()
方法,包括为什么我们需要在 Pandas 中重置 DataFrame 的索引,以及我们应该如何应用该方法
在本文我们将使用 Kaggle 上的数据集样本 Animal Shelter Analytics 来作为我们的测试数据
Pandas 中的 Reset_Index() 是什么?
如果我们使用 Pandas 的 read_csv()
方法读取 csv 文件而不指定任何索引,则生成的 DataFrame 将具有默认的基于整数的索引,第一行从 0 开始,随后每行增加 1:
import pandas as pd
import numpy as npdf = pd.read_csv('Austin_Animal_Center_Intakes.csv').head()
df
Output:
Animal IDNameDateTimeMonthYearFound LocationIntake TypeIntake ConditionAnimal TypeSex upon IntakeAge upon IntakeBreedColor
0A786884*Brock01/03/2019 04:19:00 PM01/03/2019 04:19:00 PM2501 Magin Meadow Dr in Austin (TX)StrayNormalDogNeutered Male2 yearsBeagle MixTricolor
1A706918Belle07/05/2015 12:59:00 PM07/05/2015 12:59:00 PM9409 Bluegrass Dr in Austin (TX)StrayNormalDogSpayed Female8 yearsEnglish Springer SpanielWhite/Liver
2A724273Runster04/14/2016 06:43:00 PM04/14/2016 06:43:00 PM2818 Palomino Trail in Austin (TX)StrayNormalDogIntact Male11 monthsBasenji MixSable/White
3A665644NaN10/21/2013 07:59:00 AM10/21/2013 07:59:00 AMAustin (TX)StraySickCatIntact Female4 weeksDomestic Shorthair MixCalico
4A682524Rio06/29/2014 10:38:00 AM06/29/2014 10:38:00 AM800 Grove Blvd in Austin (TX)StrayNormalDogNeutered Male4 yearsDoberman Pinsch/Australian Cattle DogTan/Gray
在某些情况下,我们可能希望拥有更有意义的行标签,因此我们将选择 DataFrame 的其中一列作为 DataFrame 索引。我们可以使用
read_csv()
方法的 index_col 参数直接执行此操作:df = pd.read_csv('Austin_Animal_Center_Intakes.csv', index_col='Animal ID').head()
df
Output:
Animal IDNameDateTimeMonthYearFound LocationIntake TypeIntake ConditionAnimal TypeSex upon IntakeAge upon IntakeBreedColor
A786884*Brock01/03/2019 04:19:00 PM01/03/2019 04:19:00 PM2501 Magin Meadow Dr in Austin (TX)StrayNormalDogNeutered Male2 yearsBeagle MixTricolor
A706918Belle07/05/2015 12:59:00 PM07/05/2015 12:59:00 PM9409 Bluegrass Dr in Austin (TX)StrayNormalDogSpayed Female8 yearsEnglish Springer SpanielWhite/Liver
A724273Runster04/14/2016 06:43:00 PM04/14/2016 06:43:00 PM2818 Palomino Trail in Austin (TX)StrayNormalDogIntact Male11 monthsBasenji MixSable/White
A665644NaN10/21/2013 07:59:00 AM10/21/2013 07:59:00 AMAustin (TX)StraySickCatIntact Female4 weeksDomestic Shorthair MixCalico
A682524Rio06/29/2014 10:38:00 AM06/29/2014 10:38:00 AM800 Grove Blvd in Austin (TX)StrayNormalDogNeutered Male4 yearsDoberman Pinsch/Australian Cattle DogTan/Gray
或者我们还可以使用 set_index() 方法将 DataFrame 的任何列设置为 DataFrame 索引:
df = pd.read_csv('Austin_Animal_Center_Intakes.csv').head()
df.set_index('Animal ID', inplace=True)
df
Output:
Animal IDNameDateTimeMonthYearFound LocationIntake TypeIntake ConditionAnimal TypeSex upon IntakeAge upon IntakeBreedColor
A786884*Brock01/03/2019 04:19:00 PM01/03/2019 04:19:00 PM2501 Magin Meadow Dr in Austin (TX)StrayNormalDogNeutered Male2 yearsBeagle MixTricolor
A706918Belle07/05/2015 12:59:00 PM07/05/2015 12:59:00 PM9409 Bluegrass Dr in Austin (TX)StrayNormalDogSpayed Female8 yearsEnglish Springer SpanielWhite/Liver
A724273Runster04/14/2016 06:43:00 PM04/14/2016 06:43:00 PM2818 Palomino Trail in Austin (TX)StrayNormalDogIntact Male11 monthsBasenji MixSable/White
A665644NaN10/21/2013 07:59:00 AM10/21/2013 07:59:00 AMAustin (TX)StraySickCatIntact Female4 weeksDomestic Shorthair MixCalico
A682524Rio06/29/2014 10:38:00 AM06/29/2014 10:38:00 AM800 Grove Blvd in Austin (TX)StrayNormalDogNeutered Male4 yearsDoberman Pinsch/Australian Cattle DogTan/Gray
如果在某个时候我们需要恢复默认的数字索引呢,这时就可以使用 reset_index()函数了
df.reset_index()
Output:
Animal IDNameDateTimeMonthYearFound LocationIntake TypeIntake ConditionAnimal TypeSex upon IntakeAge upon IntakeBreedColor
0A786884*Brock01/03/2019 04:19:00 PM01/03/2019 04:19:00 PM2501 Magin Meadow Dr in Austin (TX)StrayNormalDogNeutered Male2 yearsBeagle MixTricolor
1A706918Belle07/05/2015 12:59:00 PM07/05/2015 12:59:00 PM9409 Bluegrass Dr in Austin (TX)StrayNormalDogSpayed Female8 yearsEnglish Springer SpanielWhite/Liver
2A724273Runster04/14/2016 06:43:00 PM04/14/2016 06:43:00 PM2818 Palomino Trail in Austin (TX)StrayNormalDogIntact Male11 monthsBasenji MixSable/White
3A665644NaN10/21/2013 07:59:00 AM10/21/2013 07:59:00 AMAustin (TX)StraySickCatIntact Female4 weeksDomestic Shorthair MixCalico
4A682524Rio06/29/2014 10:38:00 AM06/29/2014 10:38:00 AM800 Grove Blvd in Austin (TX)StrayNormalDogNeutered Male4 yearsDoberman Pinsch/Australian Cattle DogTan/Gray
此方法的默认行为包括用默认的基于整数的索引替换现有的 DataFrame 索引,并将旧索引转换为与旧索引同名的新列(或名称索引)。此外,默认情况下,reset_index() 方法会从 MultiIndex 中删除所有级别并且不会影响原始 DataFrame 数据,而是创建一个新的
何时使用 Reset_Index() 方法 reset_index() 方法将 DataFrame 索引重置为默认数字索引,在以下情况下特别有用:
- 执行数据整理时——尤其是过滤数据或删除缺失值等预处理操作,会导致较小的 DataFrame 具有不再连续的数字索引
- 当索引应该被视为一个常见的 DataFrame 列时
- 当索引标签没有提供有关数据的任何有价值的信息时
level
此参数采用整数、字符串、元组或列表作为可能的数据类型,并且仅适用于具有 MultiIndex 的 DataFrame,如下所示:
df_multiindex = pd.read_csv('Austin_Animal_Center_Intakes.csv', index_col=['Animal ID', 'Name']).head()
df_multiindex
Output:
Animal IDNameDateTimeMonthYearFound LocationIntake TypeIntake ConditionAnimal TypeSex upon IntakeAge upon IntakeBreedColor
A786884*Brock01/03/2019 04:19:00 PM01/03/2019 04:19:00 PM2501 Magin Meadow Dr in Austin (TX)StrayNormalDogNeutered Male2 yearsBeagle MixTricolor
A706918Belle07/05/2015 12:59:00 PM07/05/2015 12:59:00 PM9409 Bluegrass Dr in Austin (TX)StrayNormalDogSpayed Female8 yearsEnglish Springer SpanielWhite/Liver
A724273Runster04/14/2016 06:43:00 PM04/14/2016 06:43:00 PM2818 Palomino Trail in Austin (TX)StrayNormalDogIntact Male11 monthsBasenji MixSable/White
A665644NaN10/21/2013 07:59:00 AM10/21/2013 07:59:00 AMAustin (TX)StraySickCatIntact Female4 weeksDomestic Shorthair MixCalico
A682524Rio06/29/2014 10:38:00 AM06/29/2014 10:38:00 AM800 Grove Blvd in Austin (TX)StrayNormalDogNeutered Male4 yearsDoberman Pinsch/Australian Cattle DogTan/Gray
事实上,如果现在我们检查上面 DataFrame 的索引,我们会发现它不是一个常见的 DataFrame 索引,而是一个 MultiIndex 对象:
df_multiindex.index
Output:
MultiIndex([('A786884','*Brock'),
('A706918','Belle'),
('A724273', 'Runster'),
('A665644',nan),
('A682524','Rio')],
names=['Animal ID', 'Name'])
默认情况下,reset_index() 方法参数 level (level=None) 会移除 MultiIndex 的所有级别:
df_multiindex.reset_index()
【Pandas 重置索引深度总结】Output:
Animal IDNameDateTimeMonthYearFound LocationIntake TypeIntake ConditionAnimal TypeSex upon IntakeAge upon IntakeBreedColor
0A786884*Brock01/03/2019 04:19:00 PM01/03/2019 04:19:00 PM2501 Magin Meadow Dr in Austin (TX)StrayNormalDogNeutered Male2 yearsBeagle MixTricolor
1A706918Belle07/05/2015 12:59:00 PM07/05/2015 12:59:00 PM9409 Bluegrass Dr in Austin (TX)StrayNormalDogSpayed Female8 yearsEnglish Springer SpanielWhite/Liver
2A724273Runster04/14/2016 06:43:00 PM04/14/2016 06:43:00 PM2818 Palomino Trail in Austin (TX)StrayNormalDogIntact Male11 monthsBasenji MixSable/White
3A665644NaN10/21/2013 07:59:00 AM10/21/2013 07:59:00 AMAustin (TX)StraySickCatIntact Female4 weeksDomestic Shorthair MixCalico
4A682524Rio06/29/2014 10:38:00 AM06/29/2014 10:38:00 AM800 Grove Blvd in Austin (TX)StrayNormalDogNeutered Male4 yearsDoberman Pinsch/Australian Cattle DogTan/Gray
我们看到 DataFrame 的两个索引都被转换为通用 DataFrame 列,而索引被重置为默认的基于整数的索引
相反,如果我们显式传递 level 的值,则此参数会从 DataFrame 索引中删除选定的级别,并将它们作为常见的 DataFrame 列返回(除非我们选择使用 drop 参数从 DataFrame 中完全删除此信息)。比较以下操作:
df_multiindex.reset_index(level='Animal ID')
Output:
NameAnimal IDDateTimeMonthYearFound LocationIntake TypeIntake ConditionAnimal TypeSex upon IntakeAge upon IntakeBreedColor
*BrockA78688401/03/2019 04:19:00 PM01/03/2019 04:19:00 PM2501 Magin Meadow Dr in Austin (TX)StrayNormalDogNeutered Male2 yearsBeagle MixTricolor
BelleA70691807/05/2015 12:59:00 PM07/05/2015 12:59:00 PM9409 Bluegrass Dr in Austin (TX)StrayNormalDogSpayed Female8 yearsEnglish Springer SpanielWhite/Liver
RunsterA72427304/14/2016 06:43:00 PM04/14/2016 06:43:00 PM2818 Palomino Trail in Austin (TX)StrayNormalDogIntact Male11 monthsBasenji MixSable/White
NaNA66564410/21/2013 07:59:00 AM10/21/2013 07:59:00 AMAustin (TX)StraySickCatIntact Female4 weeksDomestic Shorthair MixCalico
RioA68252406/29/2014 10:38:00 AM06/29/2014 10:38:00 AM800 Grove Blvd in Austin (TX)StrayNormalDogNeutered Male4 yearsDoberman Pinsch/Australian Cattle DogTan/Gray
最开始 Animal ID 是 DataFrame 的索引之一,当我们设置 level 参数后,将其从索引中删除并作为称为 Animal ID 的公共列插入到 DataFrame 中
df_multiindex.reset_index(level='Name')
Output:
Animal IDNameDateTimeMonthYearFound LocationIntake TypeIntake ConditionAnimal TypeSex upon IntakeAge upon IntakeBreedColor
A786884*Brock01/03/2019 04:19:00 PM01/03/2019 04:19:00 PM2501 Magin Meadow Dr in Austin (TX)StrayNormalDogNeutered Male2 yearsBeagle MixTricolor
A706918Belle07/05/2015 12:59:00 PM07/05/2015 12:59:00 PM9409 Bluegrass Dr in Austin (TX)StrayNormalDogSpayed Female8 yearsEnglish Springer SpanielWhite/Liver
A724273Runster04/14/2016 06:43:00 PM04/14/2016 06:43:00 PM2818 Palomino Trail in Austin (TX)StrayNormalDogIntact Male11 monthsBasenji MixSable/White
A665644NaN10/21/2013 07:59:00 AM10/21/2013 07:59:00 AMAustin (TX)StraySickCatIntact Female4 weeksDomestic Shorthair MixCalico
A682524Rio06/29/2014 10:38:00 AM06/29/2014 10:38:00 AM800 Grove Blvd in Austin (TX)StrayNormalDogNeutered Male4 yearsDoberman Pinsch/Australian Cattle DogTan/Gray
在这里,Name 最初是 DataFrame 的索引之一,设置完level参数后,就变成了一个常用的列,叫做Name
drop
此参数决定在索引重置后是否将旧索引保留为通用 DataFrame 列,或者将其从 DataFrame 中完全删除。默认情况下 (drop=False) 是进行保留的,正如我们在前面的所有示例中看到的那样。否则,如果我们不想将旧索引保留为列,我们可以在索引重置后将其从 DataFrame 中完全删除(drop=True):
df
Output:
Animal IDNameDateTimeMonthYearFound LocationIntake TypeIntake ConditionAnimal TypeSex upon IntakeAge upon IntakeBreedColor
A786884*Brock01/03/2019 04:19:00 PM01/03/2019 04:19:00 PM2501 Magin Meadow Dr in Austin (TX)StrayNormalDogNeutered Male2 yearsBeagle MixTricolor
A706918Belle07/05/2015 12:59:00 PM07/05/2015 12:59:00 PM9409 Bluegrass Dr in Austin (TX)StrayNormalDogSpayed Female8 yearsEnglish Springer SpanielWhite/Liver
A724273Runster04/14/2016 06:43:00 PM04/14/2016 06:43:00 PM2818 Palomino Trail in Austin (TX)StrayNormalDogIntact Male11 monthsBasenji MixSable/White
A665644NaN10/21/2013 07:59:00 AM10/21/2013 07:59:00 AMAustin (TX)StraySickCatIntact Female4 weeksDomestic Shorthair MixCalico
A682524Rio06/29/2014 10:38:00 AM06/29/2014 10:38:00 AM800 Grove Blvd in Austin (TX)StrayNormalDogNeutered Male4 yearsDoberman Pinsch/Australian Cattle DogTan/Gray
增加 drop 参数
df.reset_index(drop=True)
Output:
NameDateTimeMonthYearFound LocationIntake TypeIntake ConditionAnimal TypeSex upon IntakeAge upon IntakeBreedColor
0*Brock01/03/2019 04:19:00 PM01/03/2019 04:19:00 PM2501 Magin Meadow Dr in Austin (TX)StrayNormalDogNeutered Male2 yearsBeagle MixTricolor
1Belle07/05/2015 12:59:00 PM07/05/2015 12:59:00 PM9409 Bluegrass Dr in Austin (TX)StrayNormalDogSpayed Female8 yearsEnglish Springer SpanielWhite/Liver
2Runster04/14/2016 06:43:00 PM04/14/2016 06:43:00 PM2818 Palomino Trail in Austin (TX)StrayNormalDogIntact Male11 monthsBasenji MixSable/White
3NaN10/21/2013 07:59:00 AM10/21/2013 07:59:00 AMAustin (TX)StraySickCatIntact Female4 weeksDomestic Shorthair MixCalico
4Rio06/29/2014 10:38:00 AM06/29/2014 10:38:00 AM800 Grove Blvd in Austin (TX)StrayNormalDogNeutered Male4 yearsDoberman Pinsch/Australian Cattle DogTan/Gray
在上面的 DataFrame 中,旧索引中包含的信息已完全从 DataFrame 中删除了
drop 参数也适用于具有 MultiIndex 的 DataFrame,就像我们之前创建的那样:
df_multiindex
Output:
Animal IDNameDateTimeMonthYearFound LocationIntake TypeIntake ConditionAnimal TypeSex upon IntakeAge upon IntakeBreedColor
A786884*Brock01/03/2019 04:19:00 PM01/03/2019 04:19:00 PM2501 Magin Meadow Dr in Austin (TX)StrayNormalDogNeutered Male2 yearsBeagle MixTricolor
A706918Belle07/05/2015 12:59:00 PM07/05/2015 12:59:00 PM9409 Bluegrass Dr in Austin (TX)StrayNormalDogSpayed Female8 yearsEnglish Springer SpanielWhite/Liver
A724273Runster04/14/2016 06:43:00 PM04/14/2016 06:43:00 PM2818 Palomino Trail in Austin (TX)StrayNormalDogIntact Male11 monthsBasenji MixSable/White
A665644NaN10/21/2013 07:59:00 AM10/21/2013 07:59:00 AMAustin (TX)StraySickCatIntact Female4 weeksDomestic Shorthair MixCalico
A682524Rio06/29/2014 10:38:00 AM06/29/2014 10:38:00 AM800 Grove Blvd in Austin (TX)StrayNormalDogNeutered Male4 yearsDoberman Pinsch/Australian Cattle DogTan/Gray
增加 drop 参数
df_multiindex.reset_index(drop=True)
Output:
DateTimeMonthYearFound LocationIntake TypeIntake ConditionAnimal TypeSex upon IntakeAge upon IntakeBreedColor
001/03/2019 04:19:00 PM01/03/2019 04:19:00 PM2501 Magin Meadow Dr in Austin (TX)StrayNormalDogNeutered Male2 yearsBeagle MixTricolor
107/05/2015 12:59:00 PM07/05/2015 12:59:00 PM9409 Bluegrass Dr in Austin (TX)StrayNormalDogSpayed Female8 yearsEnglish Springer SpanielWhite/Liver
204/14/2016 06:43:00 PM04/14/2016 06:43:00 PM2818 Palomino Trail in Austin (TX)StrayNormalDogIntact Male11 monthsBasenji MixSable/White
310/21/2013 07:59:00 AM10/21/2013 07:59:00 AMAustin (TX)StraySickCatIntact Female4 weeksDomestic Shorthair MixCalico
406/29/2014 10:38:00 AM06/29/2014 10:38:00 AM800 Grove Blvd in Austin (TX)StrayNormalDogNeutered Male4 yearsDoberman Pinsch/Australian Cattle DogTan/Gray
两个旧索引都已从 Dataframe 中完全删除,并且索引已重置为默认值
当然,我们可以结合 drop 和 level 参数,指定要从 DataFrame 中完全删除哪些旧索引:
df_multiindex.reset_index(level='Animal ID', drop=True)
Output:
DateTimeMonthYearFound LocationIntake TypeIntake ConditionAnimal TypeSex upon IntakeAge upon IntakeBreedColor
Name
*Brock01/03/2019 04:19:00 PM01/03/2019 04:19:00 PM2501 Magin Meadow Dr in Austin (TX)StrayNormalDogNeutered Male2 yearsBeagle MixTricolor
Belle07/05/2015 12:59:00 PM07/05/2015 12:59:00 PM9409 Bluegrass Dr in Austin (TX)StrayNormalDogSpayed Female8 yearsEnglish Springer SpanielWhite/Liver
Runster04/14/2016 06:43:00 PM04/14/2016 06:43:00 PM2818 Palomino Trail in Austin (TX)StrayNormalDogIntact Male11 monthsBasenji MixSable/White
NaN10/21/2013 07:59:00 AM10/21/2013 07:59:00 AMAustin (TX)StraySickCatIntact Female4 weeksDomestic Shorthair MixCalico
Rio06/29/2014 10:38:00 AM06/29/2014 10:38:00 AM800 Grove Blvd in Austin (TX)StrayNormalDogNeutered Male4 yearsDoberman Pinsch/Australian Cattle DogTan/Gray
旧索引 Animal ID 已从索引和 DataFrame 本身中删除,另一个索引 Name 被保留为 DataFrame 的当前索引
inplace
该参数决定是直接修改原来的 DataFrame 还是新建一个 DataFrame 对象。默认情况下,它会使用新索引 (inplace=False) 创建一个新的 DataFrame,并保持原始 DataFrame 不变。让我们使用默认参数再次运行 reset_index() 方法,然后将结果与原始 DataFrame 进行比较:
df.reset_index()
Output:
Animal IDNameDateTimeMonthYearFound LocationIntake TypeIntake ConditionAnimal TypeSex upon IntakeAge upon IntakeBreedColor
0A786884*Brock01/03/2019 04:19:00 PM01/03/2019 04:19:00 PM2501 Magin Meadow Dr in Austin (TX)StrayNormalDogNeutered Male2 yearsBeagle MixTricolor
1A706918Belle07/05/2015 12:59:00 PM07/05/2015 12:59:00 PM9409 Bluegrass Dr in Austin (TX)StrayNormalDogSpayed Female8 yearsEnglish Springer SpanielWhite/Liver
2A724273Runster04/14/2016 06:43:00 PM04/14/2016 06:43:00 PM2818 Palomino Trail in Austin (TX)StrayNormalDogIntact Male11 monthsBasenji MixSable/White
3A665644NaN10/21/2013 07:59:00 AM10/21/2013 07:59:00 AMAustin (TX)StraySickCatIntact Female4 weeksDomestic Shorthair MixCalico
4A682524Rio06/29/2014 10:38:00 AM06/29/2014 10:38:00 AM800 Grove Blvd in Austin (TX)StrayNormalDogNeutered Male4 yearsDoberman Pinsch/Australian Cattle DogTan/Gray
df
Output:
Animal IDNameDateTimeMonthYearFound LocationIntake TypeIntake ConditionAnimal TypeSex upon IntakeAge upon IntakeBreedColor
A786884*Brock01/03/2019 04:19:00 PM01/03/2019 04:19:00 PM2501 Magin Meadow Dr in Austin (TX)StrayNormalDogNeutered Male2 yearsBeagle MixTricolor
A706918Belle07/05/2015 12:59:00 PM07/05/2015 12:59:00 PM9409 Bluegrass Dr in Austin (TX)StrayNormalDogSpayed Female8 yearsEnglish Springer SpanielWhite/Liver
A724273Runster04/14/2016 06:43:00 PM04/14/2016 06:43:00 PM2818 Palomino Trail in Austin (TX)StrayNormalDogIntact Male11 monthsBasenji MixSable/White
A665644NaN10/21/2013 07:59:00 AM10/21/2013 07:59:00 AMAustin (TX)StraySickCatIntact Female4 weeksDomestic Shorthair MixCalico
A682524Rio06/29/2014 10:38:00 AM06/29/2014 10:38:00 AM800 Grove Blvd in Austin (TX)StrayNormalDogNeutered Male4 yearsDoberman Pinsch/Australian Cattle DogTan/Gray
即使我们将索引重置为运行第一段代码的默认数字,原始 DataFrame 仍然保持不变。 如果我们需要将原始 DataFrame 重新分配给对其应用 reset_index() 方法的结果,我们可以直接重新分配它(df = df.reset_index())或将参数 inplace=True 传递给该方法:
df.reset_index(inplace=True)
df
Output:
Animal IDNameDateTimeMonthYearFound LocationIntake TypeIntake ConditionAnimal TypeSex upon IntakeAge upon IntakeBreedColor
0A786884*Brock01/03/2019 04:19:00 PM01/03/2019 04:19:00 PM2501 Magin Meadow Dr in Austin (TX)StrayNormalDogNeutered Male2 yearsBeagle MixTricolor
1A706918Belle07/05/2015 12:59:00 PM07/05/2015 12:59:00 PM9409 Bluegrass Dr in Austin (TX)StrayNormalDogSpayed Female8 yearsEnglish Springer SpanielWhite/Liver
2A724273Runster04/14/2016 06:43:00 PM04/14/2016 06:43:00 PM2818 Palomino Trail in Austin (TX)StrayNormalDogIntact Male11 monthsBasenji MixSable/White
3A665644NaN10/21/2013 07:59:00 AM10/21/2013 07:59:00 AMAustin (TX)StraySickCatIntact Female4 weeksDomestic Shorthair MixCalico
4A682524Rio06/29/2014 10:38:00 AM06/29/2014 10:38:00 AM800 Grove Blvd in Austin (TX)StrayNormalDogNeutered Male4 yearsDoberman Pinsch/Australian Cattle DogTan/Gray
我们看到现在更改已直接应用于原始 DataFrame 之上了
应用实例:删除缺失值后重置索引 让我们将到目前为止讨论的所有内容付诸实践,看看当我们从 DataFrame 中删除缺失值时,重置 DataFrame 索引是如何有用的
首先,让我们恢复我们最开始时创建的第一个 DataFrame,它具有默认数字索引:
df = pd.read_csv('Austin_Animal_Center_Intakes.csv').head()
df
Output:
Animal IDNameDateTimeMonthYearFound LocationIntake TypeIntake ConditionAnimal TypeSex upon IntakeAge upon IntakeBreedColor
0A786884*Brock01/03/2019 04:19:00 PM01/03/2019 04:19:00 PM2501 Magin Meadow Dr in Austin (TX)StrayNormalDogNeutered Male2 yearsBeagle MixTricolor
1A706918Belle07/05/2015 12:59:00 PM07/05/2015 12:59:00 PM9409 Bluegrass Dr in Austin (TX)StrayNormalDogSpayed Female8 yearsEnglish Springer SpanielWhite/Liver
2A724273Runster04/14/2016 06:43:00 PM04/14/2016 06:43:00 PM2818 Palomino Trail in Austin (TX)StrayNormalDogIntact Male11 monthsBasenji MixSable/White
3A665644NaN10/21/2013 07:59:00 AM10/21/2013 07:59:00 AMAustin (TX)StraySickCatIntact Female4 weeksDomestic Shorthair MixCalico
4A682524Rio06/29/2014 10:38:00 AM06/29/2014 10:38:00 AM800 Grove Blvd in Austin (TX)StrayNormalDogNeutered Male4 yearsDoberman Pinsch/Australian Cattle DogTan/Gray
我们看到 DataFrame 中有一个缺失值,让我们使用 dropna() 方法删除具有缺失值的整行
df.dropna(inplace=True)
df
Output:
Animal IDNameDateTimeMonthYearFound LocationIntake TypeIntake ConditionAnimal TypeSex upon IntakeAge upon IntakeBreedColor
0A786884*Brock01/03/2019 04:19:00 PM01/03/2019 04:19:00 PM2501 Magin Meadow Dr in Austin (TX)StrayNormalDogNeutered Male2 yearsBeagle MixTricolor
1A706918Belle07/05/2015 12:59:00 PM07/05/2015 12:59:00 PM9409 Bluegrass Dr in Austin (TX)StrayNormalDogSpayed Female8 yearsEnglish Springer SpanielWhite/Liver
2A724273Runster04/14/2016 06:43:00 PM04/14/2016 06:43:00 PM2818 Palomino Trail in Austin (TX)StrayNormalDogIntact Male11 monthsBasenji MixSable/White
4A682524Rio06/29/2014 10:38:00 AM06/29/2014 10:38:00 AM800 Grove Blvd in Austin (TX)StrayNormalDogNeutered Male4 yearsDoberman Pinsch/Australian Cattle DogTan/Gray
该行已从 DataFrame 中删除,但是索引不再是连续的:0、1、2、4。让我们重新设置它:
df.reset_index()
Output:
indexAnimal IDNameDateTimeMonthYearFound LocationIntake TypeIntake ConditionAnimal TypeSex upon IntakeAge upon IntakeBreedColor
00A786884*Brock01/03/2019 04:19:00 PM01/03/2019 04:19:00 PM2501 Magin Meadow Dr in Austin (TX)StrayNormalDogNeutered Male2 yearsBeagle MixTricolor
11A706918Belle07/05/2015 12:59:00 PM07/05/2015 12:59:00 PM9409 Bluegrass Dr in Austin (TX)StrayNormalDogSpayed Female8 yearsEnglish Springer SpanielWhite/Liver
22A724273Runster04/14/2016 06:43:00 PM04/14/2016 06:43:00 PM2818 Palomino Trail in Austin (TX)StrayNormalDogIntact Male11 monthsBasenji MixSable/White
34A682524Rio06/29/2014 10:38:00 AM06/29/2014 10:38:00 AM800 Grove Blvd in Austin (TX)StrayNormalDogNeutered Male4 yearsDoberman Pinsch/Australian Cattle DogTan/Gray
现在索引是连续的了,但是由于我们没有显式传递 drop 参数,旧索引被转换为列,具有默认名称 index,下面让我们从 DataFrame 中完全删除旧索引:
df.reset_index(drop=True)
Output:
Animal IDNameDateTimeMonthYearFound LocationIntake TypeIntake ConditionAnimal TypeSex upon IntakeAge upon IntakeBreedColor
0A786884*Brock01/03/2019 04:19:00 PM01/03/2019 04:19:00 PM2501 Magin Meadow Dr in Austin (TX)StrayNormalDogNeutered Male2 yearsBeagle MixTricolor
1A706918Belle07/05/2015 12:59:00 PM07/05/2015 12:59:00 PM9409 Bluegrass Dr in Austin (TX)StrayNormalDogSpayed Female8 yearsEnglish Springer SpanielWhite/Liver
2A724273Runster04/14/2016 06:43:00 PM04/14/2016 06:43:00 PM2818 Palomino Trail in Austin (TX)StrayNormalDogIntact Male11 monthsBasenji MixSable/White
3A682524Rio06/29/2014 10:38:00 AM06/29/2014 10:38:00 AM800 Grove Blvd in Austin (TX)StrayNormalDogNeutered Male4 yearsDoberman Pinsch/Australian Cattle DogTan/Gray
现在我们彻底摆脱了无意义的旧索引,当前索引是连续的。最后一步是使用 inplace 参数将这些修改保存到我们的原始 DataFrame 中:
df.reset_index(drop=True, inplace=True)
df
Output:
Animal IDNameDateTimeMonthYearFound LocationIntake TypeIntake ConditionAnimal TypeSex upon IntakeAge upon IntakeBreedColor
0A786884*Brock01/03/2019 04:19:00 PM01/03/2019 04:19:00 PM2501 Magin Meadow Dr in Austin (TX)StrayNormalDogNeutered Male2 yearsBeagle MixTricolor
1A706918Belle07/05/2015 12:59:00 PM07/05/2015 12:59:00 PM9409 Bluegrass Dr in Austin (TX)StrayNormalDogSpayed Female8 yearsEnglish Springer SpanielWhite/Liver
2A724273Runster04/14/2016 06:43:00 PM04/14/2016 06:43:00 PM2818 Palomino Trail in Austin (TX)StrayNormalDogIntact Male11 monthsBasenji MixSable/White
3A682524Rio06/29/2014 10:38:00 AM06/29/2014 10:38:00 AM800 Grove Blvd in Austin (TX)StrayNormalDogNeutered Male4 yearsDoberman Pinsch/Australian Cattle DogTan/Gray
总结 今天我们从多个方面讨论了 reset_index() 方法
- reset_index() 方法的默认行为
- 如何恢复 DataFrame 的默认数字索引
- 何时使用 reset_index() 方法
- 该方法最重要的几个参数
- 如何使用 MultiIndex
- 如何从 DataFrame 中完全删除旧索引
- 如何将修改直接保存到原始 DataFrame 中
好了,这就是今天分享的全部内容,喜欢就点个赞吧
本文由mdnice多平台发布
推荐阅读
- 编程|想接私活时薪再翻一倍,建议根据这几个开源的SpringBoot项目(含小程序)改改~
- Java|想接私活薪资翻倍,根据这几个开源SpringBoot项目改改
- 出海季收官,速来 Get 全球化发展实操手册
- 第一时间快速了解 Kubernetes 1.25
- 程序员|Java进阶(mysql的事务隔离级别面试题)
- Python自动化办公之PDF拆分
- Python自动化办公之Excel对比工具
- 粒子滤波 PF(Particle filter)算法
- Python入门系列(一)安装环境