列出数据框列中存在的所有数据类型

List all data types present in dataframe column

如何列出数据框 df 的列 Name 中存在的所有数据类型?

Name
1.0
XCY

有些可能是字符串,有些可能是浮点数等

尝试使用 mapaggapply:

>>> df['Name'].map(type).value_counts()
<class 'float'>    1
<class 'str'>      1
Name: Name, dtype: int64

>>> df['Name'].agg(type).value_counts()
<class 'float'>    1
<class 'str'>      1
Name: Name, dtype: int64

>>> df['Name'].apply(type).value_counts()
<class 'float'>    1
<class 'str'>      1
Name: Name, dtype: int64
>>> 

要获取实际的类型名称,请使用:

>>> df['Name'].map(lambda x: type(x).__name__).value_counts()
float    1
str      1
Name: Name, dtype: int64
>>> 

您可以将 map 更改为 applyagg,代码仍会按预期工作。

我使用 type.__name__ 获取类型名称,更多信息请参阅文档 here:

The name of the class, function, method, descriptor, or generator instance.