1

df is like this,

     A    B    C
0  NaN  150 -150
1  100  NaN  150
2 -100 -150  NaN
3 -100 -150  NaN
4  NaN  150  150
5  100  NaN -150

Another array is array([1, 2, 3])

I want to replace non-null value in each column with each value in array, and the result will be,

      A     B     C
0   NaN   2     3
1   1     NaN   3
2   1     2     NaN
3   1     2     NaN
4   NaN   2     3
5   1     NaN   3

How can I achieve this in a simple way? I write something like,

df[df.notnull()] = np.array([1,2,3])
df[df.notnull()].loc[:,] = np.array([1,2,3])

but all cannot work.

1 Answer 1

1

How about:

>>> (df * 0 + 1) * arr
    A   B   C
0 NaN   2   3
1   1 NaN   3
2   1   2 NaN
3   1   2 NaN
4 NaN   2   3
5   1 NaN   3
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.