Python pandas 如何检查数据框中所有列的 dtype?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40353079/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:26:10  来源:igfitidea点击:

pandas how to check dtype for all columns in a dataframe?

pythonpandasdataframe

提问by user3768495

It seems that dtype only work for pandas.DataFrame.Series, right? Is there a function to display data types of all columns at once?

似乎 dtype 只适用于 pandas.DataFrame.Series,对吗?是否有一次显示所有列的数据类型的功能?

回答by Psidom

The singularform dtypeis used to check the data type for a single column. And the pluralform dtypesis for data frame which returns data types for all columns. Essentially:

单数形式dtype是用来检查数据类型为一列。和复数形式dtypes是,其为所有列返回数据类型的数据帧。本质上:

For a single column:

对于单列

dataframe.column.dtype

For all columns:

对于所有列

dataframe.dtypes

Example:

示例

import pandas as pd
df = pd.DataFrame({'A': [1,2,3], 'B': [True, False, False], 'C': ['a', 'b', 'c']})

df.A.dtype
# dtype('int64')
df.B.dtype
# dtype('bool')
df.C.dtype
# dtype('O')

df.dtypes
#A     int64
#B      bool
#C    object
#dtype: object

回答by Mumtaj Ali

Suppose df is a pandas DataFrame then to get number of non-null values and data types of all column at once use:

假设 df 是一个 Pandas DataFrame,然后一次获取所有列的非空值和数据类型的数量,请使用:

df.info()