Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何删除dataframe中特定列多次出现的行_Python_Pandas - Fatal编程技术网

Python 如何删除dataframe中特定列多次出现的行

Python 如何删除dataframe中特定列多次出现的行,python,pandas,Python,Pandas,我有一个包含3列和大量行的数据框 A B C D E aa hi 43 21 22 45 ab helo 44 65 86 94 ac hola 42 71 91 44 ad hi 12 79 45 12 ae hey 81 14 34 42 af hi 21 45 12 02 ag hola

我有一个包含3列和大量行的数据框

     A     B     C     D     E
aa   hi    43    21    22    45
ab   helo  44    65    86    94
ac   hola  42    71    91    44
ad   hi    12    79    45    12
ae   hey   81    14    34    42
af   hi    21    45    12    02
ag   hola  04    12    39    65
我希望删除列A中的所有多个引用,保留第一行并删除其余行,因此我希望数据帧如下所示

     A     B     C     D     E
aa   hi    43    21    22    45
ab   helo  44    65    86    94
ac   hola  42    71    91    44
ae   hey   81    14    34    42
与参数
subset
一起用于检查重复的列:

df = df.drop_duplicates(subset=['A'])
#same as keep='first', because default value can be omited
# df = df.drop_duplicates(subset=['A'], keep='first')
print (df)
       A   B   C   D   E
aa    hi  43  21  22  45
ab  helo  44  65  86  94
ac  hola  42  71  91  44
ae   hey  81  14  34  42
也可以仅保留最后一行:

df = df.drop_duplicates('A', keep='last')
print (df)
       A   B   C   D   E
ab  helo  44  65  86  94
ae   hey  81  14  34  42
af    hi  21  45  12   2
ag  hola   4  12  39  65
See的可能副本