I want to create a Pandas dataframe with 2 columns and x number rows that contain random strings.
I have found code to generate a pandas dataframe with random ints and a random stringer generator. I still don’t see a clear path to create a pandas data frame with random strings.
Code for random int dataframe
import numpy as np import pandas as pd df = pd.DataFrame(np.random.randint(0,100,size=(100, 4)), columns=list('ABCD'))
Code for random strings
import string import random def id_generator(size=1500, chars=string.ascii_uppercase + string.digits): return ''.join(random.choice(chars) for _ in range(size)) id_generator()
I would like destiredDataframe.head() to output two columns of random text and x number of rows.
Advertisement
Answer
Try this:
num_rows = 10 data = np.array([id_generator() for i in range(2*num_rows)]).reshape(-1,2) pd.DataFrame(data)