0

I want to replace the values of the rows of a specific column with one name, but I want to skip the rows that have no value.

enter image description here

So only the row with text in it should get a different name.

I tried this: df.loc[:, 'Groenlinks'] = 'gl' But with this all the rows change to 'gl'. I'm not sure how I can only replace the names of different rows with one word without typing all the words it should replace.

I want it to look like this:

VVD          PVV          Groenlinks
vvd                       gl
             pvv          
vvd                    
                    
vvd                       gl
vvd          pvv          

2 Answers 2

2

You can use:

df.loc[df['Groenlinks'] != '', 'Groenlinks'] = 'gl'

Empty string is not equivalent to NaN / null in Pandas. So, if you have empty string in other blank rows in the column, you can use the code above to filter the rows to set up with the new value.

Sign up to request clarification or add additional context in comments.

Comments

2

If the values in the column are an empty string or they are null values, then this should work perfectly:

df['Groenlinks'].loc[df['Groenlinks'].notnull() | (df['Groenlinks'] != '')] = 'gl'

Comments

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.