我的数据集中有一个 X 列,其中包含以下数据
1,
2,
4,
5more,
3,
2
所以当我使用
dataset['X'].map({
"5more" : 5
})
它将所有值转换为 NaN
。
如何将这些数据集转换为没有 NaN/Infinity 的数值?
最佳答案
使用map,所有不在字典中的键都会被替换为NaN。来自 pandas documentation在 map
When arg is a dictionary, values in Series that are not in the dictionary (as keys) are converted to NaN.
您可以尝试使用替换
:
dataset['X'].replace("5more" : 5})
您还可以添加inplace=True
来替换inplace:
dataset['X'].replace("5more" : 5}, inplace=True)
关于python - 如何使用python中的map将分类(由字母和数字组成)转换为数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52658375/