mercredi 22 juillet 2020

Replace strings in a pandas column with a randomly generated code, and store the matchings in a dictionary

I have a dataframe with a column containing strings that I am trying to replace with a randomly generated string, and keep a dictionary with the originals and the replacements.

Concretely, I have something like this:

  col1                       
0  Marie  
1  Marie  
2  Lucas 
3  Dog  
4  Table
5  Dog  

And I want to replace those strings with a code. The format of the code is indifferent, but, for example, with à 6 characters only letters code, the output would look like this:

  col1                       
0  aadfre
1  aadfre
2  qwerty
3  lfkdjs
4  hgyeoy
5  lfkdjs

And I am trying to keep a dictionary of the matching, like this: {'Marie': 'aadfre', 'Lucas': 'qwerty', 'Dog ': 'lfkdjs', 'Table': 'hgyeoy'}

Is there any way to do this?

Thanks!!




Aucun commentaire:

Enregistrer un commentaire