Python Pandas Index.repeat()

Python Pandas Index.repeat()

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

Pandas Index.repeat()函数重复一个索引的元素。该函数返回一个新的索引,其中当前索引中的每个元素都被连续地重复给定的次数。

语法: Index.repeat(repeats, *args, **kwargs)

参数 :
repeats :每个元素的重复次数。
**kwargs :附加的关键字没有任何作用,但为了与numpy兼容,可能会被接受。

返回:新创建的具有重复元素的索引。

例子#1:使用Index.repeat()()函数来重复索引中的元素2次。

# importing pandas as pd
import pandas as pd
  
# Creating the index
idx = pd.Index(['Beagle', 'Pug', 'Labrador', 'Pug',
                        'Mastiff', None, 'Beagle'])
  
# Print the Index
idx

输出 :
Python Pandas Index.repeat()

让我们重复索引元素2次。

# to repeat the values
idx.repeat(2)

输出 :
Python Pandas Index.repeat()
正如我们在输出中看到的,该函数返回了一个新的索引,所有的值都重复了2次。需要注意的一件事是,该函数也重复了NaN值。

例子#2:使用Index.repeat()函数来重复index的值3次。

# importing pandas as pd
import pandas as pd
  
# Creating the index
idx = pd.Index([22, 14, 8, 56, None, 21, None, 23])
  
# Print the Index
idx

输出 :
Python Pandas Index.repeat()

让我们重复索引元素3次。

# to repeat the values
idx.repeat(3)

输出 :
Python Pandas Index.repeat()

正如我们在输出中看到的,该函数返回了一个新的索引,所有的值都重复了3次。需要注意的一件事是,该函数也重复了NaN值。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程