欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python中reset_index()函數(shù)的使用

 更新時間:2023年05月16日 11:00:15   作者:小小白2333  
本文主要介紹了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ù)drop的示例

參數(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ù)inplace的示例

參數(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ù)level的示例1

參數(shù)level的示例2

參數(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_level的示例

參數(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')

輸出:
參數(shù)col_fill的示例1

參數(shù)col_fill的示例2

到此這篇關(guān)于Python中reset_index()函數(shù)的使用的文章就介紹到這了,更多相關(guān)Python reset_index()內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論