2017-04-09 1 views
1

나는 location_id, customers, cluster의 3 개의 열이있는 Dataframe을 가지고 있습니다. 이전에는 데이터로 5 클러스터로 클러스터링했습니다. 따라서 클러스터 열은 [0, 1, 2, 3, 4] 값을 포함합니다.데이터 그룹 groupby 그룹의 조각 만들기

테스트의 다음 단계를 위해 각 클러스터를 2 개의 슬라이스로 분리하고 싶습니다. 예 : 50-50 슬라이스 또는 30-70 슬라이스 또는 20-80 슬라이스.

질문 - 열을 data.groupby('cluster')에 추가하는 함수를 어떻게 적용합니까?

이상적인 결과

location_id  customers cluster slice 
0  149213  132817  1  1 
1  578371  76655  1  0 
2  91703  74048  2  1 
3  154868  62397  2  1 
4  1022759  59162  2  0 

업데이트

MaxU의 솔루션 @ 옳은 길에 나를 넣어. 해결책은 dataframe.assign 함수를 사용하여 새 열을 추가하고 현재 인덱스/전체 인덱스 길이를 확인하여 올바른 비율의 슬라이스를 할당하는 것입니다. 그러나 아래의 코드는 어떻게 든 나에게 도움이되지 못했습니다. @ MaxU의 솔루션을 별도의 단계로 나눠서 끝내면 효과가있었습니다.

testgroup= (data.groupby('cluster') 
.apply(lambda x: x.assign(index1=(np.arange(len(x)))) 
)) 
testgroup= (testgroup.groupby('cluster') 
.apply(lambda x: x.assign(total_len=len(x)) 
)) 

testgroup['is_slice'] = ((testgroup['index1']/testgroup['total_len']) <= 0.5) 

      location_id customers cluster index1 total_len is_slice 

    0  149213  132817  1  0  12 True 
    1  578371   76655  1  1  12 True 
    2   91703   74048  1  2  12 True 
    3  154868   62397  1  3  12 True 
    4  1022759   59162  1  4  12 True 
    5   87016   58134  1  5  12 True 
    6  649432   56849  1  6  12 False 
    7  219163   56802  1  7  12 False 
    8   97704   54718  1  8  12 False 
    9  248455   52806  1  9  12 False 
    10  184828   52783  1 10  12 False 
    11  152887   52565  1 11  12 False 
+0

원래 DataFrame의 예를 제공해주십시오. – greole

답변

1

이 시도 :

In [31]: df = pd.concat([df] * 3, ignore_index=True) 

In [32]: df 
Out[32]: 
    location_id customers cluster 
0  149213  132817  1 
1  578371  76655  1 
2   91703  74048  2 
3  154868  62397  2 
4  1022759  59162  2 
5  149213  132817  1 
6  578371  76655  1 
7   91703  74048  2 
8  154868  62397  2 
9  1022759  59162  2 
10  149213  132817  1 
11  578371  76655  1 
12  91703  74048  2 
13  154868  62397  2 
14  1022759  59162  2 

조각 30 ~ 70 :

In [34]: (df.groupby('cluster') 
    ...: .apply(lambda x: x.assign(slice=((np.arange(len(x))/len(x)) <= 0.3).astype(np.uint8))) 
    ...: .reset_index(level=0, drop=True) 
    ...:) 
    ...: 
Out[34]: 
    location_id customers cluster slice 
0  149213  132817  1  1 
1  578371  76655  1  1 
5  149213  132817  1  0 
6  578371  76655  1  0 
10  149213  132817  1  0 
11  578371  76655  1  0 
2   91703  74048  2  1 
3  154868  62397  2  1 
4  1022759  59162  2  1 
7   91703  74048  2  0 
8  154868  62397  2  0 
9  1022759  59162  2  0 
12  91703  74048  2  0 
13  154868  62397  2  0 
14  1022759  59162  2  0 

조각 20 ~ 80 :

In [35]: (df.groupby('cluster') 
    ...: .apply(lambda x: x.assign(slice=((np.arange(len(x))/len(x)) <= 0.2).astype(np.uint8))) 
    ...: .reset_index(level=0, drop=True) 
    ...:) 
    ...: 
Out[35]: 
    location_id customers cluster slice 
0  149213  132817  1  1 
1  578371  76655  1  1 
5  149213  132817  1  0 
6  578371  76655  1  0 
10  149213  132817  1  0 
11  578371  76655  1  0 
2   91703  74048  2  1 
3  154868  62397  2  1 
4  1022759  59162  2  0 
7   91703  74048  2  0 
8  154868  62397  2  0 
9  1022759  59162  2  0 
12  91703  74048  2  0 
13  154868  62397  2  0 
14  1022759  59162  2  0 

이의이 샘플 DF가 큰 비트 만들어 보자

관련 문제