Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

I have the data frame as below:

ID | Night_Shift | Day_Shift | Weekend_Shift | Morning_Shift

3       True         False         False          True

1       False        False         False          False

2       True         True          False          True 

I am trying to create a new column which should be True if there is at least 1 True in any of the Shift Column or False if all are False?

This is my expected Output:

ID | Night_Shift | Day_Shift | Weekend_Shift | Morning_Shift | New_Col

    3       True         False         True          True        True

    1       False        False         False          False       False

    2       True         False         False          False       True

1 Answer

0 votes
by (36.8k points)

import pandas as pd

data = {'col_1': [True, True, False, False], 'col_2': [True, False, True, False]}

df = pd.DataFrame.from_dict(data)

df['result'] = df['col_1'] | df['col_2']

print (df)

This is the code:

df['New_Col'] = df['Night_Shift'] | df['Day_Shift'] | df['Weekend_Shift'] | df['Morning_Shift']

If you are a beginner and want to know more about Python the do check out the python for data science course

Browse Categories

...