目标是在最小订单时选择独特的产品。
import pandas as pd
df = pd.DataFrame({'Order': [1,2,3],
'Product':['Apple','Apple','Orange'],
'Metric': ['20 x 2 g','20 x 2 g','23 x 2 g']})
df
Order Product Metric
0 1 Apple 20 x 2 g
1 2 Apple 20 x 2 g
2 3 Orange 23 x 2 g
预期输出:要选择Apple,请按订单号1的最低标准,选择公制20 x 2g。
Order Product Metric
0 1 Apple 20 x 2 g
1 3 Orange 23 x 2 g
请让我知道您的看法。谢谢!
将DataFrame.sort_values
与DataFrame.sort_values
一起使用:
groupby.first
或groupby.first
df.sort_values('Order').groupby('Metric',as_index=False).first()
输出:
DataFrame.drop_duplicates