將 NumPy 陣列轉換為 Pandas Series
NumPy 陣列是 N 維陣列,也稱為 ndarray,它是 NumPy 庫的主要物件。同樣,Pandas Series 是 Pandas 庫中的一維資料結構。Pandas 和 NumPy 都是 Python 中廣泛使用的開源庫。下面我們可以看到一維 NumPy 陣列。
NumPy array array([1, 2, 3, 4])
Pandas Series 是一種一維資料結構,具有標記索引,它與一維 NumPy 陣列非常相似。
Pandas Series: 0 1 1 2 2 3 3 4 4 5 dtype: int64
從上面的程式碼塊中,我們可以看到 Pandas Series 物件,它有 5 個整數元素,每個元素都用位置索引值標記。在下面的文章中,我們將學習如何將 NumPy 陣列轉換為 Pandas Series 物件。
輸入輸出場景
讓我們看看輸入輸出場景,以瞭解如何將 NumPy 陣列轉換為 Pandas Series。
假設我們有一個包含一些值的一維 NumPy 陣列,在輸出中,我們將看到從 NumPy 陣列轉換而來的 Pandas Series 物件。
Input numpy array: [1 2 3 4] Output Series: 0 1 1 2 2 3 3 4 dtype: int64
要將 NumPy 陣列轉換為 Pandas Series,我們可以使用 pandas.Series() 方法。
pandas.Series() 方法
pandas.Series() 方法用於根據給定資料建立 Series 物件。該方法返回一個 Series 物件。以下是該方法的語法:
pandas.Series(data=None, index=None, dtype=None, name=None, copy=False, fastpath=False)
其中,
data:可迭代物件、字典或標量值。
index:使用此引數指定行標籤。預設值為 0 到 n-1。
dtype:這是一個字串值,用於指定 Series 的資料型別。(可選)
name:這是一個字串值,用於指定 Series 物件的名稱。(可選)
copy:從輸入複製資料,預設為 False。
示例
讓我們使用 pandas.Series() 方法將 NumPy 陣列轉換為 Pandas Series。
# importing the modules
import numpy as np
import pandas as pd
# Creating a 1 dimensional numpy array
numpy_array = np.array([1, 2, 8, 3, 0, 2, 9, 4])
print("Input numpy array:")
print(numpy_array)
# Convert NumPy array to Series
s = pd.Series(numpy_array)
print("Output Series:")
print(s)
輸出
Input numpy array: [1 2 8 3 0 2 9 4] Output Series: 0 1 1 2 2 8 3 3 4 0 5 2 6 9 7 4 dtype: int64
最初,透過使用整數元素建立了一個一維 NumPy 陣列,然後將該陣列轉換為 Pandas Series 物件。
示例
在這個例子中,Series 是從浮點數的 NumPy 陣列轉換而來的。在轉換過程中,我們將使用 index 引數為 Series 物件指定行標籤。
# importing the modules
import numpy as np
import pandas as pd
# Creating a 1 dimensional numpy array
numpy_array = np.array([1, 2.8, 3.0, 2, 9, 4.2])
print("Input numpy array:")
print(numpy_array)
# Convert NumPy array to Series
s = pd.Series(numpy_array, index=list('abcdef'))
print("Output Series:")
print(s)
輸出
Input numpy array: [1. 2.8 3. 2. 9. 4.2] Output Series: a 1.0 b 2.8 c 3.0 d 2.0 e 9.0 f 4.2 dtype: float64
將字串列表提供給 Series 建構函式的 index 引數。
示例
在這個例子中,我們將把一個二維 NumPy 陣列轉換為 Series 物件。
# importing the modules
import numpy as np
import pandas as pd
# Creating a numpy array
numpy_array = np.array([[4, 1], [7, 2], [2, 0]])
print("Input numpy array:")
print(numpy_array)
# Convert NumPy array to Series
s = pd.Series(map(lambda x: x, numpy_array))
print("Output Series:")
print(s)
輸出
Input numpy array: [[4 1] [7 2] [2 0]] Output Series: 0 [4, 1] 1 [7, 2] 2 [2, 0] dtype: object
透過將 map 和 lambda 函式結合使用,我們在這裡將二維 NumPy 陣列轉換為 Series 物件。轉換後的 Series 的資料型別為物件型別,每個 Series 元素都包含一個整數陣列。
示例
讓我們再舉一個例子,將二維陣列轉換為 Series 物件。
# importing the modules
import numpy as np
import pandas as pd
# Creating a numpy array
numpy_array = np.array([[4, 1], [7, 2], [2, 0]])
print("Input numpy array:")
print(numpy_array)
# Convert NumPy array to Series
s = pd.Series(map(lambda x: x[0], numpy_array))
print("Output Series:")
print(s)
輸出
Input numpy array: [[4 1] [7 2] [2 0]] Output Series: 0 4 1 7 2 2 dtype: int64
這裡 Series 是使用二維 NumPy 陣列的第一行元素建立的。
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP