[英]Randomly selecting rows where the columns should switch values in a DataFrame
Given a DataFrame like this: 给定一个像这样的DataFrame:
import pandas as pd
import numpy as np
df = pd.DataFrame({'A': np.ones(5), 'B': np.zeros(5), 'C': np.ones(5), 'D': np.zeros(5)})
I want to be able to randomly select a number of rows where the A and B value along with their corresponding C and D values - BUT NOT column E and F are - switched so that the B column value is put in the A column and vice versa. 我希望能够随机选择一些行,其中A和B值以及它们对应的C和D值 - 但不是E和F列 - 被切换,以便B列值被放入A列和副反之亦然。
So it's not the whole row, but only certain columns that should switch (A, B, C , D) while E and F keep their values. 所以它不是整行,而是只有某些列应该切换(A,B,C,D),而E和F保持它们的值。
Does anybody have any ideas on how to accomplish this? 有没有人对如何做到这一点有任何想法?
I think there definitely could be a more efficient way than taking copies of the Series
here: 我认为肯定比在这里复制Series
更有效:
import pandas as pd
import numpy as np
df = pd.DataFrame({'A': np.ones(5), 'B': np.zeros(5), 'C': np.ones(5), 'D': np.zeros(5)})
rows_to_swap = np.random.choice(len(df), size=3, replace=False)
a_column = df['A'].copy()
b_column = df['B'].copy()
df.loc[rows_to_swap, 'A'] = b_column[rows_to_swap]
df.loc[rows_to_swap, 'B'] = a_column[rows_to_swap]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.