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

giser_yugang

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.

jezrael

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Pandas how to set column to NaN based on values in other columns using .loc

Pandas: how to set values in a dataframe specified by a loc (index and column) creating rows if index position doesn't exist?

How to use UPDATE SET without making other column values NULL

Slice pandas dataframe using .loc with both index values and multiple column values, then set values

How to use .loc to flag a new column with groupby pandas

pandas: How to set the values of a column with incremental values?

Set values of a column in pandas dataframe based on values in other columns

How to set a column by slicing values of other columns

how to use previous row value as well as values in other column in same row to compute value of a column in pandas

set multiple column values for multiple rows with loc

How to map the values based on other column in pandas?

How to update values of a column according to the values of a column of the other datafreme with pandas?

How to replace values in a pandas multiindex column set

How to create index column for set of values in other column

How to sum a column over unique values in two other column with Pandas?

How to add a column to a pandas DataFrame based on other column values?

How to create a new column with .size() values of other column in pandas?

How To Iterate column values over the other column by pandas?

How to sort values in Pandas based on values in other column?

How to use column values as headers in Pandas DataFrame

iterate over pandas rows and set column values based on values in other column

How to add values to a column between 2 set values in pandas

How do I use the Pandas .loc() function when converting a column to DateTime?

Python and pandas: How to use df.loc to make a new column based on conditions?

How to set values of a column based on multiple conditions in other columns in python?

How to use .loc with groupby and two conditions in pandas

How to use loc in pandas dataframe slicing?

Using .loc with a column of values

How to average certain values of a column based on other columns condition in pandas

TOP Ranking

HotTag

Archive