0

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

1
  • 5
    df.drop_duplicates('Districtname') ? Commented Sep 2, 2019 at 17:19

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

Comments

1

Use drop_duplicates with inplace=true:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.