2014-05-11 20 views
0

은 내가 팬더의 dataframe 있습니다GROUPBY와 팬더에 집계 작업을 Dataframe

 Date  Type  Section  Status 
-------------------------------------------- 
0  1-Apr Type1  A   Present 
1  1-Apr Type2  A   Absent 
2  1-Apr Type2  A   Present 
3  1-Apr Type1  B   Absent 
4  2-Apr Type1  A   Present 
5  2-Apr Type2  C   Present 
6  2-Apr Type2  C   Present  
나는 조금 다른 형식으로 DF를 GROUPBY 싶습니다

:

 Date  Type  A_Pre A_Abs B_Pre B_Abs C_Pre C_Abs 
------------------------------------------------------------------------------ 
0  1-Apr Type1  1 0  0  1  0  0 
1    Type2  1 1  0  0  0  0 
2  2-Apr Type1  1 0  0  0  0  0   
3    Type2  0 0  0  0  1  1   

내가 집계를 얻으려면 항목이 날짜 및 유형별로 그룹화 된 다음 원래 유형 표로보고 한 다음 다양한 유형으로 분리하십시오. 시도 2 일 후에이 접근법을 처리하는 방법을 알지 못합니다.

도움을 주시면 감사하겠습니다.

+0

을이이 거의 중복 : http://stackoverflow.com/questions/23580009/data-processing-with-adding-columns-dynamically-in-python-pandas-dataframe – Jeff

+0

링크를 이용해 주셔서 감사합니다. 내가 내 대답을 얻을 수 있다면 여기에서 그것을 업데이트 할 것이다. 고마워. –

+0

실제로 질문이 더 간단 할 수도 있습니다 : df.groupby (..) [ 'Status']. apply (pd.get_dummies) – Jeff

답변

1

첫째로 나는 당신이 0과 1로 채워 집계 한 다음 GROUPBY를 사용하여 값의 단순 합을하고자하는 열을 ... 만들 것

나는이 밖으로 시도 할 수 didnt는, 그러나 나는 생각한다 다음 작동합니다 :

Present = ['A_Pre', 'B_Pre', 'C_Pre' ] 
Absent = ['A_Abs', 'B_Abs', 'C_Abs' ] 

for string in Present: 
    DF[string] = pd.Series([1 if stat == 'Present' and sect == string[0] else 0 
          for stat, sect in zip(DF['Status'], DF['Section'])], 
          index = DF.index) 
for string in Absent: 
    DF[string] = pd.Series([1 if stat == 'Absent' and sect == string[0] else 0 
          for stat, sect in zip(DF['Status'], DF['Section'])], 
          index = DF.index) 

DF.groupby(['Date', 'type']).agg(sum)