2024-10-02 00:43:28 发布
网友
我有一个Pandas DataFrame,它以最后一列作为标签,是一个值为[0, 1]的分类变量,我们称之为df['label']。数据帧如下所示:
[0, 1]
df['label']
feat1 feat2...featn label . . . 0 . . . 1 . . . . . . . 1
现在我需要返回一个名为sex,长度为2的Pandas系列,包含这些整数值和index=['male', 'female']。到目前为止,我只能使用交叉表函数,但无法获得所需的系列。在
sex
['male', 'female']
如果没有df外观的示例,或者预期输出的图像,我假设value_counts()函数可能就是您想要的?在
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())
如果关了就告诉我?在
如果没有df外观的示例,或者预期输出的图像,我假设value_counts()函数可能就是您想要的?在
如果关了就告诉我?在
相关问题 更多 >
编程相关推荐