獲取 Pandas 中列的資料型別 - Python
Pandas 是一個流行且功能強大的 Python 庫,通常用於資料分析和處理。它提供許多資料結構,包括 Series、DataFrame 和 Panel,用於處理表格和時間序列資料。
Pandas DataFrame 是一個二維表格資料結構。在本文中,我們將介紹幾種在 Pandas 中確定列資料型別的方法。在 Pandas DataFrame 中,我們可能有很多情況需要查詢列的資料型別。Pandas DataFrame 中的每一列都可以包含不同的資料型別。
在繼續之前,讓我們建立一個示例 DataFrame,以便我們從中獲取 Pandas 中列的資料型別。
import pandas as pd
# create a sample dataframe
df = pd.DataFrame({'Vehicle name': ['Supra', 'Honda', 'Lamorghini'],'price': [5000000, 600000, 7000000]})
print(df)
輸出
這段 Python 指令碼列印我們建立的 DataFrame。
Vehicle name price 0 Supra 5000000 1 Honda 600000 2 Lamorghini 7000000
完成任務可以遵循的方法如下所示
方法
使用 dtypes 屬性
使用 select_dtypes()
使用 info() 方法
使用 describe() 函式
現在讓我們討論每種方法以及如何使用它們來獲取 Pandas 中列的資料型別。
方法 1:使用 dtypes 屬性
我們可以使用 dtypes 屬性來獲取 DataFrame 中每一列的資料型別。此屬性將返回一個包含每一列資料型別的序列。可以使用以下語法
語法
df.dtypes
返回值 DataFrame 中每一列的資料型別。
演算法
匯入 Pandas 庫。
使用 pd.DataFrame() 函式建立一個 DataFrame,並將示例作為字典傳遞。
使用 dtypes 屬性獲取 DataFrame 中每一列的資料型別。
列印結果以檢查每一列的資料型別。
示例 1
# import the Pandas library
import pandas as pd
# create a sample dataframe
df = pd.DataFrame({'Vehicle name': ['Supra', 'Honda', 'Lamorghini'],'price': [5000000, 600000, 7000000]})
# print the dataframe
print("DataFrame:\n", df)
# get the data types of each column
print("\nData types of each column:")
print(df.dtypes)
輸出
DataFrame: Vehicle name price 0 Supra 5000000 1 Honda 600000 2 Lamorghini 7000000 Data types of each column: Vehicle name object price int64 dtype: object
示例 2
在這個例子中,我們獲取的是 DataFrame 中單個列的資料型別。
# import the Pandas library
import pandas as pd
# create a sample dataframe
df = pd.DataFrame({'Vehicle name': ['Supra', 'Honda', 'Lamorghini'],'price': [5000000, 600000, 7000000]})
# print the dataframe
print("DataFrame:\n", df)
# get the data types of column named price
print("\nData types of column named price:")
print(df.dtypes['price'])
輸出
DataFrame: Vehicle name price 0 Supra 5000000 1 Honda 600000 2 Lamorghini 7000000 Data types of column named price: int64
方法 2:使用 select_dtypes()
我們可以使用 select_dtypes() 方法來過濾出我們需要的列的資料型別。根據作為輸入提供的資料型別,select_dtypes() 方法返回列的子集。此方法允許我們選擇屬於特定資料型別的列,然後確定資料型別。
演算法
匯入 Pandas 庫。
使用 pd.DataFrame() 函式建立一個 DataFrame,並將給定的資料作為字典傳遞。
列印 DataFrame 以檢查建立的資料。
使用 select_dtypes() 方法從 DataFrame 中選擇所有數值列。使用 include 引數傳遞我們要選擇的引數資料型別的列表。
迴圈遍歷列以迭代每個數值列並列印其資料型別。
示例
# import the Pandas library
import pandas as pd
# create a sample dataframe
df = pd.DataFrame({'Vehicle name': ['Supra', 'Honda', 'Lamorghini'],'price': [5000000, 600000, 7000000]})
# print the dataframe
print("DataFrame:\n", df)
# select the numeric columns
numeric_cols = df.select_dtypes(include=['float64', 'int64']).columns
# get the data type of each numeric column
for col in numeric_cols:
print("Data Type of column", col, "is", df[col].dtype)
輸出
DataFrame: Vehicle name price 0 Supra 5000000 1 Honda 600000 2 Lamorghini 7000000 Data Type of column price is int64
方法 3:使用 info() 方法
我們也可以使用 info() 方法來完成我們的任務。info() 方法為我們提供了 DataFrame 的簡潔摘要,包括每一列的資料型別。可以使用以下語法
語法
DataFrame.info(verbose=None, buf=None, max_cols=None, memory_usage=None, null_counts=None)
返回值 無
演算法
匯入 Pandas 庫。
使用 pd.DataFrame() 函式建立一個 DataFrame,並將上述資料作為字典傳遞。
列印 DataFrame 以檢查建立的資料。
使用 info() 方法獲取有關 DataFrame 的資訊。
列印從 info() 方法獲得的資訊。
示例
# import the Pandas library
import pandas as pd
# create a sample dataframe
df = pd.DataFrame({'Vehicle name': ['Supra', 'Honda', 'Lamorghini'],'price': [5000000, 600000, 7000000]})
# print the dataframe
print("DataFrame:\n", df)
# use the info() method to get the data type of each column
print(df.info())
輸出
DataFrame: Vehicle name price 0 Supra 5000000 1 Honda 600000 2 Lamorghini 7000000 <class 'pandas.core.frame.DataFrame'> RangeIndex: 3 entries, 0 to 2 Data columns (total 2 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 Vehicle name 3 non-null object 1 price 3 non-null int64 dtypes: int64(1), object(1) memory usage: 176.0+ bytes None
方法 4:使用 describe() 函式
describe() 方法用於生成 DataFrame 的描述性統計資料,包括每一列的資料型別。
演算法
使用 import 語句匯入 Pandas 庫。
使用 pd.DataFrame() 函式建立一個 DataFrame,並將給定的資料作為字典傳遞。
列印 DataFrame 以檢查建立的資料。
使用 describe() 方法獲取 DataFrame 的描述性統計資料。
使用 describe() 方法的 include 引數設定為 'all' 以包含描述性統計資料中的所有列。
使用 dtypes 屬性獲取 DataFrame 中每一列的資料型別。
列印每一列的資料型別。
示例
# import the Pandas library
import pandas as pd
# create a sample dataframe
df = pd.DataFrame({'Vehicle name': ['Supra', 'Honda', 'Lamorghini'],'price': [5000000, 600000, 7000000]})
# print the dataframe
print("DataFrame:\n", df)
# use the describe() method to get the descriptive statistics of the dataframe
desc_stats = df.describe(include='all')
# get the data type of each column
dtypes = desc_stats.dtypes
# print the data type of each column
print("Data type of each column in the descriptive statistics:\n", dtypes)
輸出
DataFrame: Vehicle name price 0 Supra 5000000 1 Honda 600000 2 Lamorghini 7000000 Data type of each column in the descriptive statistics: Vehicle name object price float64 dtype: object
結論
通過了解如何獲取每一列的資料型別,我們可以有效地完成各種資料操作和分析任務。每種方法都有其自身的優缺點,具體取決於所使用方法或函式。您可以根據所需表示式的複雜性和個人程式碼編寫偏好選擇所需的方法。
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP