Get Unique Values from a Column in Pandas DataFrame in Python5 Jan 2025 | 4 min read IntroductionOne of the most powerful data manipulation libraries in Python is Pandas. In addition, it provides a range of structured data functions. Actually about the DataFrames in particular, one often just needs to consider only unique values for a certain column. In this chapter we examine some of the methods for obtaining all those elements you require. Understanding Pandas DataFrameSo first off, we'll skip ahead a bit and quickly cover some basic facts about Pandas DataFrames. That is, before getting into the technical details of how to get unique values. A DataFrame is a two-dimensional labeled data table, with rows and columns. It was custom made for data work, sitting on the shoulders of NumPy. Output: Name Age City 0 Alice 25 New York 1 Bob 30 San Francisco 2 Alice 25 New York 3 Charlie 35 Los Angeles 4 Bob 30 San Francisco Method 1: Using 'unique()' MethodPandas unique() method is an efficient way to get the unique elements of a column. It returns an array containing only unique values, in the order that they appear in DataFrame. Output: Unique Names: ['Alice' 'Bob' 'Charlie'] In this piece of code, 'unique()' in Pandas gets the unique values from df['Name'] column. unique_names is an array of the original names displayed in order. This print statement displays these special names. Method 2: Using 'value_counts()' MethodIn addition to giving unique values, the 'value_counts()' method also counts their occurrences. If you want to know how many times each unique element occurs in a given column, it can be very useful. Output: Name Counts: Bob 2 Alice 2 Charlie 1 Name: Name, dtype: int64 Here, the 'value_counts()' method is used to extract both unique names and their counts from that Name column. The result of name_counts is a Pandas Series providing the frequency distribution for all unique names. Method 3: Using 'drop_duplicates()' MethodA second way to get unique values is the 'drop_duplicates()' method. Unlike unique(), this method returns a new DataFrame containing no duplicates. Output:
DataFrame with Unique Names:
Name Age City
0 Alice 25 New York
1 Bob 30 San Francisco
3 Charlie 35 Los Angeles
Drop duplicate rows based on the 'Name' column (unique_df) using drop_duplicates(). As a result, our DataFrame retains only the first instance of each unique name and we have set which is clean. Method 4: Applying a SetBy definition, Python's set stores only unique elements. If we change a column to set, then finding all the distinct values is easy. Output:
Unique Cities: {'San Francisco', 'Los Angeles', 'New York'}
This tiny piece of code turns the 'City' column into a set (unique_cities). Since sets, by definition, contain only non-repeated elements this procedure finds the city names that are different from DataFrame and prints them. Method 5: Using 'nunique()' MethodThe method 'nunique()' returns the number of unique elements in a column. It's especially good when what you want is a count of unique values but without having to enumerate them. Output: Number of Unique Names: 3 'nunique()' calculates the number of unique names in that column, returning a single numeric value (num_unique_names). The print statement shows the number of unique names. Method 6: Custom Functions for Unique ValuesIn other cases, you will have to introduce custom logic of your own in order determine unique values. It could also involve using a function which checks for uniqueness based on certain criteria. Output: Unique Names based on Custom Logic: [] A custom function ('custom_unique_check') is defined to check uniqueness according to a specific standard, for example that the name be even in length. This function is then applied to 'Name' using the 'apply() method, and the resulting DataFrame contains all values meeting our custom condition. The names that meet the criterion are then printed in a unique form. ConclusionIn this exhaustive guide we went over how to extract novel values from a column in Pandas DataFrame. Whether your precision requirements dictate the use of built-in methods such as 'unique()', 'value_counts()' and/or, drop duplicates (), or you choose to write custom functions, Pandas offers a range options for meeting all manner of needs. Knowing these skills are essential for data cleansing, preprocessing and analysis work which enable you to appreciate what makes your datasets special. As you continue your work with Pandas DataFrames, learning these methods will make it easier to break down and extract information from your data. Next TopicGet-utc-timestamp-in-python |
How to Change File Extension in Python
? Changing file extensions in Python includes modifying the filename of a file to supplant its current expansion with another one. This undertaking can be helpful in different situations, for example, information handling, file the board, or while working with various document designs. In principle, there...
6 min read
Decimal to Binary Algorithm in Python
What are Decimal Numbers? Decimal Numbers are the number system that uses 10 digits, from 0 to 9. The base of the decimal number system is 10. It is also known as the base-10 number system. It is used to form digits with different combinations. Each...
4 min read
Import from Parent Directory in Python
In Python, the ability to import modules and packages is critical for code organisation and reuse. You may need to import modules or packages from the parent directory of your current Python script or module. This is especially useful if you have a project with...
3 min read
How to Write a Case-Insensitive Python Regular Expression Without re.compile
? Introduction: In this tutorial we are learning to write the case insensitive Python regular expression or regex without re.compile method. Regular expressions or regex are useful tools for pattern matching and searching in the strings. By default, the regular expression patterns are case sensitive; that is,...
4 min read
Printing Patterns in Python
Pattern questions are very common in programming. These questions help in getting familiar with the flow of the programs and also help in understanding how to write programs to get the desired output. Patterns are printed using loops. We generally use nested loops to print...
22 min read
Transliterate in Python
Transliteration is the method of changing content from one script to another while keeping pronunciation. Not at all like translation, which centers on meaning, transliteration looks to preserve word sounds. It is often exceptionally convenient for transcribing outside names, specialized expressions, or social expressions in dialects...
4 min read
How to Encrypt and Decrypt Strings in Python
? An Introduction to Encryption and Decryption Encryption and its counterpart, decryption play an important role in cybersecurity as they are the method by which sensitive data can be protected from unauthorized access. These methods are instrumental in protecting from communications, personal information, financial transactions and even governmental...
7 min read
Find Most and Least Frequent Element of the Array
In this problem, we are given an array of integers. We have to find the element that has occurred the most number of times and the element that has occurred the least number of times in the array. If there are multiple elements with the...
10 min read
How to Read and Write Unicode (UTF-8) Files in Python
? Introduction Python's Unicode (UTF-eight) studying and writing capabilities cope with textual content encoded in a format that helps a huge variety of languages and characters. A popular Unicode encoding widespread that works with lots of devices and structures is UTF-8. Using Python's open() technique and the...
4 min read
Python re.findall() Method
The re.findall() method in Python returns all occurrences of a pattern in a string. You can look at it as scanning a sentence for a word that has a defined "format". re.findall() finds all matches with the help of regular expression (regex) for defining the...
4 min read
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India