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
4
  • dftmp = df.copy() Commented Jul 27, 2017 at 16:45
  • It would be of great help if you share your dataframe with us using for instance: df.to_dict() Commented Jul 27, 2017 at 16:47
  • @asongtoruin why? Commented Jul 27, 2017 at 16:49
  • @emax A quick search gave this: stackoverflow.com/questions/27673231/… Commented Jul 27, 2017 at 16:56

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

2 Comments

this is very clever!
@MaxU Thank you. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.