3
data = {'Cat':  ['A','A','B','B','B','B'],
        'L1': ['0','0','0','0','0','0'],
        'L2': ['0','0','0','0','0','0'],
        'L3': ['0','0','0','0','0','0'],
        }

df = pd.DataFrame (data, columns = ['Cat','L1','L2','L3'])

Where 'Cat' is B, I would like to replace values in L1, L2, L3 to nan or ''.

df[['L1','L2','L3']] = np.where(df[['Cat'] == 'B', '')

the above did not work for me as I don't have y. Any suggestion?

1 Answer 1

3
df.loc[df.Cat == "B", "L1":"L3"] = np.nan
print(df)

Prints:

  Cat   L1   L2   L3
0   A    0    0    0
1   A    0    0    0
2   B  NaN  NaN  NaN
3   B  NaN  NaN  NaN
4   B  NaN  NaN  NaN
5   B  NaN  NaN  NaN
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.