获取 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