vendredi 19 février 2021

How to replace values in a column randomly using values from a list on Pandas?

I have the following dataframe:

data = {'Names':['Antonio','Bianca','Chad','Damien','Edward','Frances','George'],'Sport':['Basketball','Placeholder','Football','Placeholder','Tennis','Placeholder','Placeholder']}

df = pd.DataFrame(data, columns = ['Names','Sport'])

I want to replace the value 'Placeholder' randomly with any value from the following list:

extra_sports = ['Football','Basketball','Tennis','Rowing']

The final outcome should look something like this whereby the value 'Placeholder' is now gone and replaced randomly with values from the list:

data = {'Names':['Antonio','Bianca','Chad','Damien','Edward','Frances','George'],'Sport':['Basketball','Tennis','Football','Rowing','Tennis','Football','Tennis']}

df = pd.DataFrame(data, columns = ['Names','Sport'])

And if possible how would I implement random.seed so that I can reproduce the results.




Aucun commentaire:

Enregistrer un commentaire