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
JavaScript
x
4
1
import numpy as np
2
import pandas as pd
3
df = pd.DataFrame(np.random.randint(0,100,size=(100, 4)), columns=list('ABCD'))
4
Code for random strings
JavaScript
1
7
1
import string
2
import random
3
def id_generator(size=1500, chars=string.ascii_uppercase + string.digits):
4
return ''.join(random.choice(chars) for _ in range(size))
5
6
id_generator()
7
I would like destiredDataframe.head() to output two columns of random text and x number of rows.
Advertisement
Answer
Try this:
JavaScript
1
5
1
num_rows = 10
2
3
data = np.array([id_generator() for i in range(2*num_rows)]).reshape(-1,2)
4
pd.DataFrame(data)
5