giser_yugang
giser_yugang

Reputation: 6166

How to use .loc to set as other column values in pandas

For example, I have a dataframe:

    cond  value1  value2
0   True       1       1
1  False       3       5
2   True      34       2
3   True      23      23
4  False       4       2

I hope to replace value1 to value2*2 when cond=True. So I want the result is:

    cond  value1  value2
0   True       2       1
1  False       3       5
2   True       4       2
3   True      46      23
4  False       4       2

I can achieve it by follow code:

 def convert(x):
     if x.cond:
         x.value1= x.value2*2
     return x
 data = data.apply(lambda x: convert(x),axis=1)

I think it is so slow when data is big. I try it by .loc, but I don't know how to set value.

How can I achieve it by .loc or other simple ways? Thanks in advance.

Upvotes: 3

Views: 16178

Answers (3)

Space Impact
Space Impact

Reputation: 13255

Using np.where :

df['value1'] = np.where(df.cond,df.value2*2,df.value1)

print(df)
    cond  value1  value2
0   True       2       1
1  False       3       5
2   True       4       2
3   True      46      23
4  False       4       2

Upvotes: 3

cs95
cs95

Reputation: 402493

You can use where/mask:

df.value1 = df.value1.mask(df.cond, df.value2*2)
# Or,
# df.value1 = df.value1.where(~df.cond, df.value2*2)

print(df)
    cond  value1  value2
0   True       2       1
1  False       3       5
2   True       4       2
3   True      46      23
4  False       4       2

Upvotes: 5

jezrael
jezrael

Reputation: 862661

Create boolean mask and multiple only filtered rows:

mask = df.cond
df.loc[mask, 'value1'] =  df.loc[mask, 'value2'] * 2
print (df)
    cond  value1  value2
0   True       2       1
1  False       3       5
2   True       4       2
3   True      46      23
4  False       4       2

Upvotes: 5

Related Questions