[英]Shuffle Comma Separated Strings per Individual Row and Column (per field)
I have a dataframe with rows of values that have been concatenated, but separated by a comma.我有一个 dataframe 的值行已经连接,但用逗号分隔。
Row1 foo,bar,test,case第 1 行 foo ,bar,test,case
Row2 base,ball,basket,foot Row2垒、球、篮、脚
The goal is the shuffle/randomize each fields values, will retaining row order (do not shuffle columns, index must be kept) The hope is to return something like this:目标是随机化/随机化每个字段值,将保留行顺序(不要随机排列列,必须保留索引)希望返回如下内容:
Row1 test,foo,case,bar第 1 行测试,foo,case,bar
Row2 ball,foot,base,basket Row2球、脚、底、篮
SOLUTION FOUND:找到的解决方案:
Original_DF = # Our csv loaded data - the DF contains multiple columns of data attached to primary
data_list=[e for e in Original_DF['Data_List']] # each 'Data_List' field was one long string with a comma seperating words, we needed to make them a list
Shuff_DF=pd.DataFrame()
for i in range(len(data_list)):
myList=np.random.permutation(data_list[i].split(","))
myString = ",".join(myList)
Shuff_DF = Shuff_DF.append({'Data_List2': myString}, ignore_index=True)
Original_DF['Data_List2']=Shuff_DF['Data_List2'] # Append newly shuffled Lists to original df
You can use numpy.random.permutation to shuffle a list您可以使用 numpy.random.permutation 来打乱列表
https://docs.scipy.org/doc/numpy-1.14.0/reference/generated/numpy.random.permutation.html https://docs.scipy.org/doc/numpy-1.14.0/reference/generated/numpy.random.permutation.html
Example:例子:
import numpy.random
mydata = "foo,bar,baz,bat"
print(numpy.random.permutation(mydata.split(",")))
Another approach, using pandas functionality (sample):另一种方法,使用 pandas 功能(示例):
df = pd.DataFrame({"a": ["foo", "bar", "test", "case"],
"b": ["xoo", "xar", "xest", "xase"]})
for col in df.columns:
df[col] = df[col].sample(frac=1).values
Probably there is a more elegant approach avoiding the for loop, using apply.可能有一种更优雅的方法可以避免 for 循环,使用 apply。
PS: Alternatively, modifying the other answer from @Simon Crane: PS:或者,修改@Simon Crane的另一个答案:
df.apply(np.random.permutation, axis=0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.