python - 在数据框中混洗多列

标签 python pandas numpy scikit-learn sklearn-pandas

我有一个这样的数据框:

'a'                   'b'    'c'    'd'               'e'  'f'
'hello.text'           1      2      'hello2.text'     2   10
'hello3.text'          5      8      'hello4.text'     8   15

现在我需要随机排列“a”、“b”、“c”列。 像这样的事情:

'a'                   'b'    'c'    'd'               'e'  'f'
'hello3.text'          5      8      'hello2.text'     2   10
'hello.text'           1      2      'hello4.text'     8   15

我该怎么做?

最佳答案

使用 np.random.permutationDataFrame.apply用于单独处理每一列,因为不同类型的数据:

cols = ['a','b','c']

df[cols] = df[cols].apply(lambda x: np.random.permutation(x))
print (df)
               a  b  c              d  e   f
0   'hello.text'  5  2  'hello2.text'  2  10
1  'hello3.text'  1  8  'hello4.text'  8  15

关于python - 在数据框中混洗多列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57553046/

相关文章:

python 在使用 ssh 时找不到模块

python - 声子后端能力太窄

python - 无法删除 'Unmamed:' 行

python - 列的总和列表,即使不是全部

python - Pandas:取回存储在 DataFrame 中的原始 numpy 数组

python - 为什么我不能为整数规划设置 SciPy 的约束优化?

python - 在 Python 中,为什么 urllib.urlopen 使 Google 给出 http 状态 "302 Moved"?

python - Auth0 - 为什么范围不足以及如何处理?

python - groupby 操作后按月对数据框进行排序

python - 从 df.info() pandas 获取列名和空值计数