获取Pandas中列的数据类型 - Python

PHPz
PHPz 转载
2023-08-30 20:01:02 608浏览

获取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,如有侵犯,请联系admin@php.cn删除