1

I want to change is the name column is NaN, I want to add a string 'P' if there is a dash in column symbol.

      name    symbol
0     NaN     Bom
1     John    Madam-T
2     Marry   Madam
3     NaN     Madam-T 
4     NaN     Bom-T
5     NaN     Marry-Y

The desired outcome is

      name    symbol
0     NaN     Bom
1     John    Madam-T
2     Marry   Madam
3     NaN     Madam-PT 
4     NaN     Bom-PT
5     NaN     Marry-PY

Index 3 to 5 will add a string of P as it contains - But I only want it to occurred if the condition of the column name is NaN. df = df['symbol'].str.replace('-', '-P') replace all columns that contains - df = df['name'].isnull call the NaN in column name however, I unable to combine both.

1 Answer 1

1

Use DataFrame.loc for select and set values filterd by mask:

m = df['name'].isnull()
df.loc[m, 'symbol'] = df.loc[m, 'symbol'].str.replace('-', '-P')
print (df)
    name    symbol
0    NaN       Bom
1   John   Madam-T
2  Marry     Madam
3    NaN  Madam-PT
4    NaN    Bom-PT
5    NaN  Marry-PY
Sign up to request clarification or add additional context in comments.

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.