在特定列的数据框中删除重复的行

tho*_* is 0 python dataframe python-3.x pandas

我有一个如下数据框:


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

Run Code Online (Sandbox Code Playgroud)

如何根据列DistrictName删除行并选择第一个唯一值

我想要的输出:

    Districtname    pincode
0   central delhi   110001

Run Code Online (Sandbox Code Playgroud)

Tar*_*lla 5

数据帧可以使用删除pandas.DataFrame.drop_duplicates(),默认情况下保留第一次出现。在你的情况下DataFrame.drop_duplicates(subset = "Districtname")应该工作。如果您想更新相同的DataFrame DataFrame.drop_duplicates(subset = "Districtname", inplace = True)可能会有所帮助。有关更多信息,请单击此处