2015-01-29 5 views
0

가치를 점점 :그룹화 열의 값을 기준으로하고이 종자 데이터 집합 또 다른 열

In[1]: my_data = 
     [{'client':'A','product_s_n':'1','status':'in_store','month':'Jan'}, 
     {'client':'A','product_s_n':'1','status':'sending', 'month':'Feb'}, 
     {'client':'A','product_s_n':'2','status':'in_store','month':'Jan'}, 
     {'client':'A','product_s_n':'2','status':'in_store','month':'Feb'}, 
     {'client':'B','product_s_n':'3','status':'in_store','month':'Jan'}, 
     {'client':'B','product_s_n':'3','status':'sending', 'month':'Feb'}, 
     {'client':'B','product_s_n':'4','status':'in_store','month':'Jan'}, 
     {'client':'B','product_s_n':'4','status':'in_store','month':'Feb'}, 
     {'client':'C','product_s_n':'5','status':'in_store','month':'Jan'}, 
     {'client':'C','product_s_n':'5','status':'sending', 'month':'Feb'}] 
df = pd.DataFrame(my_data) 
df 

Out[1]: 
     client month product_s_n status 
0  A  Jan  1    in_store 
1  A  Feb  1    sending 
2  A  Jan  2    in_store 
3  A  Feb  2    in_store 
4  B  Jan  3    in_store 
5  B  Jan  4    in_store 
6  B  Feb  4    in_store 
8  C  Jan  5    sending 

내가이 데이터를 묻고 싶은 질문은 각 PRODUCT_SERIAL_NUMBER의 클라이언트가 무엇입니까?

product_s_n client 
0  1   A 
1  2   A 
2  3   B 
3  4   B 
4  5   C 

여러분도 알다시피, '상태'와 '달'필드된다 :이 예제의 데이터에서이 결과 DataFrame (나는 결과로 새로운 DataFrame 필요)과 같을 것이다 어떻게 이 샘플 데이터 세트의 데이터에 '감각'및 구조 만 제공하십시오. groupby를 사용하여 시도했지만 성공하지 못했습니다. 어떤 아이디어?

감사합니다.

답변

2

df.groupby(['product_s_n'])을 호출 한 후 ['client']을 사용하여 색인을 생성하면 특정 열에주의를 제한 할 수 있습니다. first()을 호출하여 각 그룹에서 첫 번째 값 client을 선택할 수 있습니다.

>>> df.groupby(['product_s_n'])['client'].first()  
product_s_n 
1    A 
2    A 
3    B 
4    B 
5    C 
Name: client, dtype: object 
+0

나는 그게 쉽지 않다고 생각합니다. 이것은'groupby'에 대한 새로운 아이디어의 세계를 열어줍니다. 나는 지금 내가 그것을 이해하기 시작하고 있다고 생각한다. 고마워요! – Andres

관련 문제