Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
有没有办法在Python中标记变量?_Python_Pandas_Statistics - Fatal编程技术网

有没有办法在Python中标记变量?

有没有办法在Python中标记变量?,python,pandas,statistics,Python,Pandas,Statistics,我有Python中的nexy代码 Id=np.arange(1,16) l=np.arange(1,101).tolist() Ing=random.choices(l,k=15) base2= pd.DataFrame({'Id':Id,'Ingreso':Ing,}) base2=base2.set_index('Id') base2['Grupo'] = pd.cut(base2['Ingreso'], bins=[0,30,50,70,100],labels=[1,2,3,4]

我有Python中的nexy代码

Id=np.arange(1,16)

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

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

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

base2=base2.set_index('Id')

base2['Grupo'] = pd.cut(base2['Ingreso'], bins=[0,30,50,70,100],labels=[1,2,3,4], include_lowest=True)

grouped = base2.groupby(['Ingreso', 'Grupo'], as_index=False)

s=np.arange(0,2).tolist()

base2['Sexo']= random.choices(s,k=15)

有了这个数据帧,我想做一个交叉表

pd.crosstab(base2.Grupo,base2.Sexo)
这是我的输出

Sexo   0  1
Grupo      
1      1  2
2      2  2
3      0  3
4      3  2
但是我想要这样的东西

Sexo Men Women 
Grupo      
1      1  2
2      2  2
3      0  3
4      3  2
它可以在Python中使用吗?

请在使用之前使用

或者之后

输出

Sexo Men Women 
Grupo      
1      1  2
2      2  2
3      0  3
4      3  2
试试pd.crosstab(base2.Grupo,base2.Sexo)
pd.crosstab(base2.Grupo, base2.Sexo).rename(columns={0 : 'Men', 1 : 'Women'})
Sexo Men Women 
Grupo      
1      1  2
2      2  2
3      0  3
4      3  2