The Python Oracle

Assign True/False condition based on existing columns

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Ocean Floor

--

Chapters
00:00 Question
01:15 Accepted answer (Score 4)
01:28 Answer 2 (Score 0)
02:02 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)