根据条件删除pandas DataFrame中的重复行

时间:2015-10-07 14:53:58

标签: python pandas dataframe

我希望删除dataFrame中列'a'的重复行,参数'take_last = True',除非有一些条件。例如,如果我有以下dataFrame

 a | b | c
 1 | S | Blue 
 2 | M | Black
 2 | L | Blue
 1 | L | Green

我想删除关于列'a'的重复行,一般规则为take_last = true,除非某些条件为c ='Blue',在这种情况下我想使参数take_last = false。

所以我得到这个作为我的结果df

 a | b | c
 1 | L | Green
 2 | M | Black

1 个答案:

答案 0 :(得分:1)

#   a  b      c
#0  1  S   Blue
#1  2  M  Black
#2  2  L   Blue
#3  1  L  Green

#get first rows of groups, sort them and reset index; delete redundant col index
df1 = df.groupby('a').head(1).sort('a').reset_index()
del df1['index']

#get last rows of groups, sort them and reset index; delete redundant col index
df2 = df.groupby('a').tail(1).sort('a').reset_index()
del df2['index']
print df1
#   a  b      c
#0  1  S   Blue
#1  2  M  Black
print df2
#   a  b      c
#0  1  L  Green
#1  2  L   Blue

#if value in col c in df1 is 'Blue' replace this row with row from df2 (indexes are same)
df1.loc[df1['c'].isin(['Blue'])] = df2
print df1
#   a  b      c
#0  1  L  Green
#1  2  M  Black