Python 随机洗牌数据帧列表中的列
我有一个熊猫数据帧列表(称为final_list)(其中3个),每个数据帧有3列。单个数据帧如下所示Python 随机洗牌数据帧列表中的列,python,pandas,Python,Pandas,我有一个熊猫数据帧列表(称为final_list)(其中3个),每个数据帧有3列。单个数据帧如下所示 x y T/F 2 0 False 2 1 False 3 2 False 5 3 True 6 4 False 6 5 False 6 6 False 4 7 False 2 8 False 2 9 True 3 10 True index = 0 while counter < len(fi
x y T/F
2 0 False
2 1 False
3 2 False
5 3 True
6 4 False
6 5 False
6 6 False
4 7 False
2 8 False
2 9 True
3 10 True
index = 0
while counter < len(final_list):
random.shuffle(final_list[counter]['T/F'])
counter += 1
我想我应该使用random.shuffle循环遍历列表,并像这样随机排列“T/F”列
x y T/F
2 0 False
2 1 False
3 2 False
5 3 True
6 4 False
6 5 False
6 6 False
4 7 False
2 8 False
2 9 True
3 10 True
index = 0
while counter < len(final_list):
random.shuffle(final_list[counter]['T/F'])
counter += 1
index=0
当计数器
我的目标是让每个数据帧中的“T/F”列随机移动,这意味着每个数据帧中的“T/F”列应该不同,但是在运行代码后,每个数据帧中的“T/F”列与原始列不同,但它们彼此相同 IIUC,您需要:
将以下内容作为输入:
final_list=[df,df,df]
list_dfs=[i.assign(**{'T/F':np.random.choice(i['T/F'],len(i))}) for i in final_list]