Skip to main content
 首页 » 编程设计

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

2024年10月24日12unruledboy

除非某些条件,否则我想使用参数“take_last = True”删除与数据帧中“a”列相关的重复行。例如,如果我有以下数据框

 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 

请您参考如下方法:

#   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