Python Pandas Index.insert()

Python Pandas Index.insert()

Python是一种进行数据分析的伟大语言,主要是因为以数据为中心的Python包的奇妙生态系统。Pandas就是这些包中的一个,它使导入和分析数据变得更加容易。

Pandas Index.insert()函数制作新的索引,在位置上插入新的项目。这个函数也遵循Python list.append()对负值的语义。如果负值被传递,那么它将从另一端开始。

语法: Index.insert(loc, item)

参数 :
loc : int
item:对象

返回 : new_index : 索引

例子#1:使用Index.insert()函数在索引中插入一个新的值。

# importing pandas as pd
import pandas as pd
  
# Creating the Index
idx = pd.Index(['Labrador', 'Beagle', 'Labrador',
                     'Lhasa', 'Husky', 'Beagle'])
  
# Print the Index
idx

输出 :
Python Pandas Index.insert()
现在在第一个索引处插入’Great_Dane’。

# Inserting a value at the first position in the index.
idx.insert(1, 'Great_Dane')

输出 :
Python Pandas Index.insert()

正如我们在输出中看到的,Index.insert()函数已经在所需的位置插入了传递的值。

例子#2:使用Index.insert()函数将一个值插入Index中从最后一个开始的第二个位置。

# importing pandas as pd
import pandas as pd
  
# Creating the Index
idx = pd.Index(['Labrador', 'Beagle', 'Labrador',
                     'Lhasa', 'Husky', 'Beagle'])
  
# Print the Index
idx

输出 :
Python Pandas Index.insert()

现在在最后一个索引中插入’Great_Dane’。

# Inserting a value at the first position in the index.
idx.insert(-1, 'Great_Dane')

输出 :
Python Pandas Index.insert()
正如我们在输出中所看到的,通过的值已经被插入到索引中的理想位置。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程