x89
x89

Reputation: 3460

List all data types present in dataframe column

How can I list all datatypes present in a column Name of my dataframe df?

Name
1.0
XCY

Some might be strings, some might be floats etc.

Upvotes: 2

Views: 308

Answers (1)

U13-Forward
U13-Forward

Reputation: 71560

Try using map, agg or apply:

>>> 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
>>> 

To get the actual type names, use:

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

You can change map to apply or agg and the code would still work as expected.

I use type.__name__ to get the type name, more about it in the documentation here:

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

Upvotes: 5

Related Questions