How to fill pandas dataframe columns with random dictionary values
Yep, you're right. You can use np.random.choice
with map
:
df
Year Approved
0 2016 Yes
1 2016 Yes
2 2017 No
df['Continent'] = np.random.choice(list(countries), len(df))
df['Country'] = df['Continent'].map(countries)
df
Year Approved Continent Country
0 2016 Yes Africa Ghana
1 2016 Yes Asia Vietnam
2 2017 No Europe Lithuania
You choose len(df)
number of keys at random from the country
key-list, and then use the country
dictionary as a mapper to find the country equivalents of the previously picked keys.