首頁  >  文章  >  後端開發  >  取得Pandas中列的資料類型 - Python

取得Pandas中列的資料類型 - Python

PHPz
PHPz轉載
2023-08-30 20:01:021262瀏覽

获取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中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除