我有一个数据框架。df‘’Skill‘=python,sql,java。现在,对于每个字符串,我想添加随机元素(高、低、中)。例如:df‘’Skill‘=python:high,sql:low,java:medium。
我试过一个代码,但它在字符串的末尾添加了“低”、“高”、“中”的分数。
有人能建议一下我该怎么做吗?
score=['low','medium','high']
df[Skill']=df['Skill'].apply(lambda x:[x + ": " + "".join(w for w in random.choice(score))])输出:
['python, java, sql: medium']但我想要的是python: low, java: high, sql: medium
发布于 2021-08-11 11:17:21
请检查以下内容:
df['Skill']= df['Skill'].apply(lambda x:','.join([y+': '+random.choice(score) for y in x.split(',')]))发布于 2021-08-11 11:16:07
试试这个:
import random
skill = ['python','java', 'sql', 'html']
score=['low','medium','high']
select_score = list()
for i in range(len(skill)):
select_score.append(random.choice(score))
select_score
freq_s = (dict(zip(skill, select_score)))
freq_s输出:
{'python': 'medium', 'java': 'low', 'sql': 'medium', 'html': 'low'}https://stackoverflow.com/questions/68740830
复制相似问题