0

Let's say I have main df:

 ID title grade1 grade2 "1111" "something" 1 2 "2222" "something1" 2 2 "3333" "something2" 1 1 "something3" "8865" 1 1 "4444" "something3" 1 2 "5555" "something4" 1 1 "6666" "something5" 2 2 "somethingX" "8888" 1 1 

And as u can see it's thera are mixed up values in columns 1 and 2

So what I do:

  1. put all wrong names to new df: temp = df[df['ID'].str.len() > 4]
  2. create list from new df: listToMove = temp['ID].unique()
  3. delete all records from df by for: df = df[df['ID'] != listToMove[i])
  4. change column plce in temp df
  5. append temp df to df.

But i think it's not the best practice. it's work fine... but that for... i think it's slow, and I can do it faster.

Any suggestion?

Result shoud be:

 ID title grade1 grade2 "1111" "something" 1 2 "2222" "something1" 2 2 "3333" "something2" 1 1 "8865" "something3" 1 1 "4444" "something3" 1 2 "5555" "something4" 1 1 "6666" "something5" 2 2 "8888" "somethingX" 1 1 

1 Answer 1

2

You can swap values by condition in DataFrame.loc:

m1 = df['ID'].str.len() != 4 df.loc[m1, ['ID', 'title']] = df.loc[m1, ['title', 'ID']].to_numpy() print (df) ID title grade1 grade2 0 1111 something 1 2 1 2222 something1 2 2 2 3333 something2 1 1 3 8865 something3 1 1 4 4444 something3 1 2 5 5555 something4 1 1 6 6666 something5 2 2 7 8888 somethingX 1 1 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.