barciewicz
barciewicz

Reputation: 3783

Short way to replace values in a Series based on values in another Series?

I the below code, I am replacing all NaN values from column b with blank string if the corresponding value in column a is 1.

The code works, but I have to type df.loc[df.a == 1, 'b'] twice.

Is there a shorter/better way to do it?

    import pandas as pd

    df = pd.DataFrame({
        'a': [1, None, 3],
        'b': [None, 5, 6],
    })

    filtered = df.loc[df.a == 1, 'b']
    filtered.fillna('', inplace=True)
    df.loc[df.a == 1, 'b'] = filtered
    print(df)

Upvotes: 1

Views: 431

Answers (3)

Atendra Gautam
Atendra Gautam

Reputation: 475

Use where() to do it in one line

import numpy as np

df['b'] = np.where((df['b'].isnull()) & (df['a']==1),'',df['a'])

Upvotes: 1

MEdwin
MEdwin

Reputation: 2960

how about the use of numpy where clause to check values in a and b and replace? see a mockup below. I have used column 'c' to illustrate

import pandas as pd
import numpy as np
df = pd.DataFrame({
        'a': [1, None, 3],
        'b': [None, 5, 6],
    })
#replace b value if the corresponding value in column a is 1 and column b is NaN
df['c'] = np.where(((df['a'] == 1) & (df['b'].isna())), df['a'], df['b'])
df

original dataframe

     a  b
0   1.0 1.0
1   NaN 5.0
2   3.0 6.0

result:

      a b   c
0   1.0 NaN 1.0
1   NaN 5.0 5.0
2   3.0 6.0 6.0

Upvotes: 1

jezrael
jezrael

Reputation: 862751

Use Series.fillna only for matched values by condition:

df.loc[df.a == 1, 'b'] = df['b'].fillna('')

Upvotes: 0

Related Questions