CodePudding user response:
Import the randomThe import pandas as pd
S=pd. DataFrame ([[' zhang ', ' '], [' bill ', ' '], [' Cathy ', ' ']], the columns=[' name ', 'title'])
Print (s)
S [' title ']=[' title '] s map (lambda x: the random choice ([" doctor ", "workers", 'students']))
Print (s)
CodePudding user response:
But you have the same name will randomly to different titles, I want to name the same titleCodePudding user response:
import pandas as pd
Df=pd DataFrame ([[' zhang ', ' '], [' bill ', ' '], [' Cathy ', ' '], [' zhang ', ' '], [' Cathy ', ' ']], the columns=[' name ', 'title'])
L=[" doctor ", "workers", 'students',' civil servants']
Df [' title ']=df [' name ']. The map (lambda x: l [int (x.e ncode (" utf-8 "), hex (), 16) % 4])
Print (df)
CodePudding user response:
That is better to do,import random
The import pandas as pd
S=pd. DataFrame ([[' zhang ', ' '], [' bill ', ' '], [' Cathy ', ' '], [' zhang ', ' ']], the columns=[' name ', 'title'])
Print (s)
Names=list (the set (s) [r]. 'name' tolist ()))
Print (names)
Dict1=dict (zip (names, the list (random. Choice ([" doctor ", "workers", 'students']) for _ in names)))
Print (dict1)
S [' title ']=[' name '] s map (lambda x: dict1 [x])
Print (s)
CodePudding user response:
The