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
Release: 2024-11-04 10:41:30
Original
607 people have browsed it

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
Copy after login

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>
Copy after login

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>
Copy after login

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
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template