Home > other >  Pandas to the same value as in the same column, random to one other numerical
Pandas to the same value as in the same column, random to one other numerical

Time:09-16

The same name, for example, in the title column in random assignment for worker or a doctor, a, how do I use python implementation?

CodePudding user response:

Import the random
The 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 title

CodePudding 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
reference 4 floor weixin_45903952 reply:
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)
thanks brother!
  • Related