如何使用Pandas上列表中的值随机替换列中的值?

2024-04-19 18:41:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下数据帧:

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

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

我想用以下列表中的任意值随机替换值“占位符”:

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

最终结果应该是这样的,即值“占位符”现在消失,并随机替换为列表中的值:

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

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

如果可能的话,我将如何实现random.seed,以便复制结果


1条回答
网友
1楼 · 发布于 2024-04-19 18:41:16

我相信您只需要将值Placeholder替换为list,对于list的长度,请使用sum的布尔值True来替换受益数组的正确长度:

extra_sports = ['Football','Basketball','Tennis','Rowing']
   
np.random.seed(1) 
m = df['Sport'].eq('Placeholder')
df.loc[m, 'Sport'] = np.random.choice(extra_sports, size=m.sum())
print (df)
     Names       Sport
0  Antonio  Basketball
1   Bianca  Basketball
2     Chad    Football
3   Damien      Rowing
4   Edward      Tennis
5  Frances    Football
6   George    Football

相关问题 更多 >