【问题标题】:numpy/pandas - why the selected the element from list are the same by random.choicenumpy/pandas - 为什么从列表中选择的元素与 random.choice 相同
【发布时间】:2022-12-20 16:59:24
【问题描述】:

有一个包含整数值的列表。 列表=[1,2,3,.......] 然后我使用 np.random.choice 函数选择一个随机元素并将其添加到现有的数据框列中,请参考下面的代码

df.message = df.message.astype(str) + "rowNumber=" + '"' + str(np.random.choice(list))  + '"'

但是 np.random.choice 选择并附加到消息列的元素对于所有消息行始终相同。

这里有什么问题?

预期结果是从列表中选择的元素不相同。

【问题讨论】:

    标签: pandas numpy


    【解决方案1】:

    将整数列表转换为字符串列表并使用参数size传递给np.random.choice

    L1 = list(map(str, L))
    df.message = df.message.astype(str) + "rowNumber=" + '"' + np.random.choice(L1, size=len(df))  + '"'
    

    【讨论】:

      猜你喜欢
      • 2014-05-17
      • 1970-01-01
      • 2017-04-16
      • 1970-01-01
      • 1970-01-01
      • 2020-10-23
      • 2020-03-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多