pandas 是一个流行且功能强大的 python 库,通常用于数据分析和操作。它提供了许多数据结构,包括 series、dataframe 和 panel,用于处理表格和时间序列数据。
pandas dataframe 是一种二维表格数据结构。在本文中,我们将介绍确定 pandas 中列的数据类型的各种方法。在很多情况下,我们都必须在 pandas dataframe 中查找列的数据类型。 pandas dataframe 中的每一列都可以包含不同的数据类型。
在继续之前,让我们制作一个示例数据框,我们必须在该数据框上获取 pandas 中列的数据类型
import pandas as pd# create a sample dataframedf = pd.dataframe({'vehicle name': ['supra', 'honda', 'lamorghini'],'price': [5000000, 600000, 7000000]})print(df)
输出这个 python 脚本打印我们创建的 dataframe。
vehicle name price0 supra 50000001 honda 6000002 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 libraryimport pandas as pd# create a sample dataframedf = pd.dataframe({'vehicle name': ['supra', 'honda', 'lamorghini'],'price': [5000000, 600000, 7000000]})# print the dataframeprint(dataframe:\n, df)# get the data types of each columnprint(\ndata types of each column:)print(df.dtypes)
输出dataframe: vehicle name price0 supra 50000001 honda 6000002 lamorghini 7000000data types of each column:vehicle name objectprice int64dtype: object
示例 2在此示例中,我们获取 dataframe 的单列的数据类型
# import the pandas libraryimport pandas as pd# create a sample dataframedf = pd.dataframe({'vehicle name': ['supra', 'honda', 'lamorghini'],'price': [5000000, 600000, 7000000]})# print the dataframeprint(dataframe:\n, df)# get the data types of column named priceprint(\ndata types of column named price:)print(df.dtypes['price'])
输出dataframe: vehicle name price0 supra 50000001 honda 6000002 lamorghini 7000000data 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 libraryimport pandas as pd# create a sample dataframedf = pd.dataframe({'vehicle name': ['supra', 'honda', 'lamorghini'],'price': [5000000, 600000, 7000000]})# print the dataframeprint(dataframe:\n, df)# select the numeric columnsnumeric_cols = df.select_dtypes(include=['float64', 'int64']).columns# get the data type of each numeric columnfor col in numeric_cols: print(data type of column, col, is, df[col].dtype)
输出dataframe: vehicle name price0 supra 50000001 honda 6000002 lamorghini 7000000data 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 libraryimport pandas as pd# create a sample dataframedf = pd.dataframe({'vehicle name': ['supra', 'honda', 'lamorghini'],'price': [5000000, 600000, 7000000]})# print the dataframeprint(dataframe:\n, df)# use the info() method to get the data type of each columnprint(df.info())
输出dataframe: vehicle name price0 supra 50000001 honda 6000002 lamorghini 7000000<class 'pandas.core.frame.dataframe'>rangeindex: 3 entries, 0 to 2data 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+ bytesnone
方法4:使用describe()函数describe()方法用于生成dataframe的描述性统计信息,包括每列的数据类型。
算法使用 import 语句导入 pandas 库。
使用 pd.dataframe() 函数创建 dataframe 并将给定数据作为字典传递。
打印dataframe以检查创建的数据。
使用describe()方法获取dataframe的描述性统计信息。
使用describe()方法的include参数为'all'以包含描述性统计中的所有列。
使用 dtypes 属性获取 dataframe 中每列的数据类型。
打印每列的数据类型。
示例# import the pandas libraryimport pandas as pd# create a sample dataframedf = pd.dataframe({'vehicle name': ['supra', 'honda', 'lamorghini'],'price': [5000000, 600000, 7000000]})# print the dataframeprint(dataframe:\n, df)# use the describe() method to get the descriptive statistics of the dataframedesc_stats = df.describe(include='all')# get the data type of each column dtypes = desc_stats.dtypes# print the data type of each columnprint(data type of each column in the descriptive statistics:\n, dtypes)
输出dataframe: vehicle name price0 supra 50000001 honda 6000002 lamorghini 7000000data type of each column in the descriptive statistics: vehicle name objectprice float64dtype: object
结论知道如何获取每一列的数据类型,我们就可以高效地完成各种数据操作和分析工作。根据所使用的方法或功能,每种方法都有其自身的优点和缺点。您可以根据您想要的表达式的复杂程度以及您个人编写代码的偏好来选择您想要的方法。
以上就是获取pandas中列的数据类型 - python的详细内容。