简体   繁体   English

每次在Python中运行代码时,如何以相同的顺序随机化pandas列?

[英]How do I randomize a pandas column in the same order every time I run the code in Python?

This is my code: 这是我的代码:

random_idx = np.random.permutation(len(cIds))
train_Ids = cIds[random_idx[:train_size]]

Now, I want the list to be randomized in the same order every time I run this line of code. 现在,我希望每次运行这行代码时,列表都以相同的顺序随机化。

Note: I do not want to save random_idx variable in a text file, and fetch the same list. 注意:我不想在文本文件中保存random_idx变量,并获取相同的列表。

You can use seed in order to tell numpy to generate the same random numbers: 您可以使用seed来告诉numpy生成相同的随机数:

np.random.seed(seed=1234)
random_idx = np.random.permutation(len(cIds))

same as: 如同:

np.random.seed(1234)
random_idx = np.random.permutation(len(cIds))

Or 要么

random_idx = np.random.RandomState(seed=1234).permutation(len(cIds)

seed: Must be convertible to 32 bit unsigned integers` 种子:必须可以转换为32位无符号整数

Or you can do in in pandas style: 或者你可以用熊猫风格做:

cIds.sample(n=train_size, 
            replace=False, 
            random_state=your_favorite_number_here)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 每次我运行python程序时如何添加新列 - how to add a new column every time i run a python program 如何格式化代码以每次都以相同的宽度打印? - How do I format code to print with the same width every time? 如何在 Python 中每 24 小时调用一次函数? 我目前正在使用线程同时运行 Flask 服务器和函数 - How do I call a function every 24 hours in Python? I'm currently using Threading to run a Flask server and the function at the same time 每次运行python代码时都必须导入excel文件吗? - Do I have to import excel file every time when I run the python code? 如何同时在 Python 中运行两个不同的代码? - How can I run two different code in Python at the same time? 每当我在Python中按下相同的按钮时,如何依次更改龟图像? - How can I change turtle images in order every time I press the same button in Python? 如何每天使用调度程序同时运行python脚本? - How do I run python script everyday at the same time with scheduler? 如何防止每次运行脚本时显示 Python 之禅? - How do I prevent The Zen of Python from displaying every time I run a script? 如何在带有小时列的Pandas数据框中“丰富”每条记录? - How do I “enrich” every record in a Pandas dataframe with an hour column? 如何根据python中的pandas数据框中的列按降序分组? (Jupyter 笔记本) - How do I group by based on a column in pandas data frame in python and in descending order? (Jupyter Notebook)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM