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

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

 更新時(shí)間:2023年05月16日 11:00:15   作者:小小白2333  
本文主要介紹了Python中reset_index()函數(shù)的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

resert_index()函數(shù)

Series.reset_index(level=None, drop=False, name=NoDefault.no_default, inplace=False)
  • drop: 重新設(shè)置索引后是否將原索引作為新的一列并入DataFrame,默認(rèn)為False
  • inplace: 是否在原DataFrame上改動(dòng),默認(rèn)為False
  • level: 如果索引(index)有多個(gè)列,僅從索引中刪除level指定的列,默認(rèn)刪除所有列
  • col_level: 如果列名(columns)有多個(gè)級(jí)別,決定被刪除的索引將插入哪個(gè)級(jí)別,默認(rèn)插入第一級(jí)
  • col_fill: 如果列名(columns)有多個(gè)級(jí)別,決定其他級(jí)別如何命名
  •  作用: 用索引重置生成一個(gè)新的DataFrame或Series。當(dāng)索引需要被視為列,或者索引沒(méi)有意義,需要在另一個(gè)操作之前重置為默認(rèn)值時(shí)。在機(jī)器學(xué)習(xí)中常常會(huì)對(duì)索引進(jìn)行一定的處理,用于是否保留原有的索引。

返回:DataFrame or None。具有新索引的數(shù)據(jù)幀,如果inplace=True,則無(wú)索引。

例子:

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()) # 會(huì)將原來(lái)的索引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中原來(lái)的索引作為一個(gè)列,列名為 index

補(bǔ):各參數(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作為新的對(duì)象返回

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

如果索引有多個(gè)列,僅從索引中刪除由level指定的列,默認(rèn)刪除所有列。輸入整數(shù)時(shí)表示將index的names中下標(biāo)為level的索引刪除;輸入為字符串時(shí)表示將名字為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)有多個(gè)級(jí)別,決定被刪除的索引將插入哪個(gè)級(jí)別,默認(rèn)插入第一級(jí)(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

重置索引時(shí)被刪除的索引只能插入一個(gè)級(jí)別,如果列名(columns)有多個(gè)級(jí)別,那么這個(gè)列的列名的其他級(jí)別如何命名就由col_fill決定,默認(rèn)不做填充,如果傳入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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論