Search code examples
pythonpandasunique

pandas unique values multiple columns different dtypes


Similar to pandas unique values multiple columns I want to count the number of unique values per column. However, as the dtypes differ I get the following error: enter image description here

The data frame looks like enter image description here A small[['TARGET', 'title']].apply(pd.Series.describe) gives me the result, but only for the category types and I am unsure how to filter the index for only the last row with the unique values per column


Solution

  • Use apply and np.unique to grab the unique values in each column and take its size:

    small[['TARGET','title']].apply(lambda x: np.unique(x).size)
    

    Thanks!