我有以下数据框:
import pandas as pd
data = dict(name=['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c'],
objective=[20.0, 20.0, 25.0, 40.0, 40.5, 41.0, 60.0, 60.0],
price=[0.5, 1.0, 1.5, 1.0, 1.2, 1.4, 0.5, 1.0])
df = pd.DataFrame(data, columns=data.keys())
然后我可以找到所有这些的唯一组合,如下所示:
df.groupby(['name','objective', 'price']).size()
如下所示:
name objective price
a 20.0 0.5 1
1.0 1
25.0 1.5 1
b 40.0 1.0 1
40.5 1.2 1
41.0 1.4 1
c 60.0 0.5 1
1.0 1
当给定的price
和name
有多个objective
值时,我想保留较低的price
值 only ,即
name objective price
a 20.0 0.5 1
25.0 1.5 1
b 40.0 1.0 1
40.5 1.2 1
41.0 1.4 1
c 60.0 0.5 1
请问我该如何实现?
答案 0 :(得分:2)
您可以再做一个groupby
和first
:
(df.groupby(['name','objective', 'price']).size()
.reset_index()
.groupby(['name', 'objective'])
.first()
)
输出:
price 0
name objective
a 20.0 0.5 1
25.0 1.5 1
b 40.0 1.0 1
40.5 1.2 1
41.0 1.4 1
c 60.0 0.5 1
答案 1 :(得分:2)
我会做什么
df.sort_values('price').drop_duplicates(['name','objective'],keep='last').assign(cnt=1)
Out[421]:
name objective price cnt
0 a 20.0 0.5 1
2 a 25.0 1.5 1
3 b 40.0 1.0 1
4 b 40.5 1.2 1
5 b 41.0 1.4 1
6 c 60.0 0.5 1
答案 2 :(得分:1)
您可以使用groupby和minimum
app.get("/api/posts/latest-post", async (req, res) => {
try {
var result = await PostModel.findOne({published:true}).sort({"date.published.iso": -1}).exec();
res.send(result);
} catch (error) {
res.status(500).send(error);
}
});