Python 获取Pandas中列的数据类型
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
在此示例中,我们获取数据框中单个列的数据类型。
# 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
结论
通过了解每列的数据类型,我们可以高效地完成各种数据操作和分析任务。根据所使用的方法或函数,每种方法都有其自身的优缺点。您可以根据您希望拥有的表达式的复杂性以及您个人对编写代码的偏好来选择所需的方法。