gpt-3.5-turbo-0613· OpenAI
WildChat-4.8M (AI2)
get unique not null column from dataframe
- User
get unique not null column from dataframe
- Assistant
To get the unique non-null values from a column in a dataframe, you can use the following steps:
- Import the required libraries:
import pandas as pd- Create a sample dataframe:
df = pd.DataFrame({'Column1': [1, 2, 3, None, 4, 5, 5, None]})- Use the
unique()method to get unique values anddropna()method to remove null values:
unique_values = df['Column1'].dropna().unique()- 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 theunique()method to get the unique non-null values from the column.