Pandas 重置索引深度總結(jié)

今天我們來討論 Pandas 中的 `reset_index()` 方法,包括為什么我們需要在 Pandas 中重置 DataFrame 的索引例证,以及我們應(yīng)該如何應(yīng)用該方法 在本文我們將使用 Kaggle 上的數(shù)據(jù)集樣本 Animal Shelter Analytics 來作為我們的測試數(shù)據(jù) ## Pandas 中的 Reset_Index() 是什么? 如果我們使用 Pandas 的 `read_csv()` 方法讀取 csv 文件而不指定任何索引迷捧,則生成的 DataFrame 將具有默認(rèn)的基于整數(shù)的索引织咧,第一行從 0 開始,隨后每行增加 1: ```Python import pandas as pd import numpy as np df = pd.read_csv('Austin_Animal_Center_Intakes.csv').head() df ``` Output: ```Text Animal ID Name DateTime MonthYear Found Location Intake Type Intake Condition Animal Type Sex upon Intake Age upon Intake Breed Color 0 A786884 *Brock 01/03/2019 04:19:00 PM 01/03/2019 04:19:00 PM 2501 Magin Meadow Dr in Austin (TX) Stray Normal Dog Neutered Male 2 years Beagle Mix Tricolor 1 A706918 Belle 07/05/2015 12:59:00 PM 07/05/2015 12:59:00 PM 9409 Bluegrass Dr in Austin (TX) Stray Normal Dog Spayed Female 8 years English Springer Spaniel White/Liver 2 A724273 Runster 04/14/2016 06:43:00 PM 04/14/2016 06:43:00 PM 2818 Palomino Trail in Austin (TX) Stray Normal Dog Intact Male 11 months Basenji Mix Sable/White 3 A665644 NaN 10/21/2013 07:59:00 AM 10/21/2013 07:59:00 AM Austin (TX) Stray Sick Cat Intact Female 4 weeks Domestic Shorthair Mix Calico 4 A682524 Rio 06/29/2014 10:38:00 AM 06/29/2014 10:38:00 AM 800 Grove Blvd in Austin (TX) Stray Normal Dog Neutered Male 4 years Doberman Pinsch/Australian Cattle Dog Tan/Gray ``` 在某些情況下漠秋,我們可能希望擁有更有意義的行標(biāo)簽笙蒙,因此我們將選擇 DataFrame 的其中一列作為 DataFrame 索引。我們可以使用 `read_csv()` 方法的 index_col 參數(shù)直接執(zhí)行此操作: ```Python df = pd.read_csv('Austin_Animal_Center_Intakes.csv', index_col='Animal ID').head() df ``` Output: ```Text Animal ID Name DateTime MonthYear Found Location Intake Type Intake Condition Animal Type Sex upon Intake Age upon Intake Breed Color A786884 *Brock 01/03/2019 04:19:00 PM 01/03/2019 04:19:00 PM 2501 Magin Meadow Dr in Austin (TX) Stray Normal Dog Neutered Male 2 years Beagle Mix Tricolor A706918 Belle 07/05/2015 12:59:00 PM 07/05/2015 12:59:00 PM 9409 Bluegrass Dr in Austin (TX) Stray Normal Dog Spayed Female 8 years English Springer Spaniel White/Liver A724273 Runster 04/14/2016 06:43:00 PM 04/14/2016 06:43:00 PM 2818 Palomino Trail in Austin (TX) Stray Normal Dog Intact Male 11 months Basenji Mix Sable/White A665644 NaN 10/21/2013 07:59:00 AM 10/21/2013 07:59:00 AM Austin (TX) Stray Sick Cat Intact Female 4 weeks Domestic Shorthair Mix Calico A682524 Rio 06/29/2014 10:38:00 AM 06/29/2014 10:38:00 AM 800 Grove Blvd in Austin (TX) Stray Normal Dog Neutered Male 4 years Doberman Pinsch/Australian Cattle Dog Tan/Gray ``` 或者我們還可以使用 set_index() 方法將 DataFrame 的任何列設(shè)置為 DataFrame 索引: ```Python df = pd.read_csv('Austin_Animal_Center_Intakes.csv').head() df.set_index('Animal ID', inplace=True) df ``` Output: ```Text Animal ID Name DateTime MonthYear Found Location Intake Type Intake Condition Animal Type Sex upon Intake Age upon Intake Breed Color A786884 *Brock 01/03/2019 04:19:00 PM 01/03/2019 04:19:00 PM 2501 Magin Meadow Dr in Austin (TX) Stray Normal Dog Neutered Male 2 years Beagle Mix Tricolor A706918 Belle 07/05/2015 12:59:00 PM 07/05/2015 12:59:00 PM 9409 Bluegrass Dr in Austin (TX) Stray Normal Dog Spayed Female 8 years English Springer Spaniel White/Liver A724273 Runster 04/14/2016 06:43:00 PM 04/14/2016 06:43:00 PM 2818 Palomino Trail in Austin (TX) Stray Normal Dog Intact Male 11 months Basenji Mix Sable/White A665644 NaN 10/21/2013 07:59:00 AM 10/21/2013 07:59:00 AM Austin (TX) Stray Sick Cat Intact Female 4 weeks Domestic Shorthair Mix Calico A682524 Rio 06/29/2014 10:38:00 AM 06/29/2014 10:38:00 AM 800 Grove Blvd in Austin (TX) Stray Normal Dog Neutered Male 4 years Doberman Pinsch/Australian Cattle Dog Tan/Gray ``` 如果在某個時候我們需要恢復(fù)默認(rèn)的數(shù)字索引呢庆锦,這時就可以使用 reset_index()函數(shù)了 ```Python df.reset_index() ``` Output: ```Text Animal ID Name DateTime MonthYear Found Location Intake Type Intake Condition Animal Type Sex upon Intake Age upon Intake Breed Color 0 A786884 *Brock 01/03/2019 04:19:00 PM 01/03/2019 04:19:00 PM 2501 Magin Meadow Dr in Austin (TX) Stray Normal Dog Neutered Male 2 years Beagle Mix Tricolor 1 A706918 Belle 07/05/2015 12:59:00 PM 07/05/2015 12:59:00 PM 9409 Bluegrass Dr in Austin (TX) Stray Normal Dog Spayed Female 8 years English Springer Spaniel White/Liver 2 A724273 Runster 04/14/2016 06:43:00 PM 04/14/2016 06:43:00 PM 2818 Palomino Trail in Austin (TX) Stray Normal Dog Intact Male 11 months Basenji Mix Sable/White 3 A665644 NaN 10/21/2013 07:59:00 AM 10/21/2013 07:59:00 AM Austin (TX) Stray Sick Cat Intact Female 4 weeks Domestic Shorthair Mix Calico 4 A682524 Rio 06/29/2014 10:38:00 AM 06/29/2014 10:38:00 AM 800 Grove Blvd in Austin (TX) Stray Normal Dog Neutered Male 4 years Doberman Pinsch/Australian Cattle Dog Tan/Gray ``` 此方法的默認(rèn)行為包括用默認(rèn)的基于整數(shù)的索引替換現(xiàn)有的 DataFrame 索引捅位,并將舊索引轉(zhuǎn)換為與舊索引同名的新列(或名稱索引)。此外搂抒,默認(rèn)情況下艇搀,reset_index() 方法會從 MultiIndex 中刪除所有級別并且不會影響原始 DataFrame 數(shù)據(jù),而是創(chuàng)建一個新的 ## 何時使用 Reset_Index() 方法 reset_index() 方法將 DataFrame 索引重置為默認(rèn)數(shù)字索引燕耿,在以下情況下特別有用: - 執(zhí)行數(shù)據(jù)整理時——尤其是過濾數(shù)據(jù)或刪除缺失值等預(yù)處理操作中符,會導(dǎo)致較小的 DataFrame 具有不再連續(xù)的數(shù)字索引 - 當(dāng)索引應(yīng)該被視為一個常見的 DataFrame 列時 - 當(dāng)索引標(biāo)簽沒有提供有關(guān)數(shù)據(jù)的任何有價值的信息時 ## 如何調(diào)整 Reset_Index() 方法 前面的討論中姜胖,我們看到了當(dāng)我們不向它傳遞任何參數(shù)時誉帅,reset_index() 方法是如何工作的,當(dāng)然如果有需要右莱,我們可以通過調(diào)整方法的各種參數(shù)來更改此默認(rèn)行為蚜锨。 讓我們看看最有用的三種參數(shù):level、drop 和 inplace ### level 此參數(shù)采用整數(shù)慢蜓、字符串亚再、元組或列表作為可能的數(shù)據(jù)類型,并且僅適用于具有 MultiIndex 的 DataFrame晨抡,如下所示: ```Python df_multiindex = pd.read_csv('Austin_Animal_Center_Intakes.csv', index_col=['Animal ID', 'Name']).head() df_multiindex ``` Output: ```Text Animal ID Name DateTime MonthYear Found Location Intake Type Intake Condition Animal Type Sex upon Intake Age upon Intake Breed Color A786884 *Brock 01/03/2019 04:19:00 PM 01/03/2019 04:19:00 PM 2501 Magin Meadow Dr in Austin (TX) Stray Normal Dog Neutered Male 2 years Beagle Mix Tricolor A706918 Belle 07/05/2015 12:59:00 PM 07/05/2015 12:59:00 PM 9409 Bluegrass Dr in Austin (TX) Stray Normal Dog Spayed Female 8 years English Springer Spaniel White/Liver A724273 Runster 04/14/2016 06:43:00 PM 04/14/2016 06:43:00 PM 2818 Palomino Trail in Austin (TX) Stray Normal Dog Intact Male 11 months Basenji Mix Sable/White A665644 NaN 10/21/2013 07:59:00 AM 10/21/2013 07:59:00 AM Austin (TX) Stray Sick Cat Intact Female 4 weeks Domestic Shorthair Mix Calico A682524 Rio 06/29/2014 10:38:00 AM 06/29/2014 10:38:00 AM 800 Grove Blvd in Austin (TX) Stray Normal Dog Neutered Male 4 years Doberman Pinsch/Australian Cattle Dog Tan/Gray ``` 事實上氛悬,如果現(xiàn)在我們檢查上面 DataFrame 的索引则剃,我們會發(fā)現(xiàn)它不是一個常見的 DataFrame 索引,而是一個 MultiIndex 對象: ```Python df_multiindex.index ``` Output: ```Text MultiIndex([('A786884', '*Brock'), ('A706918', 'Belle'), ('A724273', 'Runster'), ('A665644', nan), ('A682524', 'Rio')], names=['Animal ID', 'Name']) ``` 默認(rèn)情況下如捅,reset_index() 方法參數(shù) level (level=None) 會移除 MultiIndex 的所有級別: ```Python df_multiindex.reset_index() ``` Output: ```Text Animal ID Name DateTime MonthYear Found Location Intake Type Intake Condition Animal Type Sex upon Intake Age upon Intake Breed Color 0 A786884 *Brock 01/03/2019 04:19:00 PM 01/03/2019 04:19:00 PM 2501 Magin Meadow Dr in Austin (TX) Stray Normal Dog Neutered Male 2 years Beagle Mix Tricolor 1 A706918 Belle 07/05/2015 12:59:00 PM 07/05/2015 12:59:00 PM 9409 Bluegrass Dr in Austin (TX) Stray Normal Dog Spayed Female 8 years English Springer Spaniel White/Liver 2 A724273 Runster 04/14/2016 06:43:00 PM 04/14/2016 06:43:00 PM 2818 Palomino Trail in Austin (TX) Stray Normal Dog Intact Male 11 months Basenji Mix Sable/White 3 A665644 NaN 10/21/2013 07:59:00 AM 10/21/2013 07:59:00 AM Austin (TX) Stray Sick Cat Intact Female 4 weeks Domestic Shorthair Mix Calico 4 A682524 Rio 06/29/2014 10:38:00 AM 06/29/2014 10:38:00 AM 800 Grove Blvd in Austin (TX) Stray Normal Dog Neutered Male 4 years Doberman Pinsch/Australian Cattle Dog Tan/Gray ``` 我們看到 DataFrame 的兩個索引都被轉(zhuǎn)換為通用 DataFrame 列棍现,而索引被重置為默認(rèn)的基于整數(shù)的索引 相反,如果我們顯式傳遞 level 的值镜遣,則此參數(shù)會從 DataFrame 索引中刪除選定的級別己肮,并將它們作為常見的 DataFrame 列返回(除非我們選擇使用 drop 參數(shù)從 DataFrame 中完全刪除此信息)。比較以下操作: ```Python df_multiindex.reset_index(level='Animal ID') ``` Output: ```Text Name Animal ID DateTime MonthYear Found Location Intake Type Intake Condition Animal Type Sex upon Intake Age upon Intake Breed Color *Brock A786884 01/03/2019 04:19:00 PM 01/03/2019 04:19:00 PM 2501 Magin Meadow Dr in Austin (TX) Stray Normal Dog Neutered Male 2 years Beagle Mix Tricolor Belle A706918 07/05/2015 12:59:00 PM 07/05/2015 12:59:00 PM 9409 Bluegrass Dr in Austin (TX) Stray Normal Dog Spayed Female 8 years English Springer Spaniel White/Liver Runster A724273 04/14/2016 06:43:00 PM 04/14/2016 06:43:00 PM 2818 Palomino Trail in Austin (TX) Stray Normal Dog Intact Male 11 months Basenji Mix Sable/White NaN A665644 10/21/2013 07:59:00 AM 10/21/2013 07:59:00 AM Austin (TX) Stray Sick Cat Intact Female 4 weeks Domestic Shorthair Mix Calico Rio A682524 06/29/2014 10:38:00 AM 06/29/2014 10:38:00 AM 800 Grove Blvd in Austin (TX) Stray Normal Dog Neutered Male 4 years Doberman Pinsch/Australian Cattle Dog Tan/Gray ``` 最開始 Animal ID 是 DataFrame 的索引之一悲关,當(dāng)我們設(shè)置 level 參數(shù)后谎僻,將其從索引中刪除并作為稱為 Animal ID 的公共列插入到 DataFrame 中 ```Python df_multiindex.reset_index(level='Name') ``` Output: ```Text Animal ID Name DateTime MonthYear Found Location Intake Type Intake Condition Animal Type Sex upon Intake Age upon Intake Breed Color A786884 *Brock 01/03/2019 04:19:00 PM 01/03/2019 04:19:00 PM 2501 Magin Meadow Dr in Austin (TX) Stray Normal Dog Neutered Male 2 years Beagle Mix Tricolor A706918 Belle 07/05/2015 12:59:00 PM 07/05/2015 12:59:00 PM 9409 Bluegrass Dr in Austin (TX) Stray Normal Dog Spayed Female 8 years English Springer Spaniel White/Liver A724273 Runster 04/14/2016 06:43:00 PM 04/14/2016 06:43:00 PM 2818 Palomino Trail in Austin (TX) Stray Normal Dog Intact Male 11 months Basenji Mix Sable/White A665644 NaN 10/21/2013 07:59:00 AM 10/21/2013 07:59:00 AM Austin (TX) Stray Sick Cat Intact Female 4 weeks Domestic Shorthair Mix Calico A682524 Rio 06/29/2014 10:38:00 AM 06/29/2014 10:38:00 AM 800 Grove Blvd in Austin (TX) Stray Normal Dog Neutered Male 4 years Doberman Pinsch/Australian Cattle Dog Tan/Gray ``` 在這里,Name 最初是 DataFrame 的索引之一寓辱,設(shè)置完level參數(shù)后艘绍,就變成了一個常用的列,叫做Name ### drop 此參數(shù)決定在索引重置后是否將舊索引保留為通用 DataFrame 列秫筏,或者將其從 DataFrame 中完全刪除鞍盗。默認(rèn)情況下 (drop=False) 是進(jìn)行保留的,正如我們在前面的所有示例中看到的那樣跳昼。否則般甲,如果我們不想將舊索引保留為列,我們可以在索引重置后將其從 DataFrame 中完全刪除(drop=True): ```Python df ``` Output: ```Text Animal ID Name DateTime MonthYear Found Location Intake Type Intake Condition Animal Type Sex upon Intake Age upon Intake Breed Color A786884 *Brock 01/03/2019 04:19:00 PM 01/03/2019 04:19:00 PM 2501 Magin Meadow Dr in Austin (TX) Stray Normal Dog Neutered Male 2 years Beagle Mix Tricolor A706918 Belle 07/05/2015 12:59:00 PM 07/05/2015 12:59:00 PM 9409 Bluegrass Dr in Austin (TX) Stray Normal Dog Spayed Female 8 years English Springer Spaniel White/Liver A724273 Runster 04/14/2016 06:43:00 PM 04/14/2016 06:43:00 PM 2818 Palomino Trail in Austin (TX) Stray Normal Dog Intact Male 11 months Basenji Mix Sable/White A665644 NaN 10/21/2013 07:59:00 AM 10/21/2013 07:59:00 AM Austin (TX) Stray Sick Cat Intact Female 4 weeks Domestic Shorthair Mix Calico A682524 Rio 06/29/2014 10:38:00 AM 06/29/2014 10:38:00 AM 800 Grove Blvd in Austin (TX) Stray Normal Dog Neutered Male 4 years Doberman Pinsch/Australian Cattle Dog Tan/Gray ``` 增加 drop 參數(shù) ```Python df.reset_index(drop=True) ``` Output: ```Text Name DateTime MonthYear Found Location Intake Type Intake Condition Animal Type Sex upon Intake Age upon Intake Breed Color 0 *Brock 01/03/2019 04:19:00 PM 01/03/2019 04:19:00 PM 2501 Magin Meadow Dr in Austin (TX) Stray Normal Dog Neutered Male 2 years Beagle Mix Tricolor 1 Belle 07/05/2015 12:59:00 PM 07/05/2015 12:59:00 PM 9409 Bluegrass Dr in Austin (TX) Stray Normal Dog Spayed Female 8 years English Springer Spaniel White/Liver 2 Runster 04/14/2016 06:43:00 PM 04/14/2016 06:43:00 PM 2818 Palomino Trail in Austin (TX) Stray Normal Dog Intact Male 11 months Basenji Mix Sable/White 3 NaN 10/21/2013 07:59:00 AM 10/21/2013 07:59:00 AM Austin (TX) Stray Sick Cat Intact Female 4 weeks Domestic Shorthair Mix Calico 4 Rio 06/29/2014 10:38:00 AM 06/29/2014 10:38:00 AM 800 Grove Blvd in Austin (TX) Stray Normal Dog Neutered Male 4 years Doberman Pinsch/Australian Cattle Dog Tan/Gray ``` 在上面的 DataFrame 中鹅颊,舊索引中包含的信息已完全從 DataFrame 中刪除了 drop 參數(shù)也適用于具有 MultiIndex 的 DataFrame敷存,就像我們之前創(chuàng)建的那樣: ```Python df_multiindex ``` Output: ```Text Animal ID Name DateTime MonthYear Found Location Intake Type Intake Condition Animal Type Sex upon Intake Age upon Intake Breed Color A786884 *Brock 01/03/2019 04:19:00 PM 01/03/2019 04:19:00 PM 2501 Magin Meadow Dr in Austin (TX) Stray Normal Dog Neutered Male 2 years Beagle Mix Tricolor A706918 Belle 07/05/2015 12:59:00 PM 07/05/2015 12:59:00 PM 9409 Bluegrass Dr in Austin (TX) Stray Normal Dog Spayed Female 8 years English Springer Spaniel White/Liver A724273 Runster 04/14/2016 06:43:00 PM 04/14/2016 06:43:00 PM 2818 Palomino Trail in Austin (TX) Stray Normal Dog Intact Male 11 months Basenji Mix Sable/White A665644 NaN 10/21/2013 07:59:00 AM 10/21/2013 07:59:00 AM Austin (TX) Stray Sick Cat Intact Female 4 weeks Domestic Shorthair Mix Calico A682524 Rio 06/29/2014 10:38:00 AM 06/29/2014 10:38:00 AM 800 Grove Blvd in Austin (TX) Stray Normal Dog Neutered Male 4 years Doberman Pinsch/Australian Cattle Dog Tan/Gray ``` 增加 drop 參數(shù) ```Python df_multiindex.reset_index(drop=True) ``` Output: ```Text DateTime MonthYear Found Location Intake Type Intake Condition Animal Type Sex upon Intake Age upon Intake Breed Color 0 01/03/2019 04:19:00 PM 01/03/2019 04:19:00 PM 2501 Magin Meadow Dr in Austin (TX) Stray Normal Dog Neutered Male 2 years Beagle Mix Tricolor 1 07/05/2015 12:59:00 PM 07/05/2015 12:59:00 PM 9409 Bluegrass Dr in Austin (TX) Stray Normal Dog Spayed Female 8 years English Springer Spaniel White/Liver 2 04/14/2016 06:43:00 PM 04/14/2016 06:43:00 PM 2818 Palomino Trail in Austin (TX) Stray Normal Dog Intact Male 11 months Basenji Mix Sable/White 3 10/21/2013 07:59:00 AM 10/21/2013 07:59:00 AM Austin (TX) Stray Sick Cat Intact Female 4 weeks Domestic Shorthair Mix Calico 4 06/29/2014 10:38:00 AM 06/29/2014 10:38:00 AM 800 Grove Blvd in Austin (TX) Stray Normal Dog Neutered Male 4 years Doberman Pinsch/Australian Cattle Dog Tan/Gray ``` 兩個舊索引都已從 Dataframe 中完全刪除,并且索引已重置為默認(rèn)值 當(dāng)然堪伍,我們可以結(jié)合 drop 和 level 參數(shù)锚烦,指定要從 DataFrame 中完全刪除哪些舊索引: ```Python df_multiindex.reset_index(level='Animal ID', drop=True) ``` Output: ```Text DateTime MonthYear Found Location Intake Type Intake Condition Animal Type Sex upon Intake Age upon Intake Breed Color Name *Brock 01/03/2019 04:19:00 PM 01/03/2019 04:19:00 PM 2501 Magin Meadow Dr in Austin (TX) Stray Normal Dog Neutered Male 2 years Beagle Mix Tricolor Belle 07/05/2015 12:59:00 PM 07/05/2015 12:59:00 PM 9409 Bluegrass Dr in Austin (TX) Stray Normal Dog Spayed Female 8 years English Springer Spaniel White/Liver Runster 04/14/2016 06:43:00 PM 04/14/2016 06:43:00 PM 2818 Palomino Trail in Austin (TX) Stray Normal Dog Intact Male 11 months Basenji Mix Sable/White NaN 10/21/2013 07:59:00 AM 10/21/2013 07:59:00 AM Austin (TX) Stray Sick Cat Intact Female 4 weeks Domestic Shorthair Mix Calico Rio 06/29/2014 10:38:00 AM 06/29/2014 10:38:00 AM 800 Grove Blvd in Austin (TX) Stray Normal Dog Neutered Male 4 years Doberman Pinsch/Australian Cattle Dog Tan/Gray ``` 舊索引 Animal ID 已從索引和 DataFrame 本身中刪除,另一個索引 Name 被保留為 DataFrame 的當(dāng)前索引 ### inplace 該參數(shù)決定是直接修改原來的 DataFrame 還是新建一個 DataFrame 對象帝雇。默認(rèn)情況下涮俄,它會使用新索引 (inplace=False) 創(chuàng)建一個新的 DataFrame,并保持原始 DataFrame 不變尸闸。讓我們使用默認(rèn)參數(shù)再次運(yùn)行 reset_index() 方法彻亲,然后將結(jié)果與原始 DataFrame 進(jìn)行比較: ```Python df.reset_index() ``` Output: ```Text Animal ID Name DateTime MonthYear Found Location Intake Type Intake Condition Animal Type Sex upon Intake Age upon Intake Breed Color 0 A786884 *Brock 01/03/2019 04:19:00 PM 01/03/2019 04:19:00 PM 2501 Magin Meadow Dr in Austin (TX) Stray Normal Dog Neutered Male 2 years Beagle Mix Tricolor 1 A706918 Belle 07/05/2015 12:59:00 PM 07/05/2015 12:59:00 PM 9409 Bluegrass Dr in Austin (TX) Stray Normal Dog Spayed Female 8 years English Springer Spaniel White/Liver 2 A724273 Runster 04/14/2016 06:43:00 PM 04/14/2016 06:43:00 PM 2818 Palomino Trail in Austin (TX) Stray Normal Dog Intact Male 11 months Basenji Mix Sable/White 3 A665644 NaN 10/21/2013 07:59:00 AM 10/21/2013 07:59:00 AM Austin (TX) Stray Sick Cat Intact Female 4 weeks Domestic Shorthair Mix Calico 4 A682524 Rio 06/29/2014 10:38:00 AM 06/29/2014 10:38:00 AM 800 Grove Blvd in Austin (TX) Stray Normal Dog Neutered Male 4 years Doberman Pinsch/Australian Cattle Dog Tan/Gray ``` ```Python df ``` Output: ```Text Animal ID Name DateTime MonthYear Found Location Intake Type Intake Condition Animal Type Sex upon Intake Age upon Intake Breed Color A786884 *Brock 01/03/2019 04:19:00 PM 01/03/2019 04:19:00 PM 2501 Magin Meadow Dr in Austin (TX) Stray Normal Dog Neutered Male 2 years Beagle Mix Tricolor A706918 Belle 07/05/2015 12:59:00 PM 07/05/2015 12:59:00 PM 9409 Bluegrass Dr in Austin (TX) Stray Normal Dog Spayed Female 8 years English Springer Spaniel White/Liver A724273 Runster 04/14/2016 06:43:00 PM 04/14/2016 06:43:00 PM 2818 Palomino Trail in Austin (TX) Stray Normal Dog Intact Male 11 months Basenji Mix Sable/White A665644 NaN 10/21/2013 07:59:00 AM 10/21/2013 07:59:00 AM Austin (TX) Stray Sick Cat Intact Female 4 weeks Domestic Shorthair Mix Calico A682524 Rio 06/29/2014 10:38:00 AM 06/29/2014 10:38:00 AM 800 Grove Blvd in Austin (TX) Stray Normal Dog Neutered Male 4 years Doberman Pinsch/Australian Cattle Dog Tan/Gray ``` 即使我們將索引重置為運(yùn)行第一段代碼的默認(rèn)數(shù)字,原始 DataFrame 仍然保持不變吮廉。 如果我們需要將原始 DataFrame 重新分配給對其應(yīng)用 reset_index() 方法的結(jié)果苞尝,我們可以直接重新分配它(df = df.reset_index())或?qū)?shù) inplace=True 傳遞給該方法: ```Python df.reset_index(inplace=True) df ``` Output: ```Text Animal ID Name DateTime MonthYear Found Location Intake Type Intake Condition Animal Type Sex upon Intake Age upon Intake Breed Color 0 A786884 *Brock 01/03/2019 04:19:00 PM 01/03/2019 04:19:00 PM 2501 Magin Meadow Dr in Austin (TX) Stray Normal Dog Neutered Male 2 years Beagle Mix Tricolor 1 A706918 Belle 07/05/2015 12:59:00 PM 07/05/2015 12:59:00 PM 9409 Bluegrass Dr in Austin (TX) Stray Normal Dog Spayed Female 8 years English Springer Spaniel White/Liver 2 A724273 Runster 04/14/2016 06:43:00 PM 04/14/2016 06:43:00 PM 2818 Palomino Trail in Austin (TX) Stray Normal Dog Intact Male 11 months Basenji Mix Sable/White 3 A665644 NaN 10/21/2013 07:59:00 AM 10/21/2013 07:59:00 AM Austin (TX) Stray Sick Cat Intact Female 4 weeks Domestic Shorthair Mix Calico 4 A682524 Rio 06/29/2014 10:38:00 AM 06/29/2014 10:38:00 AM 800 Grove Blvd in Austin (TX) Stray Normal Dog Neutered Male 4 years Doberman Pinsch/Australian Cattle Dog Tan/Gray ``` 我們看到現(xiàn)在更改已直接應(yīng)用于原始 DataFrame 之上了 ## 應(yīng)用實例:刪除缺失值后重置索引 讓我們將到目前為止討論的所有內(nèi)容付諸實踐,看看當(dāng)我們從 DataFrame 中刪除缺失值時宦芦,重置 DataFrame 索引是如何有用的 首先宙址,讓我們恢復(fù)我們最開始時創(chuàng)建的第一個 DataFrame,它具有默認(rèn)數(shù)字索引: ```Python df = pd.read_csv('Austin_Animal_Center_Intakes.csv').head() df ``` Output: ```Text Animal ID Name DateTime MonthYear Found Location Intake Type Intake Condition Animal Type Sex upon Intake Age upon Intake Breed Color 0 A786884 *Brock 01/03/2019 04:19:00 PM 01/03/2019 04:19:00 PM 2501 Magin Meadow Dr in Austin (TX) Stray Normal Dog Neutered Male 2 years Beagle Mix Tricolor 1 A706918 Belle 07/05/2015 12:59:00 PM 07/05/2015 12:59:00 PM 9409 Bluegrass Dr in Austin (TX) Stray Normal Dog Spayed Female 8 years English Springer Spaniel White/Liver 2 A724273 Runster 04/14/2016 06:43:00 PM 04/14/2016 06:43:00 PM 2818 Palomino Trail in Austin (TX) Stray Normal Dog Intact Male 11 months Basenji Mix Sable/White 3 A665644 NaN 10/21/2013 07:59:00 AM 10/21/2013 07:59:00 AM Austin (TX) Stray Sick Cat Intact Female 4 weeks Domestic Shorthair Mix Calico 4 A682524 Rio 06/29/2014 10:38:00 AM 06/29/2014 10:38:00 AM 800 Grove Blvd in Austin (TX) Stray Normal Dog Neutered Male 4 years Doberman Pinsch/Australian Cattle Dog Tan/Gray ``` 我們看到 DataFrame 中有一個缺失值调卑,讓我們使用 dropna() 方法刪除具有缺失值的整行 ```Python df.dropna(inplace=True) df ``` Output: ```Text Animal ID Name DateTime MonthYear Found Location Intake Type Intake Condition Animal Type Sex upon Intake Age upon Intake Breed Color 0 A786884 *Brock 01/03/2019 04:19:00 PM 01/03/2019 04:19:00 PM 2501 Magin Meadow Dr in Austin (TX) Stray Normal Dog Neutered Male 2 years Beagle Mix Tricolor 1 A706918 Belle 07/05/2015 12:59:00 PM 07/05/2015 12:59:00 PM 9409 Bluegrass Dr in Austin (TX) Stray Normal Dog Spayed Female 8 years English Springer Spaniel White/Liver 2 A724273 Runster 04/14/2016 06:43:00 PM 04/14/2016 06:43:00 PM 2818 Palomino Trail in Austin (TX) Stray Normal Dog Intact Male 11 months Basenji Mix Sable/White 4 A682524 Rio 06/29/2014 10:38:00 AM 06/29/2014 10:38:00 AM 800 Grove Blvd in Austin (TX) Stray Normal Dog Neutered Male 4 years Doberman Pinsch/Australian Cattle Dog Tan/Gray ``` 該行已從 DataFrame 中刪除抡砂,但是索引不再是連續(xù)的:0大咱、1、2注益、4徽级。讓我們重新設(shè)置它: ```Python df.reset_index() ``` Output: ```Text index Animal ID Name DateTime MonthYear Found Location Intake Type Intake Condition Animal Type Sex upon Intake Age upon Intake Breed Color 0 0 A786884 *Brock 01/03/2019 04:19:00 PM 01/03/2019 04:19:00 PM 2501 Magin Meadow Dr in Austin (TX) Stray Normal Dog Neutered Male 2 years Beagle Mix Tricolor 1 1 A706918 Belle 07/05/2015 12:59:00 PM 07/05/2015 12:59:00 PM 9409 Bluegrass Dr in Austin (TX) Stray Normal Dog Spayed Female 8 years English Springer Spaniel White/Liver 2 2 A724273 Runster 04/14/2016 06:43:00 PM 04/14/2016 06:43:00 PM 2818 Palomino Trail in Austin (TX) Stray Normal Dog Intact Male 11 months Basenji Mix Sable/White 3 4 A682524 Rio 06/29/2014 10:38:00 AM 06/29/2014 10:38:00 AM 800 Grove Blvd in Austin (TX) Stray Normal Dog Neutered Male 4 years Doberman Pinsch/Australian Cattle Dog Tan/Gray ``` 現(xiàn)在索引是連續(xù)的了,但是由于我們沒有顯式傳遞 drop 參數(shù)聊浅,舊索引被轉(zhuǎn)換為列餐抢,具有默認(rèn)名稱 index,下面讓我們從 DataFrame 中完全刪除舊索引: ```Python df.reset_index(drop=True) ``` Output: ```Text Animal ID Name DateTime MonthYear Found Location Intake Type Intake Condition Animal Type Sex upon Intake Age upon Intake Breed Color 0 A786884 *Brock 01/03/2019 04:19:00 PM 01/03/2019 04:19:00 PM 2501 Magin Meadow Dr in Austin (TX) Stray Normal Dog Neutered Male 2 years Beagle Mix Tricolor 1 A706918 Belle 07/05/2015 12:59:00 PM 07/05/2015 12:59:00 PM 9409 Bluegrass Dr in Austin (TX) Stray Normal Dog Spayed Female 8 years English Springer Spaniel White/Liver 2 A724273 Runster 04/14/2016 06:43:00 PM 04/14/2016 06:43:00 PM 2818 Palomino Trail in Austin (TX) Stray Normal Dog Intact Male 11 months Basenji Mix Sable/White 3 A682524 Rio 06/29/2014 10:38:00 AM 06/29/2014 10:38:00 AM 800 Grove Blvd in Austin (TX) Stray Normal Dog Neutered Male 4 years Doberman Pinsch/Australian Cattle Dog Tan/Gray ``` 現(xiàn)在我們徹底擺脫了無意義的舊索引低匙,當(dāng)前索引是連續(xù)的旷痕。最后一步是使用 inplace 參數(shù)將這些修改保存到我們的原始 DataFrame 中: ```Python df.reset_index(drop=True, inplace=True) df ``` Output: ```Text Animal ID Name DateTime MonthYear Found Location Intake Type Intake Condition Animal Type Sex upon Intake Age upon Intake Breed Color 0 A786884 *Brock 01/03/2019 04:19:00 PM 01/03/2019 04:19:00 PM 2501 Magin Meadow Dr in Austin (TX) Stray Normal Dog Neutered Male 2 years Beagle Mix Tricolor 1 A706918 Belle 07/05/2015 12:59:00 PM 07/05/2015 12:59:00 PM 9409 Bluegrass Dr in Austin (TX) Stray Normal Dog Spayed Female 8 years English Springer Spaniel White/Liver 2 A724273 Runster 04/14/2016 06:43:00 PM 04/14/2016 06:43:00 PM 2818 Palomino Trail in Austin (TX) Stray Normal Dog Intact Male 11 months Basenji Mix Sable/White 3 A682524 Rio 06/29/2014 10:38:00 AM 06/29/2014 10:38:00 AM 800 Grove Blvd in Austin (TX) Stray Normal Dog Neutered Male 4 years Doberman Pinsch/Australian Cattle Dog Tan/Gray ``` ## 總結(jié) 今天我們從多個方面討論了 reset_index() 方法 - reset_index() 方法的默認(rèn)行為 - 如何恢復(fù) DataFrame 的默認(rèn)數(shù)字索引 - 何時使用 reset_index() 方法 - 該方法最重要的幾個參數(shù) - 如何使用 MultiIndex - 如何從 DataFrame 中完全刪除舊索引 - 如何將修改直接保存到原始 DataFrame 中 最好我們又完整的完成了一個在刪除缺失值后重置 DataFrame 索引的實戰(zhàn)案例 好了,這就是今天分享的全部內(nèi)容顽冶,喜歡就點(diǎn)個贊吧 本文由[mdnice](https://mdnice.com/?platform=6)多平臺發(fā)布
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末欺抗,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子强重,更是在濱河造成了極大的恐慌绞呈,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,372評論 6 498
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件间景,死亡現(xiàn)場離奇詭異佃声,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)倘要,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,368評論 3 392
  • 文/潘曉璐 我一進(jìn)店門圾亏,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人封拧,你說我怎么就攤上這事志鹃。” “怎么了泽西?”我有些...
    開封第一講書人閱讀 162,415評論 0 353
  • 文/不壞的土叔 我叫張陵曹铃,是天一觀的道長。 經(jīng)常有香客問我捧杉,道長陕见,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,157評論 1 292
  • 正文 為了忘掉前任糠溜,我火速辦了婚禮淳玩,結(jié)果婚禮上直撤,老公的妹妹穿的比我還像新娘非竿。我一直安慰自己,他們只是感情好谋竖,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,171評論 6 388
  • 文/花漫 我一把揭開白布红柱。 她就那樣靜靜地躺著承匣,像睡著了一般。 火紅的嫁衣襯著肌膚如雪锤悄。 梳的紋絲不亂的頭發(fā)上韧骗,一...
    開封第一講書人閱讀 51,125評論 1 297
  • 那天,我揣著相機(jī)與錄音零聚,去河邊找鬼袍暴。 笑死,一個胖子當(dāng)著我的面吹牛隶症,可吹牛的內(nèi)容都是我干的政模。 我是一名探鬼主播,決...
    沈念sama閱讀 40,028評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼蚂会,長吁一口氣:“原來是場噩夢啊……” “哼淋样!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起胁住,我...
    開封第一講書人閱讀 38,887評論 0 274
  • 序言:老撾萬榮一對情侶失蹤趁猴,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后彪见,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體儡司,經(jīng)...
    沈念sama閱讀 45,310評論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,533評論 2 332
  • 正文 我和宋清朗相戀三年余指,在試婚紗的時候發(fā)現(xiàn)自己被綠了枫慷。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,690評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡浪规,死狀恐怖或听,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情笋婿,我是刑警寧澤誉裆,帶...
    沈念sama閱讀 35,411評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站缸濒,受9級特大地震影響足丢,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜庇配,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,004評論 3 325
  • 文/蒙蒙 一斩跌、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧捞慌,春花似錦耀鸦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽氮帐。三九已至,卻和暖如春洛姑,著一層夾襖步出監(jiān)牢的瞬間上沐,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,812評論 1 268
  • 我被黑心中介騙來泰國打工楞艾, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留参咙,地道東北人。 一個月前我還...
    沈念sama閱讀 47,693評論 2 368
  • 正文 我出身青樓硫眯,卻偏偏與公主長得像昂勒,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子舟铜,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,577評論 2 353

推薦閱讀更多精彩內(nèi)容