Python Pandas - 重复索引的元素
要重复索引的元素,请使用Pandas中的方法。将重复次数设置为参数。首先,导入所需的库-index.repeat()
import pandas as pd
创建熊猫索引-
index = pd.Index(['Car','Bike','Airplane', 'Ship','Truck','Suburban'], name ='Transport')
显示熊猫指数-
print("Pandas Index...\n",index)重复索引的元素-
print("\nResult after repeating each index element twice...\n",index.repeat(2))示例
以下是代码-
import pandas as pd
#创建Pandas索引
index = pd.Index(['Car','Bike','Airplane', 'Ship','Truck','Suburban'], name ='Transport')
#显示Pandas索引
print("Pandas Index...\n",index)
#返回索引中的元素数
print("\nNumber of elements in the index...\n",index.size)
#返回数据的dtype
print("\nThe dtype object...\n",index.dtype)
#重复索引元素
print("\nResult after repeating each index element twice...\n",index.repeat(2))输出结果这将产生以下输出-
Pandas Index... Index(['Car', 'Bike', 'Airplane', 'Ship', 'Truck', 'Suburban'], dtype='object', name='Transport') Number of elements in the index... 6 The dtype object... object Result after repeating each index element twice... Index(['Car', 'Car', 'Bike', 'Bike', 'Airplane', 'Airplane', 'Ship', 'Ship', 'Truck', 'Truck', 'Suburban', 'Suburban'], dtype='object', name='Transport')