1

How would I go through a dataframe and add a new column containing values based on whether the existing columns have the same values for each row?

For example in the following Dataframe I want to add a new column that contains 1 in the rows where Col1 and Col2 contain 1s and 0 if they do not all contain 1.

Col1 Col2
1    1
1    1
1    0 
0    0 
0    1
1    1
1    1

The output that I would want is

Col1 Col2 Col3
1    1    1
1    1    1
1    0    0 
0    0    0 
0    1    0
1    1    1
1    1    1

Ideally this would be scalable for more columns in the future (new column would only contain 1 if all columns contain 1)

2 Answers 2

1

if there are only 0 and 1 you try with Series.mul

df['Col3'] = df['Col1'].mul(df['Col2'])
Sign up to request clarification or add additional context in comments.

3 Comments

check now:) @ jezrael
Super, now working very nice for 2 columns fileld by 1 or 0
@ansev Creative solution! +1
1

If need check if all columns are 1 use DataFrame.all with casting to integers, working if data are only 1 and 0:

df['col3'] = df.all(axis=1).astype(int)

If need test only 1, working for any data use DataFrame.eq for ==:

df['col3'] = df.eq(1).all(axis=1).astype(int)

If want select columns for check add subset:

cols = ['Col1', 'Col2']

df['col3'] = df[cols].all(axis=1).astype(int)

Or:

df['col3'] = df[cols].eq(1).all(axis=1).astype(int)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.