I'm tying to iterate through the index of a pandas dataframe and set each cell value to some a list of variable length.
import numpy as np
import pandas as pd
import string
letters = list(string.ascii_lowercase)
df = pd.DataFrame({'col1': range(10),
'col2': letters[:10]})
df['col3'] = np.nan
for i in df.index:
sample_size = np.random.choice(range(26))
df.loc[i,'col3'] = np.random.choice(letters, sample_size).tolist()
However, I keep getting the following error:
ValueError: Must have equal len keys and value when setting with an iterable
What is this error and how can I get around it?