user11669928
user11669928

Reputation:

Drop duplicate rows in a dataframe of particular column

I have a dataframe like the following:


    Districtname    pincode
0   central delhi   110001
1   central delhi   110002
2   central delhi   110003
3   central delhi   110004
4   central delhi   110005

How can I drop rows based on column DistrictName and select the first unique value

The output I want:

    Districtname    pincode
0   central delhi   110001

Upvotes: 0

Views: 84

Answers (2)

Tarun Kolla
Tarun Kolla

Reputation: 1014

Data Frames can be dropped using pandas.DataFrame.drop_duplicates() and defaults to keeping the first occurrence. In your case DataFrame.drop_duplicates(subset = "Districtname") should work. If you would like to update the same DataFrame DataFrame.drop_duplicates(subset = "Districtname", inplace = True) will do the job. Docs: https://pandas.pydata.org/pandas-docs/version/0.17/generated/pandas.DataFrame.drop_duplicates.html

Upvotes: 4

ansev
ansev

Reputation: 30940

Use drop_duplicates with inplace=true:

df.drop_duplicates('Districtname',inplace=True)

Upvotes: 1

Related Questions