pythonpandasdataframecomplex-data-types

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


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


Solution

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