variáveis Agrupate em Python

Luis Rodríguez Fuentes:

Eu tenho esse código em R, mas eu quero fazer o mesmo processo em python, eu não sei como mudar o nome de um valor para as variáveis ​​de trama de dados

ingresos <- sample(0:100,15,replace = T)

sexo <- sample(0:1,15,replace=T)

base2 <- data.frame(ingresos, sexo)

base2$grupo[as.numeric(base2$ingresos) >= 0 &
              as.numeric(base2$ingresos)<=29] <- 1

base2$grupo[as.numeric(base2$ingresos) >= 30 &
              as.numeric(base2$ingresos)<=49] <- 2

base2$grupo[as.numeric(base2$ingresos) >= 50 &
              as.numeric(base2$ingresos)<=69] <- 3

base2$grupo[as.numeric(base2$ingresos) >= 70] <- 4

base2

Este é o meu avanço em python

Id=np.arange(1,16)

Grupo=np.arange(1,16)

l=np.arange(1,101).tolist()

Ing=random.choices(l,k=15)

base2= pd.DataFrame({'Id':Id,'Ingreso':Ing,'Grupo':Grupo})

base2=base2.set_index('Id')

Obrigado pelo seu tempo

StupidWolf:

Em R você faz:

base2$bins = cut(base2$ingresos,breaks=c(0,30,50,70,+Inf),
include.lowest=TRUE,right=FALSE)

   ingresos sexo     bins
1        38    0  [30,50)
2        98    0 [70,Inf]
3        17    1   [0,30)
4        76    1 [70,Inf]
5        54    0  [50,70)
6        91    1 [70,Inf]
7         4    0   [0,30)
8        68    0  [50,70)
9         9    0   [0,30)
10       32    0  [30,50)
11       13    0   [0,30)
12       64    1  [50,70)
13       35    1  [30,50)
14       44    0  [30,50)
15       63    0  [50,70)

Em pandas que você pode fazer:

base2['bins'] = pd.cut(base2['Ingreso'],
bins=[0,30, 50,70,+np.Inf],include_lowest=True,right=False)


Ingreso Grupo   bins
Id          
1   57  1   [50.0, 70.0)
2   71  2   [70.0, inf)
3   25  3   [0.0, 30.0)
4   45  4   [30.0, 50.0)
5   26  5   [0.0, 30.0)
6   1   6   [0.0, 30.0)
7   51  7   [50.0, 70.0)
8   39  8   [30.0, 50.0)
9   67  9   [50.0, 70.0)
10  78  10  [70.0, inf)
11  58  11  [50.0, 70.0)
12  27  12  [0.0, 30.0)
13  48  13  [30.0, 50.0)
14  75  14  [70.0, inf)
15  22  15  [0.0, 30.0)

Acho que você gosta

Origin http://43.154.161.224:23101/article/api/json?id=351568&siteId=1
Recomendado
Clasificación