I have a dataframe df
composed by two columns x
and y
. I want to select half of the value of the dataframe and revert the valuese of x
and y
in this way.
df
x y
0 2557 7
1 2570 67
2 2564 27
3 2581 2578
4 2571 38
5 2565 11
6 2578 41
7 2577 44
8 2579 30
dftmp = df #save original data frame
indeces = np.arange(0, len(df)) # list of index
shuffle(indeces) # random shuffle
indeces = indeces[0: int(len(indeces) / 2) ] # take half of random indeces
df['x'][indeces] = dftmp['y'][indeces] # x new = y original
df['y'][indeces] = dftmp['x'][indeces] # y new = x original
I do not know why it changes only the value of y
df
x y
0 2557 2557
1 2570 2570
2 2564 2564
3 2581 2581
4 38 38
5 2565 11
6 41 41
7 2577 44
8 2579 30