Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· OpenAIAi2 WildChat-4.8M (AI2)

get unique not null column from dataframe

  1. User

    get unique not null column from dataframe

  2. Assistant

    To get the unique non-null values from a column in a dataframe, you can use the following steps:

    1. Import the required libraries:
    import pandas as pd
    
    1. Create a sample dataframe:
    df = pd.DataFrame({'Column1': [1, 2, 3, None, 4, 5, 5, None]})
    
    1. Use the unique() method to get unique values and dropna() method to remove null values:
    unique_values = df['Column1'].dropna().unique()
    
    1. Print the unique non-null values:
    print(unique_values)
    

    Here is the complete code:

    import pandas as pd
    
    df = pd.DataFrame({'Column1': [1, 2, 3, None, 4, 5, 5, None]})
    
    unique_values = df['Column1'].dropna().unique()
    
    print(unique_values)
    

    Output:

    [1. 2. 3. 4. 5.]
    

    In the above example, the column 'Column1' in the dataframe 'df' contains both unique and null values. By using the dropna() method, we remove the null values and then use the unique() method to get the unique non-null values from the column.