Bruce Jinru Su
Bruce Jinru Su

Reputation: 189

Python Dataframe - apply function on selected rows and columns

I have a dataframe df which has two columns A and B. Some rows have NaN for A. I want to apply a function to rows with NaN in A based on the value in column B.

I tried something like:

df.loc[df['A'].isnull(), 'A']=df['B'].apply(lambda x: func(x))

I know it won't work but can't figure out the right way.

Upvotes: 4

Views: 9036

Answers (2)

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210842

IIUC you can do it this way:

df.loc[df['A'].isnull(), 'A'] = df.loc[df['A'].isnull(), 'B'].map(func)

Upvotes: 3

Allen Qin
Allen Qin

Reputation: 19947

#This should work. 
df['A'] = df.apply(lambda x: func(x.B) if np.isnan(x.A) else x.A, axis=1)

Setup

df=pd.DataFrame({'A': {0: 1.0, 1: 1.0, 2: np.nan, 3: np.nan, 4: np.nan, 5: 1.0, 6: 1.0, 7: 1.0},
 'B': {0: 1, 1: 1, 2: 1, 3: 20, 4: 20, 5: 300, 6: 300, 7: 20}})

Out[765]: 
     A    B
0  1.0    1
1  1.0    1
2  NaN    1
3  NaN   20
4  NaN   20
5  1.0  300
6  1.0  300
7  1.0   20

def func(x):
    return x*x

Solution

#check d.A for nan inside the lambda can call the function only when d.A is nan.
df['A'] = df.apply(lambda x: func(x.B) if np.isnan(x.A) else x.A, axis=1)

df
Out[769]: 
       A    B
0    1.0    1
1    1.0    1
2    1.0    1
3  400.0   20
4  400.0   20
5    1.0  300
6    1.0  300
7    1.0   20

Upvotes: 1

Related Questions