取得Pandas中列的資料類型 - Python
Pandas 是一個流行且功能強大的 Python 函式庫,通常用於資料分析和操作。它提供了許多資料結構,包括 Series、DataFrame 和 Panel,用於處理表格和時間序列資料。
Pandas DataFrame 是一種二維表格資料結構。在本文中,我們將介紹確定 Pandas 中列的資料類型的各種方法。在很多情況下,我們都必須在 Pandas DataFrame 中尋找列的資料類型。 Pandas 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
結論
知道如何取得每一列的資料類型,我們就可以有效率地完成各種資料操作和分析工作。根據所使用的方法或功能,每種方法都有其自身的優點和缺點。您可以根據您想要的表達式的複雜程度以及您個人編寫程式碼的偏好來選擇您想要的方法。
以上是取得Pandas中列的資料類型 - Python的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

Linux終端中查看Python版本時遇到權限問題的解決方法當你在Linux終端中嘗試查看Python的版本時,輸入python...

在使用Python的pandas庫時,如何在兩個結構不同的DataFrame之間進行整列複製是一個常見的問題。假設我們有兩個Dat...

如何在10小時內教計算機小白編程基礎?如果你只有10個小時來教計算機小白一些編程知識,你會選擇教些什麼�...

Uvicorn是如何持續監聽HTTP請求的? Uvicorn是一個基於ASGI的輕量級Web服務器,其核心功能之一便是監聽HTTP請求並進�...

在Python中,如何通過字符串動態創建對象並調用其方法?這是一個常見的編程需求,尤其在需要根據配置或運行...

使用FiddlerEverywhere進行中間人讀取時如何避免被檢測到當你使用FiddlerEverywhere...

本文討論了諸如Numpy,Pandas,Matplotlib,Scikit-Learn,Tensorflow,Tensorflow,Django,Blask和請求等流行的Python庫,並詳細介紹了它們在科學計算,數據分析,可視化,機器學習,網絡開發和H中的用途
