how to remove entire column if a particular row has duplicate values in a dataframe in python

隐身守侯 提交于 2019-12-11 05:55:51

问题


I have a dataframe like this,

 df,

        Name    City
   0    sri     chennai
   1    pedhci  pune
   2    bahra   pune

there is a duplicate in City column.

I tried:

df["City"].drop_duplicates()

but it gives only the particular column.

my desired output should be

output_df
        Name    City
   0    sri     chennai
   1    pedhci  pune

回答1:


You can use:

df2 = df.drop_duplicates(subset='City')

if you want to store the result in a new dataframe, or:

df.drop_duplicates(subset='City',inplace=True)

if you want to update df.

This produces:

>>> df
      City    Name
0  chennai     sri
1     pune  pedhci
2     pune   bahra
>>> df.drop_duplicates(subset='City')
      City    Name
0  chennai     sri
1     pune  pedhci

This will thus only take duplicates for City into account, duplicates in Name are ignored.



来源:https://stackoverflow.com/questions/45522478/how-to-remove-entire-column-if-a-particular-row-has-duplicate-values-in-a-datafr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!