import pandas as pd
data = {"number":[1,1,np.nan,np.nan,2,2,1,2,2],
"letter":['a','b',np.nan,np.nan,'c','d','b','c','d'],
"label":[1,1,9,9,2,2,1,2,2],"label2":[33,12,9,9,2,2,1,2,2]}
dataset1 = pd.DataFrame(data) #初始化DataFrame 得到数据集dataset1
print(dataset1)
t_optarr=['letter','number']
sumobj={'label':sum,'label2':sum}
##去重 合并求和 并且保留原列表的函数
def rechong_sum_hwj(odata,optarr,sumobj):
duplicate_row = odata.duplicated(subset=optarr,keep=False)
duplicate_data = odata.loc[duplicate_row,:]
duplicate_data_sum = duplicate_data.groupby(by=optarr).agg(sumobj).reset_index(drop=True)
duplicate_data_one = duplicate_data.drop_duplicates(subset=optarr, keep="first").reset_index(drop=True)
no_duplicate=odata.drop_duplicates(subset=optarr,keep=False)
for i in sumobj:
duplicate_data_one[i] = duplicate_data_sum[i]
result2 = pd.concat([no_duplicate, duplicate_data_one])
return result2
##去重 合并求和 并且保留原列表的函数
print(rechong_sum_hwj(dataset,t_optarr,sumobj))
结果是这样:基本实现了又保留原列,又求和合并,
number letter label label2
0 1 a 1 33
1 1 b 1 12
2 xxx xxx 9 9
3 xxx xxx 9 9
4 2 c 2 2
5 2 d 2 2
6 1 b 1 1
7 2 c 2 2
8 2 d 2 2
number letter label label2
0 1 a 1 33
0 1 b 2 13
1 xxx xxx 4 4
2 2 c 4 4
3 2 d 18 18
Process finished with exit code 0