Python – 獲取列的資料型別


要獲取列的資料型別,請使用 info() 方法。我們首先匯入所需的庫 −

import pandas as pd

建立一個具有具有不同資料型別的兩個列的 DataFrame −

dataFrame = pd.DataFrame(
   {
      "Student": ['Jack', 'Robin', 'Ted', 'Marc', 'Scarlett', 'Kat', 'John'],"Roll Number": [ 5, 10, 3, 8, 2, 9, 6]
   }
)

獲取有關資料型別的資訊 −

dataFrame.info()

示例

以下是完整的程式碼 −

import pandas as pd

# Create DataFrame
dataFrame = pd.DataFrame(
   {
      "Student": ['Jack', 'Robin', 'Ted', 'Marc', 'Scarlett', 'Kat', 'John'],"Roll Number": [ 5, 10, 3, 8, 2, 9, 6]
   }
)

print"DataFrame ...\n",dataFrame

print"\nInfo and the datatypes of the columns in the dataframe:\n"

# get the datatypes info
print(dataFrame.info())

輸出

這將產生以下輸出 −

DataFrame ...
   Roll Number   Student
0            5      Jack
1           10     Robin
2            3       Ted
3            8      Marc
4            2  Scarlett
5            9       Kat
6            6      John

Info and the datatypes of the columns in the dataframe:

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 7 entries, 0 to 6
Data columns (total 2 columns):
Roll Number    7  non-null  int64
Student        7  non-null  object
dtypes: int64(1), object(1)
memory usage: 184.0+ bytes
None

更新於:21-Sep-2021

107 次瀏覽

開啟你的職業生涯生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.