Home >Backend Development >Python Tutorial >How to Extract Rows Based on Distinct Values in a Dataframe Column?

How to Extract Rows Based on Distinct Values in a Dataframe Column?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-11-04 10:41:30612browse

How to Extract Rows Based on Distinct Values in a Dataframe Column?

Querying for Distinct Values in a Dataframe Column

When working with dataframes, it is often necessary to retrieve rows based on distinct values in a specific column. This allows us to eliminate duplicate values and obtain a unique set of data points.

Consider the following dataframe:

COL1   COL2
a.com  22
b.com  45
c.com  34
e.com  45
f.com  56
g.com  22
h.com  45

Suppose we want to extract the rows corresponding to the unique values in column COL2. To achieve this, we can utilize pandas' drop_duplicates function. This function takes a column name as an argument and removes duplicate rows from the dataframe.

<code class="python">import pandas as pd

df = pd.DataFrame(...)  # assuming the given dataframe
df = df.drop_duplicates('COL2')</code>

By default, drop_duplicates retains the first occurrence of each unique value. Alternatively, we can specify keep='last' to keep the last occurrence or keep=False to remove all duplicate rows:

<code class="python"># Keep first occurrence
df = df.drop_duplicates('COL2', keep='first')

# Keep last occurrence
df = df.drop_duplicates('COL2', keep='last')

# Remove all duplicates
df = df.drop_duplicates('COL2', keep=False')</code>

After executing any of the above commands, the dataframe df will contain only the rows corresponding to the distinct values in column COL2:

    COL1  COL2
0  a.com    22
1  b.com    45
2  c.com    34
4  f.com    56

The above is the detailed content of How to Extract Rows Based on Distinct Values in a Dataframe Column?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn