2

So this should be the easiest thing on earth. Pseudocode:

Replace column C with NaN if column E is NaN

I know I can do this by pulling out all dataframe rows where column E is NaN, replacing all of Column C, and then merging that on the original dataset, but that seems like a lot of work for a simple operation. Why doesn't this work:

Sample data:

dfz = pd.DataFrame({'A' : [1,0,0,1,0,0],
               'B' : [1,0,0,1,0,1],
               'C' : [1,0,0,1,3,1],
               'D' : [1,0,0,1,0,0],
               'E' : [22.0,15.0,None,10.,None,557.0]})

Replace Function:

def NaNfunc(dfz):
  if dfz['E'] == None:
    return None
  else:
    return dfz['C']

dfz['C'] = dfz.apply(NaNfunc, axis=1)

And how to do this in one line?

1 Answer 1

7

Use np.where:

In [34]:
dfz['C'] = np.where(dfz['E'].isnull(), dfz['E'], dfz['C'])
dfz

Out[34]:
   A  B   C  D    E
0  1  1   1  1   22
1  0  0   0  0   15
2  0  0 NaN  0  NaN
3  1  1   1  1   10
4  0  0 NaN  0  NaN
5  0  1   1  0  557

Or simply mask the df:

In [38]:
dfz.loc[dfz['E'].isnull(), 'C'] = dfz['E']
dfz

Out[38]:
   A  B   C  D    E
0  1  1   1  1   22
1  0  0   0  0   15
2  0  0 NaN  0  NaN
3  1  1   1  1   10
4  0  0 NaN  0  NaN
5  0  1   1  0  557
Sign up to request clarification or add additional context in comments.

1 Comment

Your solution worked for me but it's a little confusing naming the func param the same as your df, better to name it row or x to avoid ambiguity

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.