Search code examples
pythonpandasdataframesetunique

How to create sets for a column, separated by data types?


I want to create a set in which only the string values of a column appear. The numerical values of the column are to be stored in another set.


Solution

  • You can use:

    df['numeric'] = pd.to_numeric(df['col'], errors='coerce')
    df['non-numeric'] = df['col'].mask(df['numeric'].notna())
    

    Example:

       col  numeric non-numeric
    0  abc      NaN         abc
    1  123    123.0         NaN
    2  123    123.0         NaN
    3  NaN      NaN         NaN
    

    Used input:

    df = pd.DataFrame({'col': ['abc', 123, '123', float('nan')]})