To get the unique values from a specific column in a Pandas DataFrame, you can use the .unique()
method. Here’s an example:
import pandas as pd
# Sample DataFrame
data = {'Column1': [1, 2, 2, 3, 4, 4, 4, 5]}
df = pd.DataFrame(data)
# Get unique values from 'Column1'
unique_values = df['Column1'].unique()
print(unique_values)
This will output:
[1 2 3 4 5]
The .unique()
method returns an array of unique values in that column. If you want the result as a list, you can simply call tolist()
like so:
unique_values_list = df['Column1'].unique().tolist()
print(unique_values_list)