将分类变量的分布返回为Pandas系列

2024-10-02 00:43:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个Pandas DataFrame,它以最后一列作为标签,是一个值为[0, 1]的分类变量,我们称之为df['label']。数据帧如下所示:

feat1   feat2...featn   label
.       .       .       0
.       .       .       1
.       .       .       .
.       .       .       1

现在我需要返回一个名为sex,长度为2的Pandas系列,包含这些整数值和index=['male', 'female']。到目前为止,我只能使用交叉表函数,但无法获得所需的系列。在

^{pr2}$

Tags: 数据dataframepandasdfindex分类整数标签
1条回答
网友
1楼 · 发布于 2024-10-02 00:43:28

如果没有df外观的示例,或者预期输出的图像,我假设value_counts()函数可能就是您想要的?在

Click Here to view input

 import pandas as pd
df = pd.DataFrame({'Label_1':[0,1,1,1]})

df.Label_1.replace(to_replace=1, value='Male', inplace=True) 
df.Label_1.replace(to_replace=0, value='Female',inplace=True)


pd.DataFrame(df.Label_1.value_counts())

Click Here to view Output

如果关了就告诉我?在

相关问题 更多 >

    热门问题