gpt4 book ai didi

python - 混洗数据框中的列

转载 作者:行者123 更新时间:2023-12-04 16:24:37 26 4
gpt4 key购买 nike

我想无序排列列;完全伪随机,在一行代码上。
前:

  A B
0 1 2
1 1 2
后:
  B A
0 2 1
1 2 1
我到目前为止的尝试:
df = df.reindex(columns=columns)

df.sample(frac=1, axis=1)

df.apply(np.random.shuffle, axis=1)

最佳答案

您可以使用 np.random.default_rng()permutationseed使其可重现。

df = df[np.random.default_rng(seed=42).permutation(df.columns.values)]

关于python - 混洗数据框中的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67883198/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com