当我以'name'为列名指定重复项时,将John
删除为重复项:
import pandas as pd
data = {'name':['Bill','Steve','John','John','John'], 'age':[21,28,22,30,29]}
df = pd.DataFrame(data)
df = df.drop_duplicates('name')
pandas删除所有匹配实体,仅保留最左侧的实体:
age name
0 21 Bill
1 28 Steve
2 22 John
我希望保留John年龄最大的行(在这个例子中是30岁)。如何实现?
df.drop_duplicates('name', keep='last')
或df.sort_values('age').drop_duplicates('name', keep='last')
- MaxU - stand with Ukraine