Skip to content Skip to sidebar Skip to footer

Pandas: Update Column Values From Another Column If Criteria

I have a DataFrame: A B 1: 0 1 2: 0 0 3: 1 1 4: 0 1 5: 1 0 I want to update each item column A of the DataFrame with values of column B if value from column A equals 0. DataF

Solution 1:

df['A'] = df.apply(lambda x: x['B'] if x['A']==0 else x['A'], axis=1)

Output

    A  B
1:  1  12:  0  03:  1  14:  1  15:  1  0

Solution 2:

Use where

In [348]: df.A = np.where(df.A.eq(0), df.B, df.A)

In [349]: df
Out[349]:
    A  B
1:  1  12:  0  03:  1  14:  1  15:  1  0

Solution 3:

You can perform this by using a mask:

df = pd.DataFrame()
df['A'] = [0,0,1,0,1]
df['B'] = [1,0,1,1,0]
mask = (df.A == 0)
df.loc[mask,'A'] = df.loc[mask,'B']

    A   B
0   1   1
1   0   0
2   1   1
3   1   1
4   1   0

EDIT: Ok this is actually a unefficient solution:

%timeit df.loc[mask,'A'] = df.loc[mask,'B']
%timeit df.apply(lambda x: x['B'] ifx['A']==0elsex['A'], axis=1)
%timeit np.where(df.A.eq(0), df.B, df.A)

5.52 ms ± 556 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
1.27 ms ± 167 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
796 µs ± 89.2 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

So thanks to zero for this efficient solution with np.where!

Post a Comment for "Pandas: Update Column Values From Another Column If Criteria"