0

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?

reese565
  • 1
  • 1
  • Use `at` instead of `loc`. Don't forget to convert col3 to object since it will be float because it was initialized to `np.nan`. Alternatively you could initialize with pd.NA like `df['col3'] = pd.NA` which is already object – Henry Ecker Sep 03 '21 at 22:15
  • Or with a comprehension instead of initializing and a looping separately. `df['col3'] = [np.random.choice(letters, np.random.choice(range(26))) for _ in df.index]` – Henry Ecker Sep 03 '21 at 22:18

0 Answers0