Pandas: Randomize Letters In A Column
I have a data frame that looks like this: id1 | id2 ---------------------------- ab51c-ee-1a | cga--=%abd21 I am looking to randomize the letters only: id1 |
Solution 1:
Lets use apply
, with the power of str.replace
to replace only alphabets using regex i.e
import string
import random
letters = list(string.ascii_lowercase)
defrand(stri):
return random.choice(letters)
df.apply(lambda x : x.str.replace('[a-z]',rand))
Output :
id1 id2 0 gp51e-id-1v jvj--=%glw21
For one specific column use
df['id1'].str.replace('[a-z]',rand)
Added by @antonvbr: For future reference, if we want to change upper and lower case we could do this:
letters = dict(u=list(string.ascii_uppercase),l=list(string.ascii_lowercase))
(df['id1'].str.replace('[a-z]',lambda x: random.choice(letters['l']))
.str.replace('[A-Z]',lambda x: random.choice(letters['u'])))
Solution 2:
How about this:
import pandas as pd
from string import ascii_lowercase as al
import random
df = pd.DataFrame({'id1': ['ab51c-ee-1a'],
'id2': ['cga--=%abd21']})
al = list(al)
df = df.applymap(lambda x: ''.join([random.choice(al) if i in al else i for i inlist(x)]))
Post a Comment for "Pandas: Randomize Letters In A Column"