The Python Oracle

Assign True/False condition based on existing columns

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game Looping

--

Chapters
00:00 Assign True/False Condition Based On Existing Columns
01:01 Accepted Answer Score 5
01:12 Answer 2 Score 0
01:35 Thank you

--

Full question
https://stackoverflow.com/questions/6188...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #pandas

#avk47



ACCEPTED ANSWER

Score 5


Just do any

df.loc[:,'col2':].any(1)
0     True
1    False
2     True
3     True
dtype: bool

#df['col10']=df.loc[:,'col2':].any(1)



ANSWER 2

Score 0


You have done two wrong things here. One is missed iterating through rows and second is involved col1 in the expression. Here's what I've tried in a similar way that you've tried.

df['col10'] = False
for index, row in df.iterrows():
    if row['col2'] or row['col3'] or row['col4'] or row['col5'] or row['col6'] or row['col7'] or row['col8'] or row['col9']:
        df.iloc[index,9] = True
    else:
        df.iloc[index,9] = False

A single line solution to the question would be:

df['col10'] = df.loc[:,'col2':].any(1)