2024-06-25 22:48:34 发布
网友
我有一个pandas数据框,其中一列有航空公司名称(或公司名称)。 我想通过将名称的一小部分(只在一列中)更改为相似但不相同的名称来生成“混乱”数据集。 所以联合航空公司会变成联合航空公司的说法。 下面是我的数据集的一个例子
Description 0 United Airlines 1 Pinnacle Airlines Inc. 2 Ryanair 3 British Airways
有没有任何方法可以将每行的刺的变化随机应用到熊猫数据框中。 有人有什么想法吗?在
您可以使用numpy.random.choice返回索引的随机选择,它接受一个一维数组并返回一个与您传递的大小相同的随机选择:
numpy.random.choice
In [177]: rand_indices = np.random.choice(df.index, 2) rand_indices.sort() rand_indices Out[177]: array([1, 2], dtype=int64) In [178]: df.loc[rand_indices] Out[178]: Description a 1 Pinnacle Airlines Inc. 1 2 Ryanair 2 In [179]: def scramble_text(df, index, col): df.loc[index, col] = df[col].str.upper() scramble_text(df, rand_indices, 'Description') df Out[179]: Description a 0 United Airlines 0 1 PINNACLE AIRLINES INC. 1 2 RYANAIR 2 3 British Airways 3
您可以使用
numpy.random.choice
返回索引的随机选择,它接受一个一维数组并返回一个与您传递的大小相同的随机选择:相关问题 更多 >
编程相关推荐