Home > Blockchain >  Python fill out multiple columns with random values from a dictionary
Python fill out multiple columns with random values from a dictionary

Time:10-01

I'm trying to fill multiple columns of a dataframe with random values from a dictionary. From a another post I understood that you could specify a list and have a column filled with random values from that list like this:

Dataframe:

   Col1  Col2 Col3
1  NaN   NaN  values
2  NaN   NaN  .
3  NaN   NaN  .
my_list = ['a', 'b', 'c', 'd']
df['Col1'] = np.random.choice(my_list, len(df))

The code would then fill the column like this:

   Col1  Col2 Col3
1  b     NaN  values
2  d     NaN  .
3  a     NaN  .

What I want is to fill out multiple columns and while the first thought would be to use something ugly like this:

my_list1 = ['a', 'b', 'c', 'd']
my_list2 = ['k', 'l', 'e', 'f']
df['Col1'] = np.random.choice(my_list1, len(df))
df['Col2'] = np.random.choice(my_list2, len(df))

I would like to declare a dictionary of lists and somehow call a function that maps the random values to their respective columns:

my_dict = {'Col1': ['a', 'b', 'c', 'd'],
           'Col2': ['k', 'l', 'e', 'f']}
df = <insert function to fill columns>

And then the dataframe would end up looking like this:

   Col1  Col2 Col3
1  b     l    values
2  d     f    .
3  c     k    .

Note that I would only want to fill out a certain amount of columns in my dataframe and not all of them

CodePudding user response:

This should get you where you need to go:

for k, v in my_dict.items():
    df[k] = np.random.choice(v, len(df))

CodePudding user response:

@vtasca's answers is perfectly fine. Because you asked about other ways of doing this though, here's a fun way using dictionary comprehensions. It's a loop hiding behind some makeup:

chosen = {k: np.random.choice(v, len(df)) for k, v in my_dict.items()}
df = pd.concat([df, pd.DataFrame(chosen)], axis=1)
  • Related