pandas 如何按行随机打乱pandas数据帧

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47923429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 04:56:14  来源:igfitidea点击:

How to shuffle a pandas dataframe randomly by row

pythonpandasnumpyshuffle

提问by Joey

I am trying to shuffle a pandas dataframe by row instead of column.

我正在尝试按行而不是按列对 Pandas 数据框进行洗牌。

I have the following dataframe:

我有以下数据框:

   row1    row2    row3
1    3      1       6
2    5      2       7
3    7      3       8 
4    9      4       9

And would like to shuffle the df to achieve a random permutation such as:

并想对 df 进行 shuffle 以实现随机排列,例如:

   row1    row2    row3
1    6      3       1
2    3      9       2
3    7      5       8 
4    4      9       7

I tried:

我试过:

df1 = df.reindex(np.random.permutation(df.index))

however, this permutes only by column and not row.

但是,这仅按列而不是按行排列。

回答by Jan Zeiseweis

You can achieve this by using the sample method and apply it to axis # 1. This will shuffle the elements in a row:

您可以通过使用示例方法并将其应用于轴 # 1 来实现此目的。这将连续洗牌元素:

df = df.sample(frac=1, axis=1).reset_index(drop=True)

How ever your desired dataframe looks completely randomised, which can be done by shuffling by row and then by column:

您想要的数据框看起来如何完全随机,这可以通过按行然后按列混洗来完成:

df = df.sample(frac=1, axis=1).sample(frac=1).reset_index(drop=True)

Edit:

编辑:

import numpy as np
df = df.apply(np.random.permutation, axis=1)