随机化 2 个 CSV 文件



我想使用 python 作为 1 对 1 函数同时随机化两个 CSV 文件,即。

File1.csv.                  File2.csv
1.                                A
2.                                B
3.                                C
4.                                D
5.                                E

输出将是

File1.csv.                 File2.csv
4.                               D
1.                               A
3.                               C
5.                               E
2.                               B

由于csv文件是静态平面文件,因此不能直接打乱它们。您需要的是将这两个文件作为 pd 数据帧读取,将它们都随机播放,然后将它们写入 csvs。这是代码:

df1 = pd.read_csv('datafile1.csv')
df2 = pd.read_csv('datafile2.csv')
# reset the index by row numbers, so that both dataframe has identical index
df1.reset_index(inplace=True)
df2.reset_index(inplace=True)
#Shuffle the rows
df1 = df1.sample(frac=1) # frac says what fraction of rows shall be returned, 1 means return all rows. This will ensure that all rows are shuffled randomly 
df2 = df2.loc[df1.index] # Since I am using index of df1 to order df2, I am ensuring same order 
# Put back the original indes
df1.set_index('index',drop=True, inplace=True)
df2.set_index('index',drop=True, inplace=True)
# Write back to original files
df1.to_csv('datafile1.csv')
df2.to_csv('datafile2.csv')

尝试使用numpy.random.shuffle维基例如:

import numpy as np
letters = ["A","B","C","D","E"]
numbers = [1,2,3,4,5,6]
np.random.shuffle(letters)
print(letters)
np.random.shuffle(numbers)
print(numbers)

输出在这里:

['A', 'C', 'B', 'E', 'D']
[2, 6, 4, 1, 5, 3]

最新更新