Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep values of between two columns based on third column in pandas

Tags:

python

pandas

I have three columns, A, B and C. I want to create a fourth column D that contains values of A or B, based on the value of C. For example:

   A   B   C   D 
0  1   2   1   1
1  2   3   0   3
2  3   4   0   4
3  4   5   1   4

In the above example, column D takes the value of column A if the value of C is 1 and the value of column B if the value of C is 0. Is there an elegant way to do it in Pandas? Thank you for your help.

like image 251
user6566438 Avatar asked Apr 05 '17 10:04

user6566438


People also ask

How do you get the value of a column based on another column Pandas?

You can extract a column of pandas DataFrame based on another value by using the DataFrame. query() method. The query() is used to query the columns of a DataFrame with a boolean expression. The blow example returns a Courses column where the Fee column value matches with 25000.

How do you get between values in Pandas?

Pandas between() method is used on series to check which values lie between first and second argument. inclusive: A Boolean value which is True by default. If False, it excludes the two passed arguments while checking.

How replace value in Pandas based on multiple conditions?

You can replace values of all or selected columns based on the condition of pandas DataFrame by using DataFrame. loc[ ] property. The loc[] is used to access a group of rows and columns by label(s) or a boolean array. It can access and can also manipulate the values of pandas DataFrame.


2 Answers

Use numpy.where:

In [20]: df
Out[20]:
   A  B  C
0  1  2  1
1  2  3  0
2  3  4  0
3  4  5  1

In [21]: df['D'] = np.where(df.C, df.A, df.B)

In [22]: df
Out[22]:
   A  B  C  D
0  1  2  1  1
1  2  3  0  3
2  3  4  0  4
3  4  5  1  4
like image 167
juanpa.arrivillaga Avatar answered Sep 25 '22 20:09

juanpa.arrivillaga


pandas
In consideration of the OP's request

Is there an elegant way to do it in Pandas?

my opinion of elegance
and idiomatic pure pandas
assign + pd.Series.where

df.assign(D=df.A.where(df.C, df.B))

   A  B  C  D
0  1  2  1  1
1  2  3  0  3
2  3  4  0  4
3  4  5  1  4

response to comment

how would you modify the pandas answer if instead of 0, 1 in column C you had A, B?

df.assign(D=df.lookup(df.index, df.C))

   A  B  C  D
0  1  2  A  1
1  2  3  B  3
2  3  4  B  4
3  4  5  A  4
like image 36
piRSquared Avatar answered Sep 25 '22 20:09

piRSquared