Skip to content

Instantly share code, notes, and snippets.

@toby-p
Last active March 6, 2019 16:28
Show Gist options
  • Select an option

  • Save toby-p/57ff1ca26a75ca04f7b64f1246633257 to your computer and use it in GitHub Desktop.

Select an option

Save toby-p/57ff1ca26a75ca04f7b64f1246633257 to your computer and use it in GitHub Desktop.
Function to generate unique random strings; mainly for use as a temporary column name in a Pandas DataFrame.
import string
import random
def random_str(l=100):
"""Return a random string of length `l` to use as a temporary unique
variable name or Pandas DataFrame column name."""
s = random.choice(string.ascii_letters) # 1st letter = alpha so can be used as var name.
return s+"".join(random.choice(string.ascii_letters+string.digits) for i in range(l-1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment