DataFrame.drop_duplicates(subset=None, keep=‘first’, inplace=False)
参数说明
import pandas as pd
#创建数据框
df=pd.DataFrame({
'a':[1,2,4,3,3,3,4],
'b':[2,3,3,4,4,5,3]
})
print('去重前:\n',df)
#根据字段a进行去重,保留第一次出现的数据
df.drop_duplicates(['a'],keep='first',inplace=True)
print('去重后:\n',df)
>>>
去重前:
a b
0 1 2
1 2 3
2 4 3
3 3 4
4 3 4
5 3 5
6 4 3
去重后:
a b
0 1 2
1 2 3
2 4 3
3 3 4