How to populate pandas data columns with random dictionary values

I am new to Pandas and I would like to play with random text data. I am trying to add 2 new columns to a DataFrame df that will be populated with the key (newcol1) + value (newcol2) randomly selected from the dictionary.

countries = {'Africa':'Ghana','Europe':'France','Europe':'Greece','Asia':'Vietnam','Europe':'Lithuania'}

My df already has 2 columns, and I would like something like this:

    Year Approved Continent    Country
0   2016      Yes    Africa      Ghana
1   2016      Yes    Europe  Lithuania
2   2017       No    Europe     Greece

I can, of course, use a for or while loop to populate df ['Continent'] and df ['Country'], but I feel .apply () and np.random.choice can provide a simpler and more convenient solution for this.

+4
source share
2 answers

Yes you are right. You can use np.random.choice+ 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

len(df) country, country , .


pd.Series.replace :

df['Country'] = df.Continent.replace(countries)

df

    Year Approved Continent    Country
0   2016      Yes    Africa      Ghana
1   2016      Yes      Asia    Vietnam
2   2017       No    Europe  Lithuania

apply + dict.get:

df['Country'] = df.Continent.apply(countries.get)

df

    Year Approved Continent    Country
0   2016      Yes    Africa      Ghana
1   2016      Yes      Asia    Vietnam
2   2017       No    Europe  Lithuania
+5

DataFrame.sample():

df.join(
    pd.DataFrame(list(countries.items()), columns=["continent", "country"])
    .sample(len(df), replace=True)
    .reset_index(drop=True)
)

, .


Python 3.6, random.choices():

df.join(
    pd.DataFrame(choices([*countries.items()], k=len(df)), columns=["continent", "country"])
)

random.choices() numpy.random.choice(), , , numpy.random.choice() 1-D .

0

Source: https://habr.com/ru/post/1689757/


All Articles