如何使用python将csv中的数值替换为分类值[closed]

问题描述 投票:-2回答:1

我的问题是使用python将数值替换为csv文件中的字符串。目的是计算CDF(累积分布函数)。

数据集的名称是'hsb',类标签是'status',其具有304行数值数据1和2。我想用'positive'替换1,用'negative'替换2。

python python-3.x pandas cdf
1个回答
1
投票

考虑下面使用.map()映射dict值的示例。

 df = pd.DataFrame({
    'col':[1,1,2,2,2,1]
})

输出:

   col
0   1
1   1
2   2
3   2
4   2
5   1

现在,

df['col'] = df['col'].map({1:'positive', 2:'negative'})

输出:

       col
0   positive
1   positive
2   negative
3   negative
4   negative
5   positive
© www.soinside.com 2019 - 2024. All rights reserved.