Issue
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
Solution
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
Answered By - Tarun Kolla
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.