Chau Loi
Chau Loi

Reputation: 1225

How to check data types within a column with multiple types ? -Looking for better solution

Data Sample:

a=pd.DataFrame({'Col1':[1,2,'a','b',1.5],'Col2':['a','b','c',2,3],'Col3':['a',1.2,1.3,1.4,2]}))

  Col1 Col2 Col3
0    1    a    a
1    2    b  1.2
2    a    c  1.3
3    b    2  1.4
4  1.5    3    2

As you can see within a column there are str,int, float.

My code below is to check the type and count how many of it. Even though It is kind of amateur, please have a look.

def checkDtype(df='DateFrame',s='Series'):
 ListOfType={}
 for i in df.columns:              #Walk through every columns of DataFrame
     ListOfType[i]=dict(Float=0,Int=0,Str=0)
     for a in df[i]:               #Walk through every row of the selected column
         if type(a) is float:
             ListOfType[i]['Float']+=1
         if type(a) is int:
             ListOfType[i]['Int']+=1
         if type(a) is str:
             ListOfType[i]['Str']+=1
 return ListOfType

`

b= checkDtype(df=a)                #The result - put into DataFrame for visual
result=pd.DataFrame(data=[a['Col1'],a['Col2'],a['Col3']],index=['col1','col2','col3'])
result.T 

       col1  col2  col3
Float     1     0     3
Int       2     2     1
Str       2     3     1

I am looking for a better solution.

Moreover, in the case that someone already "touch" the data and eventually change the type to Int64 or Int32, etc. I think this application will not gonna work. So please help me with this problem.

Upvotes: 2

Views: 1610

Answers (1)

anky
anky

Reputation: 75080

Try:

a.applymap(type).apply(pd.value_counts).fillna(0)

                 Col1  Col2  Col3
<class 'str'>       2   3.0     1
<class 'int'>       2   2.0     1
<class 'float'>     1   0.0     3

Upvotes: 6

Related Questions