Python中reset_index()函數(shù)的使用
resert_index()函數(shù)
Series.reset_index(level=None, drop=False, name=NoDefault.no_default, inplace=False)
- drop: 重新設(shè)置索引后是否將原索引作為新的一列并入DataFrame,默認為False
- inplace: 是否在原DataFrame上改動,默認為False
- level: 如果索引(index)有多個列,僅從索引中刪除level指定的列,默認刪除所有列
- col_level: 如果列名(columns)有多個級別,決定被刪除的索引將插入哪個級別,默認插入第一級
- col_fill: 如果列名(columns)有多個級別,決定其他級別如何命名
- 作用: 用索引重置生成一個新的DataFrame或Series。當索引需要被視為列,或者索引沒有意義,需要在另一個操作之前重置為默認值時。在機器學習中常常會對索引進行一定的處理,用于是否保留原有的索引。
返回:DataFrame or None。具有新索引的數(shù)據(jù)幀,如果inplace=True,則無索引。
例子:
import pandas as pd
df = pd.DataFrame(data={'A':[1,2,3],'B':[4,5,6],'C':[7,8,9]})
print(df)
print('\n')
print(df.reset_index()) # 會將原來的索引index作為新的一列
print('\n')
print(df.reset_index(drop=True)) # 使用drop參數(shù)設(shè)置去掉原索引
print('\n')結(jié)果:
A B C
0 1 4 7
1 2 5 8
2 3 6 9
index A B C
0 0 1 4 7
1 1 2 5 8
2 2 3 6 9
A B C
0 1 4 7
1 2 5 8
2 3 6 9
讀懂代碼中resert_index():
def concat_detail(x):
return pd.Series({'備注':';'.join(x['detail'])})
df2=df1[['cwhxzqh','detail']].groupby('cwhxzqh').apply(concat_detail).reset_index()
df2將df1中原來的索引作為一個列,列名為 index
補:各參數(shù)的用法
示例
參數(shù)drop
False表示重新設(shè)置索引后將原索引作為新的一列并入DataFrame,True表示刪除原索引
import pandas as pd
import numpy as np
df = pd.DataFrame([('bird', 389.0), ('bird', 24.0), ('mammal', 80.5), ('mammal', np.nan)],
index=['falcon', 'parrot', 'lion', 'monkey'], columns=('class', 'max_speed'))
print(df)
print('\n')
df1 = df.reset_index()
print(df1)
print('\n')
df2 = df.reset_index(drop=True)
print(df2)輸出:

參數(shù)inplace
True表示在原DataFrame上修改,F(xiàn)alse將修改后的DataFrame作為新的對象返回
import pandas as pd
import numpy as np
df = pd.DataFrame([('bird', 389.0), ('bird', 24.0), ('mammal', 80.5), ('mammal', np.nan)],
index=['falcon', 'parrot', 'lion', 'monkey'], columns=('class', 'max_speed'))
print(df)
print('\n')
df1 = df.reset_index()
print(df1)
print('\n')
df2 = df.reset_index(inplace=True)
print(df2)
print('\n')
print(df)輸出:

參數(shù)level
如果索引有多個列,僅從索引中刪除由level指定的列,默認刪除所有列。輸入整數(shù)時表示將index的names中下標為level的索引刪除;輸入為字符串時表示將名字為level的索引刪除
import pandas as pd
import numpy as np
index = pd.MultiIndex.from_tuples([('bird', 'falcon'), ('bird', 'parrot'), ('mammal', 'lion'), ('mammal', 'monkey')], names=['class', 'name'])
columns = pd.MultiIndex.from_tuples([('speed', 'max'), ('species', 'type')])
df = pd.DataFrame([(389.0, 'fly'), ( 24.0, 'fly'), ( 80.5, 'run'), (np.nan, 'jump')], index=index, columns=columns)
print(df)
print('\n')
df0 = df.reset_index()
print(df0)
print('\n')
df1 = df.reset_index(level=1)
print(df1)
print('\n')
df2 = df.reset_index(level='name')
print(df2)輸出:


參數(shù)col_level
如果列名(columns)有多個級別,決定被刪除的索引將插入哪個級別,默認插入第一級(col_level=0)
import pandas as pd
import numpy as np
index = pd.MultiIndex.from_tuples([('bird', 'falcon'), ('bird', 'parrot'), ('mammal', 'lion'), ('mammal', 'monkey')], names=['class', 'name'])
columns = pd.MultiIndex.from_tuples([('speed', 'max'), ('species', 'type')])
df = pd.DataFrame([(389.0, 'fly'), ( 24.0, 'fly'), ( 80.5, 'run'), (np.nan, 'jump')], index=index, columns=columns)
print(df)
print('\n')
df1 = df.reset_index(level=0, col_level=0)
print(df1)
print('\n')
df2 = df.reset_index(level=0, col_level=1)
print(df2)
print('\n')輸出:

參數(shù)col_fill
重置索引時被刪除的索引只能插入一個級別,如果列名(columns)有多個級別,那么這個列的列名的其他級別如何命名就由col_fill決定,默認不做填充,如果傳入None則用被刪除的索引的名字填充
import pandas as pd
import numpy as np
index = pd.MultiIndex.from_tuples([('bird', 'falcon'), ('bird', 'parrot'), ('mammal', 'lion'), ('mammal', 'monkey')], names=['class', 'name'])
columns = pd.MultiIndex.from_tuples([('speed', 'max'), ('species', 'type')])
df = pd.DataFrame([(389.0, 'fly'), ( 24.0, 'fly'), ( 80.5, 'run'), (np.nan, 'jump')], index=index, columns=columns)
print(df)
print('\n')
df0 = df.reset_index(level=0, col_level=0)
print(df0)
print('\n')
df1 = df.reset_index(level=0, col_level=0, col_fill=None)
print(df1)
print('\n')
df2 = df.reset_index(level=0, col_level=1, col_fill='species')
print(df2)
print('\n')
df3 = df.reset_index(level=0, col_level=0, col_fill='genus')
print(df3)
print('\n')輸出:

到此這篇關(guān)于Python中reset_index()函數(shù)的使用的文章就介紹到這了,更多相關(guān)Python reset_index()內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python實現(xiàn)將通信達.day文件讀取為DataFrame
今天小編就為大家分享一篇Python實現(xiàn)將通信達.day文件讀取為DataFrame,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12
Python 結(jié)合opencv實現(xiàn)圖片截取和拼接代碼實踐
這篇文章主要介紹了Python 結(jié)合opencv實現(xiàn)圖片截取和拼接代碼實踐,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-09-09
淺談pytorch torch.backends.cudnn設(shè)置作用
今天小編就為大家分享一篇淺談pytorch torch.backends.cudnn設(shè)置作用,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02
Matlab中的mat數(shù)據(jù)轉(zhuǎn)成python中使用的npy數(shù)據(jù)遇到的坑及解決
這篇文章主要介紹了Matlab中的mat數(shù)據(jù)轉(zhuǎn)成python中使用的npy數(shù)據(jù)遇到的坑及解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12
pycharm中如何自定義設(shè)置通過“ctrl+滾輪”進行放大和縮小實現(xiàn)方法
這篇文章主要介紹了pycharm中如何自定義設(shè)置通過“ctrl+滾輪”進行放大和縮小實現(xiàn)方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09
Python執(zhí)行外部命令subprocess的使用詳解
subeprocess模塊是python自帶的模塊,無需安裝,主要用來取代一些就的模塊或方法,本文通過實例代碼給大家分享Python執(zhí)行外部命令subprocess及使用方法,感興趣的朋友跟隨小編一起看看吧2021-05-05

