Friday, January 24, 2025
HomeProgrammingGet unique values from a column in Pandas DataFrame

Get unique values from a column in Pandas DataFrame

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)
RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x