Pandas获得每组首次出现条件的列值

时间:2017-10-07 19:34:50

标签: python pandas lambda group-by apply

我有一个pandas数据帧如下:

player  condition   num
A       0           1
A       1           2
A       1           3
B       0           1
B       0           2
B       1           3
B       0           4

我想添加一个列,其中存储num列的最小值 per player condition 1

因此,结果应如下所示:

player  condition   num  numCondition
A       0           1    2
A       1           2    2
A       1           3    2
B       0           1    3
B       0           2    3
B       1           3    3
B       0           4    3

我知道每groupBy()我需要player。然后我需要apply(),可能使用lambda()函数。但我还是不能把这些碎片放在一起。

编辑:condition列是我示例中的简化。实际上,应该可以使用通常的pandas数据帧查询来过滤行。例如。 df[(df.condition == 1) & (df.otherCondition > 10)]

2 个答案:

答案 0 :(得分:2)

使用<?php namespace StoreBundle\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Symfony\Bundle\FrameworkBundle\Controller\Controller; class ProductController extends Controller { /** * * @Route("/products") * */ public function indexAction() { $em = $this->getDoctrine()->getManager(); $data = $em->getRepository('StoreBundle:Product')->findAll(); return $this->json(['data' => $data]); } /** * * @Route("/product") * @Method("POST") * */ public function newAction() { throw new \Exception('Method not yet implemented'); } /** * * @Route("/product/{id}") * */ public function showAction($id) { $em = $this->getDoctrine()->getManager(); $data = $em->getRepository('StoreBundle:Product')->findById($id); return $this->json(['data' => $data]); } /** * * @Route("/product/{id}/update") * @Method("PUT") * */ public function updateAction($id) { throw new \Exception('Method not yet implemented'); } /** * * @Route("/product/{id}/delete") * @Method("DELETE") * */ public function deleteAction($id) { throw new \Exception('Method not yet implemented'); } }

drop_duplicates

答案 1 :(得分:1)

首先汇总,然后与df上的player加入:

df.join(
    df.groupby('player')
      .apply(lambda g: g.num[g.condition == 1].min())
      .rename('numCondition'), 
on='player')

# player  condition   num   numCondition
#0     A          0     1   2
#1     A          1     2   2
#2     A          1     3   2
#3     B          0     1   3
#4     B          0     2   3
#5     B          1     3   3
#6     B          0     4   3