如果特定行在python的数据框中具有重复的值,如何删除整列 [英] how to remove entire column if a particular row has duplicate values in a dataframe in python

查看:66
本文介绍了如果特定行在python的数据框中具有重复的值,如何删除整列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的数据框,

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.

我尝试过:

df["City"].drop_duplicates()

但是它只给出特定的列.

but it gives only the particular column.

我想要的输出应该是

output_df
        Name    City
   0    sri     chennai
   1    pedhci  pune

推荐答案

您可以使用:

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)

如果要更新df.

这将产生:

>>> 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

因此,这只会考虑City的重复项,而忽略Name中的重复项.

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

这篇关于如果特定行在python的数据框中具有重复的值,如何删除整列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆