如何將 Pandas DataFrame 列轉換為 Series?
將Pandas DataFrame列轉換為Series是使用Python中的Pandas庫進行資料分析中的一項常見任務。Pandas中的Series物件是強大的資料結構,表示一維標記陣列,能夠儲存各種型別的資料,包括數值型、分型別和文字型資料。將DataFrame列轉換為Series具有多種優勢。它允許我們專注於特定列,並輕鬆地執行目標操作和分析。在處理大型資料集時,這顯得尤為重要,可以有效地提取和處理相關資訊。
在本文中,我們將探討在Pandas中將DataFrame列轉換為Series的不同方法。我們將介紹諸如按名稱訪問列、使用iloc和loc訪問器以及遍歷列等技術。透過理解這些方法,我們可以獲得將DataFrame列有效地轉換為Series所需的知識和工具,從而增強您在Pandas框架內操作和提取資料的能力。
方法一:按名稱訪問列
要在Pandas中將DataFrame列轉換為Series,可以使用方括號表示法(df['column_name'])或點表示法(df.column_name)按名稱訪問該列。方括號表示法返回包含列資料的Series物件,而點表示法提供了一種無需使用方括號即可訪問列的便捷方法。這兩種方法都可以輕鬆地將DataFrame列轉換為Series。
讓我們來看一個例子來更好地理解這種方法
import pandas as pd
df = pd.DataFrame({'A': [1, 2, 3, 4], 'B': [5, 6, 7, 8]})
# Extract column 'A' as a Series using bracket notation
series_A = df['A']
# Extract column 'B' as a Series using dot notation
series_B = df.B
# Print the Series objects
print(series_A)
print(series_B)
在上面的例子中,我們使用方括號表示法df['A']和點表示法df.B分別訪問列'A'和'B'。這兩個表示式都返回包含各自列資料的Series物件。
輸出
0 1 1 2 2 3 3 4 Name: A, dtype: int64 0 5 1 6 2 7 3 8 Name: B, dtype: int64
在輸出中,您將看到兩個Series物件:series_A和series_B。每個Series都表示DataFrame df中相應的列。值及其對應的索引一起顯示。dtype int64表示兩個Series中值的數 據型別為64位整數。series_A包含來自列'A'的資料,即[1, 2, 3, 4],series_B包含來自列'B'的資料,即[5, 6, 7, 8]。
方法二:使用iloc和loc訪問器
在Pandas中,iloc和loc訪問器分別用於基於整數或基於標籤的索引訪問DataFrame元素。這些訪問器提供了一種強大的方法來從資料框中提取特定列並將其轉換為Series。iloc訪問器代表“整數位置”,允許我們使用基於整數的索引訪問DataFrame元素。使用iloc,我們可以使用整數位置指定行和列位置。要使用iloc將列轉換為Series,我們將行索引指定為冒號:表示我們要選擇所有行,並將列索引指定為所需列的整數位置。
示例
這是一個例子
import pandas as pd
df = pd.DataFrame({'A': [1, 2, 3, 4], 'B': [5, 6, 7, 8]})
# Extracting column 'A' using integer-based indexing
series_A = df.iloc[:, 0]
# Extracting column 'B' using label-based indexing
series_B = df.loc[:, 'B']
# Print the contents of series_A and series_B
print(series_A)
print(series_B)
在上面的例子中,df.iloc[:, 0]訪問第一列(列索引0),而df.loc[:, 'B']訪問標記為'B'的列。這兩個表示式都返回包含各自列資料的Series物件。
輸出
0 1 1 2 2 3 3 4 Name: A, dtype: int64 0 5 1 6 2 7 3 8 Name: B, dtype: int64
提供的程式碼初始化了一個名為df的資料框,其中包含兩列'A'和'B',它們包含各自的資料值。使用df.iloc[:, 0]訪問列'A',使用df.loc[:, 'B']訪問列'B'。這允許將特定列從DataFrame中提取為Series物件。
方法三:遍歷列
在這種方法中,我們遍歷DataFrame的列並將每一列提取為單獨的Series。這允許我們將每個Series儲存在一個列表中,以便對各個列進行進一步處理或分析。
示例
讓我們來看一個例子來理解這種方法
import pandas as pd
# Create a DataFrame with columns 'A' and 'B'
df = pd.DataFrame({'A': [1, 2, 3, 4], 'B': [5, 6, 7, 8]})
series_list = []
# Iterate through each column in the DataFrame
for column in df.columns:
# Extract each column as a Series and append it to the series_list
series_list.append(df[column])
# Assign the first Series to series_A and the second Series to series_B
series_A = series_list[0]
series_B = series_list[1]
# Print the contents of series_A and series_B
print(series_A)
print(series_B)
在上面的例子中,我們匯入了pandas庫並建立了一個名為'df'的資料框,其中包含列'A'和'B'。我們初始化了一個空列表series_list來儲存Series物件。
輸出
0 1 1 2 2 3 3 4 Name: A, dtype: int64 0 5 1 6 2 7 3 8 Name: B, dtype: int64
輸出顯示series_A和series_B的內容,然後將其轉換為代表DataFrame的'A'和'B'列的Series物件。每個Series顯示其相應列的值及其索引。dtype指定Series中元素的資料型別,在本例中為int64。
結論
總之,將Pandas DataFrame列轉換為Series包括按名稱訪問列、使用iloc和loc訪問器以及遍歷列。這些方法允許高效轉換和操作Pandas強大的資料分析功能。將列轉換為Series會建立引用列資料的新物件,而不會修改原始DataFrame。這些技術允許對列執行特定操作,並促進使用Pandas進行資料操作和分析。
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP