Python | Pandas Series.drop_duplicates ()

|

👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!

Series.drop_duplicates() Pandas Series.drop_duplicates() returns a series object with duplicate values ‚Äã‚Äãremoved from this series object.

Syntax: Series.drop_duplicates (keep = ’first’, inplace = False)

Parameter:
keep: {’first’, ’last’, False}, default ’first’
inplace: If True, performs operation inplace and returns None.

Returns: deduplicated: Series

Example # 1: Use Series.drop_duplicates () to remove duplicate values ‚Äã‚Äãfrom this series object.

# import pandas as pd

import pandas as pd


# Create series

sr = pd.Series ([ 80 , 25 , 3 , 25 , 24 , 6 ])


# Create index

index_ = [ ’Coca Cola’ , ’Sprite’ , ’ Coke’ , ’Fanta’ , ’ Dew’ , ’ThumbsUp’ ]


# set index

sr.index = index_


# Print series

print (sr)

Output:

We will now use Series.drop_duplicates () to remove duplicate values ‚Äã‚Äãin base data of this series object.

# remove duplicates

result = sr.drop_duplicates ()


# Print result

print (result)

Output:

As we can see in the output, Series.drop_duplicates () successfully Series.drop_duplicates ( ) duplicate records from this series object.

Example # 2: Use Series.drop_duplicates () to remove duplicate values ‚Äã‚Äãfrom this object series.

# import pandas as pd

import pandas as pd


# Create series

sr = pd.Series ([ 11 , 11 , 8 , 18 , 65 , 18 , 32 , 10 , 5 , 32 , 32 ])


# Create index

index_ = pd. date_range ( ’2010-10-09’ , periods = 11 , freq = ’M’ )


# set index

sr.index = index_


# Print series

print (sr)

Output:

We will now use Series.drop_duplicates () to remove duplicate values ‚Äã‚Äãin the underlying data of this series object.

# remove duplicates

result = sr.drop_duplicates ()


# Print result

print (result)

Output:

As we see in the output , Series.drop_duplicates () successfully Series.drop_duplicates () duplicate records from this series object.

👻 Read also: what is the best laptop for engineering students?

Python | Pandas Series.drop_duplicates () date_range: Questions

How do I filter query objects by date range in Django?

2 answers

I"ve got a field in one model like:

class Sample(models.Model):
    date = fields.DateField(auto_now=False)

Now, I need to filter the objects by a date range.

How do I filter all the objects that have a date between 1-Jan-2011 and 31-Jan-2011?

299

Answer #1

Use

Sample.objects.filter(date__range=["2011-01-01", "2011-01-31"])

Or if you are just trying to filter month wise:

Sample.objects.filter(date__year="2011", 
                      date__month="01")

Edit

As Bernhard Vallant said, if you want a queryset which excludes the specified range ends you should consider his solution, which utilizes gt/lt (greater-than/less-than).

299

Answer #2

You can use django"s filter with datetime.date objects:

import datetime
samples = Sample.objects.filter(sampledate__gte=datetime.date(2011, 1, 1),
                                sampledate__lte=datetime.date(2011, 1, 31))

We hope this article has helped you to resolve the problem. Apart from Python | Pandas Series.drop_duplicates (), check other date_range-related topics.

Want to excel in Python? See our review of the best Python online courses 2022. If you are interested in Data Science, check also how to learn programming in R.

By the way, this material is also available in other languages:



Ken Zelotti

San Francisco | 2022-12-05

date_range is always a bit confusing 😭 Python | Pandas Series.drop_duplicates () is not the only problem I encountered. I am just not quite sure it is the best method

Dmitry Ungerschaft

Abu Dhabi | 2022-12-05

Simply put and clear. Thank you for sharing. Python | Pandas Series.drop_duplicates () and other issues with date_range was always my weak point 😁. Will get back tomorrow with feedback

Julia Wu

Warsaw | 2022-12-05

Thanks for explaining! I was stuck with Python | Pandas Series.drop_duplicates () for some hours, finally got it done 🤗. I just hope that will not emerge anymore

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically