如何在Python中使用Numpy建立序列並顯式傳遞索引值?
讓我們看看如何使用Numpy陣列建立序列資料結構,並顯式地為“索引”賦予值。
如果未為索引指定值,則預設值從0開始分配給序列中的值。
以下是一個例子:
示例
import pandas as pd
import numpy as np
my_data = np.array(['ab','bc','cd','de', 'ef', 'fg','gh', 'hi'])
my_index = [3, 5, 7, 9, 11, 23, 45, 67]
my_series = pd.Series(my_data, index = my_index)
print("This is series data structure created using Numpy array and specifying index values")
print(my_series)輸出
This is series data structure created using Numpy array and specifying index values 3 ab 5 bc 7 cd 9 de 11 ef 23 fg 45 gh 67 hi dtype: object
解釋
匯入所需的庫,併為方便使用賦予別名。
下一步是建立一個numpy陣列結構。
接下來,建立一個需要顯式指定為索引的值列表。
將先前建立的資料和索引列表作為引數傳遞給'pandas'庫中的'Series'函式。
輸出顯示在控制檯上。
注意 - 這表明在建立序列資料結構時可以指定自定義索引值。
廣告
資料結構
網路
關係資料庫管理系統(RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP