在pandas中随机化/洗牌数据帧中的行

2024-09-25 16:19:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正试图找到一种方法,以随机化在一个数据帧行的项目。我在pandas(shuffling/permutating a DataFrame in pandas)的shuffling/permutation列中找到了这个线程,但是就我的目的而言,有没有一种方法可以像

import pandas as pd

data = {'day': ['Mon', 'Tues', 'Wed', 'Thurs', 'Fri'],
       'color': ['Blue', 'Red', 'Green', 'Yellow', 'Black'],
       'Number': [11, 8, 10, 15, 11]}

dataframe = pd.DataFrame(data)
    Number   color    day
0      11    Blue    Mon
1       8     Red   Tues
2      10   Green    Wed
3      15  Yellow  Thurs
4      11   Black    Fri

把这些行随机分成

    Number   color    day
0      Mon    Blue    11
1      Red    Tues     8
2      10     Wed    Green
3      15    Yellow  Thurs
4      Black   11     Fri

如果要这样做,列标题必须消失或类似的东西,我理解。

编辑:所以,在我发布的线程中,部分代码引用了一个“axis”参数。我知道axis=0表示列,axis=1表示行。我试着获取代码并将轴更改为1,只有当表包含所有数字(而不是字符串列表或两者的组合)时,数据帧才会随机化。

也就是说,我应该考虑不使用数据帧吗?如果我的数据只由字符串或int和string的组合组成,是否有更好的2D结构可以随机化行和列?


Tags: 数据numberpandasgreenblueredcolorblack
3条回答

也许把二维数组放平然后洗牌?

In [21]: data2=dataframe.values.flatten()

In [22]: np.random.shuffle(data2)

In [23]: dataframe2=pd.DataFrame (data2.reshape(dataframe.shape), columns=dataframe.columns )

In [24]: dataframe2
Out[24]: 
  Number   color    day
0   Tues  Yellow     11
1    Red   Green    Wed
2  Thurs     Mon   Blue
3     15       8  Black
4    Fri      11     10

编辑:我误解了这个问题,这个问题只是洗牌行而不是所有的表(对吧?)

我认为使用数据帧没有多大意义,因为列名变得毫无用处。所以你可以使用2D numpy数组:

In [1]: A
Out[1]: 
array([[11, 'Blue', 'Mon'],
       [8, 'Red', 'Tues'],
       [10, 'Green', 'Wed'],
       [15, 'Yellow', 'Thurs'],
       [11, 'Black', 'Fri']], dtype=object)

In [2]: _ = [np.random.shuffle(i) for i in A] # shuffle in-place, so return None

In [3]: A
Out[3]: 
array([['Mon', 11, 'Blue'],
       [8, 'Tues', 'Red'],
       ['Wed', 10, 'Green'],
       ['Thurs', 15, 'Yellow'],
       [11, 'Black', 'Fri']], dtype=object)

如果你想保留数据帧:

In [4]: pd.DataFrame(A, columns=data.columns)
Out[4]: 
  Number  color     day
0    Mon     11    Blue
1      8   Tues     Red
2    Wed     10   Green
3  Thurs     15  Yellow
4     11  Black     Fri

这里有一个对行和列进行无序排列的函数:

import numpy as np
import pandas as pd

def shuffle(df):
    col = df.columns
    val = df.values
    shape = val.shape
    val_flat = val.flatten()
    np.random.shuffle(val_flat)
    return pd.DataFrame(val_flat.reshape(shape),columns=col)

In [2]: data
Out[2]: 
   Number   color    day
0      11    Blue    Mon
1       8     Red   Tues
2      10   Green    Wed
3      15  Yellow  Thurs
4      11   Black    Fri

In [3]: shuffle(data)
Out[3]: 
  Number  color     day
0    Fri    Wed  Yellow
1  Thurs  Black     Red
2  Green   Blue      11
3     11      8      10
4    Mon   Tues      15

希望这有帮助

基于@jrjc的答案,我已经发布了https://stackoverflow.com/a/44686455/5009287,它使用np.apply_along_axis()

a = np.array([[10, 11, 12], [20, 21, 22], [30, 31, 32],[40, 41, 42]])
print(a)
[[10 11 12]
 [20 21 22]
 [30 31 32]
 [40 41 42]]

print(np.apply_along_axis(np.random.permutation, 1, a))
[[11 12 10]
 [22 21 20]
 [31 30 32]
 [40 41 42]]

看看完整的答案,看看如何能与熊猫df整合。

相关问题 更多 >