[英]How can I replace words in a panda data frame with a number?
I'm using python to analyse survey questions.我正在使用 python 来分析调查问题。
I have a large panda dataframe of questions (columns) and each question has a different possible answer eg 'agree' 'neutral' 'disagree' & there are around 1800 responses.我有一个大熊猫问题数据框(列),每个问题都有不同的可能答案,例如“同意”“中立”“不同意”,大约有 1800 个回复。
I want to replace each word with a numerical value eg agree = 1, neutral = 2 and disagree = 3.我想用一个数值替换每个单词,例如同意 = 1,中性 = 2,不同意 = 3。
NB the data type is object not string.注意数据类型是对象而不是字符串。
What is the most efficient way to do this?最有效的方法是什么?
You could do something with the dictionary to replace你可以用字典做一些事情来替换
responses = {'key1': 'value1', 'key2': 'value2'}
df.replace(responses, regex=True)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.