Home > Software design >  How to find each row and column data type in pandas dataframe using apply, map or applymap?
How to find each row and column data type in pandas dataframe using apply, map or applymap?

Time:11-11

I have dataframe as shown in image. I want each row and columns data type using apply/map/applymap. How to get this datatype? Some columns have mixed datatype as highlighted e.g. list and str, some have list and dict.

[![samplepandasdataframe][1]][1]

[1]:

CodePudding user response:

If you want to have the evaluated type value of every cell you can use

def check_type(x):
     try: 
        return type(eval(x)) 
     except Exception as e:
        return type(x)

df.applymap(check_type)

If you want to also get how many datatypes you have you can use things like

df.applymap(type).value_counts()

or if you want to get the values for all of the dataframe instead of by column

np.unique(df.applymap(type).astype(str).values, return_counts=True)

  • Related