2017-12-11 1 views
0

저는 판다 (pandas) 데이터 프레임 내에 1-843300 범위의 컬럼을 가지고 있습니다.이 범위는 pd.cut 목적으로 4 등분으로 나누고 싶습니다. 이 일을하는 가장 비열한 방법이 무엇인지 궁금 해서요?팬더 데이터 프레임에서 열을 쿼티 처리하는 Python 방식이 있습니까?

DF가 호출 될 'news_df'열 레이블은 '주'이고 여기에 내가했던 방법 : 나는 파이썬 3

감사를 사용하고

max_shares = news_df.shares.max() 
weight_bins = [1,max_shares*0.25,max_shares*0.5,max_shares*0.75,max_shares] 

.

+0

난 당신이 pd.qcut이 한 단계를 할 수 있다고 생각합니다. –

답변

2

numpys linspace를 사용하면 그렇게 할 수 있습니다.

import numpy as np 
max_shares = 10 
weight_bins = np.linspace(0, max_shares, 5) 
weight_bins[0] = 1 
array([ 1. , 2.5, 5. , 7.5, 10. ]) 
3

당신은 pandas.qcut

예를 사용할 수 있습니다

df = pd.DataFrame({'Range':np.arange(1,14)}) 


    Range 
0  1 
1  2 
2  3 
3  4 
4  5 
5  6 
6  7 
7  8 
8  9 
9  10 
10  11 
11  12 

df.assign(qbins = pd.qcut(df.Range, 4, labels=['1st', '2nd', '3rd', '4th'])) 

출력 :

Range qbins 
0  1 1st 
1  2 1st 
2  3 1st 
3  4 2nd 
4  5 2nd 
5  6 2nd 
6  7 3rd 
7  8 3rd 
8  9 3rd 
9  10 4th 
10  11 4th 
11  12 4th 
관련 문제