1

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
emax
  • 6,965
  • 19
  • 74
  • 141

1 Answers1

3

Here's a shorter way of doing that:

df.update(df.sample(frac=0.5).rename(columns={'y': 'x', 'x': 'y'}))

df
Out: 
        x       y
0  2557.0     7.0
1  2570.0    67.0
2  2564.0    27.0
3  2578.0  2581.0
4    38.0  2571.0
5  2565.0    11.0
6    41.0  2578.0
7  2577.0    44.0
8    30.0  2579.0
ayhan
  • 70,170
  • 20
  • 182
  • 203