Python Pandas - 将一系列索引选项附加在一起
要将一系列索引选项追加在一起,请使用 Pandas 中的 append() 方法。首先,导入所需的库 −
import pandas as pd
创建 Pandas 索引 −
index1 = pd.Index([10, 20, 30, 40, 50])
显示 Pandas 索引 −
print("Pandas Index...\n",index1)
创建一个新索引进行追加 −
index2 = pd.Index([60, 70, 80])
追加新索引 −
print("\nAfter appending...\n",index1.append(index2))
示例
以下为代码 −
import pandas as pd # Creating Pandas index index1 = pd.Index([10, 20, 30, 40, 50]) # Display the Pandas index print("Pandas Index...\n",index1) # Return the number of elements in the Index print("\nNumber of elements in the index...\n",index1.size) # create a new index to be appended index2 = pd.Index([60, 70, 80]) # Append the new index print("\nAfter appending...\n",index1.append(index2))
输出
这将产生以下输出 −
Pandas Index... Int64Index([10, 20, 30, 40, 50], dtype='int64') Number of elements in the index... 5 After appending... Int64Index([10, 20, 30, 40, 50, 60, 70, 80], dtype='int64')
广告