我目前正试图找到一种方法,以随机化在一个数据帧行的项目。我在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结构可以随机化行和列?
也许把二维数组放平然后洗牌?
编辑:我误解了这个问题,这个问题只是洗牌行而不是所有的表(对吧?)
我认为使用数据帧没有多大意义,因为列名变得毫无用处。所以你可以使用2D numpy数组:
如果你想保留数据帧:
这里有一个对行和列进行无序排列的函数:
希望这有帮助
基于@jrjc的答案,我已经发布了https://stackoverflow.com/a/44686455/5009287,它使用
np.apply_along_axis()
看看完整的答案,看看如何能与熊猫df整合。
相关问题 更多 >
编程相关推荐