在Pandas中從列表、NumPy陣列和字典建立Series
Pandas是一個開源的Python庫,它使用強大的資料結構提供高效能的資料操作和分析工具。Pandas這個名字來源於面板資料——計量經濟學中的多維資料。
Series是一個一維帶標籤的陣列,能夠儲存任何型別的資料,例如整數、字串、浮點數、Python物件等。軸標籤統稱為索引。
要建立Series,首先安裝pandas庫。我們使用pip在Python中安裝任何庫:
pip install pandas
從列表建立Pandas Series
示例
我們將使用series()從列表建立一個Series。列表將作為引數設定:
import pandas as pd # Create a List myList = [5, 10, 15, 20, 25, 30] # Create a Pandas series using the series() method res = pd.Series(myList) print("Series = \n",res)
輸出
Series = 0 5 1 10 2 15 3 20 4 25 5 30 dtype: int64
從NumPy陣列建立Pandas Series
要從NumPy陣列建立Series,請使用NumPy庫:
import numpy as np
示例
讓我們來看一個例子:
import pandas as pd import numpy as np # Create an array using numpy.array() arr = np.array([5, 10, 15, 20, 25, 30]) # Create a Pandas series using the Numpy array res = pd.Series(arr) print("Series = \n",res)
輸出
Series = 0 5 1 10 2 15 3 20 4 25 5 30 dtype: int64
從字典建立Pandas Series
示例
在這個例子中,我們將從具有鍵值對的字典建立Series:
import pandas as pd import numpy as np # Create a Dictionary with Keys and Values d = { "A":"demo", "B": 40, "C": 25, "D": "Yes" } # Create a Pandas series using Dictionary res = pd.Series(d) print("Series = \n",res)
輸出
Series = A demo B 40 C 25 D Yes dtype: object
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP