2014-12-17 2 views
2

비슷한 질문을했지만 여러 칼럼으로 그룹화 된 DataFrames의 날짜는 작동하지 않습니다.팬 그룹의 그룹 당 최대 날짜 DataFrame

person_ref post_start post_number post_title     change_date 
0 123   2010-08-08 000492  Doorman      2014-04-17 
1 123   2010-08-08 000492  Doorman/Porter    2014-01-14 
2 123   2010-08-08 000492  Uniformed Security Officer 2005-12-16 
3 123   2011-04-03 000554  Security Officer    2011-01-01 
4 123   2010-07-15 000568  Night Security Officer  2010-06-30 
5 456   2012-09-17 5080   HR Systems & MI Analyst  2013-08-13 
6 456   2012-09-17 5080   HR Systems & MI Adviser  2011-04-07 
7 456   2012-09-17 5080   HRIS Adviser     2010-06-14 
8 456   2012-09-17 5080   HR Systems Assistant   2007-09-21 

이 게시물은 자신의 역사를 통해 한 것을 두 직원 (person_ref)가 개최 한 게시물 (post_number), 및 제목을 보여줍니다

나는이처럼 보이는 팬더 DataFrame이 (post_title). post_start은 직원이 게시물에서 시작한 날짜이고 change_date은 게시물 제목이 변경된 날짜입니다. 그들은 게시물에 시작했을 때

나는 각 직원 만 게시물 제목을 표시하는 DataFrame와 끝까지하려는 :

person_ref post_start post_number post_title     change_date 
0 123   2010-08-08 000492  Uniformed Security Officer 2005-12-16 
1 123   2011-04-03 000554  Security Officer    2011-01-01 
2 123   2010-07-15 000568  Night Security Officer  2010-06-30 
3 456   2012-09-17 5080   HR Systems & MI Adviser  2011-04-07 

를 이것이보다 작거나 같은 최대 change_date입니다 아래로 비등 날짜는 person_ref/post_number입니다.

나는 이것을 팬더에서 어떻게 얻을 수 있습니까? 나는 person_refpost_number으로 그룹화 할 필요가 있다고 생각하지만 정확한 변경 날짜가있는 행만 얻으려고 애 쓰고 있습니다.

답변

4
df[(df.change_date <= df.post_start)] 
    .sort_values(
     by=['person_ref','post_number','post_start','change_date'], 
     ascending=[1,1,1,0]) 
    .groupby(['person_ref','post_number']) 
    .first().reset_index() 
+0

다시 연락해 주셔서 감사합니다. 이것은 거의 효과가 있었지만 HR Systems & MI Adviser 대신 HR Systems Assistant를 456 –

+0

으로 불러 들였습니다. df [(df.change_date <= df.post_start)] sort ([ 'person_ref', 'post_number', ' groupby ([ 'person_ref', 'post_number']). 첫 번째(). reset_index() – steboc

+0

맞아요. 고마워요. –

관련 문제