Skip to main content
8 events
when toggle format what by license comment
Apr 2, 2020 at 7:21 comment added PythonWizard For me, df['color'] = ['red' if x == 'Z' else 'green' for x in df['Set']] is working.
Feb 10, 2020 at 1:51 comment added AMC @cheekybastard Or don't, since .iterrows() is notoriously sluggish and the DataFrame shouldn't be modified while iterating.
Sep 17, 2019 at 15:28 comment added Paul Rougieux Note this nice solution will not work if you need to take replacement values from another series in the data frame, such as df['color_type'] = np.where(df['Set']=='Z', 'green', df['Type'])
Jan 14, 2019 at 1:38 comment added cheekybastard Add iterrows to the dataframe, then you can access multiple columns via row: ['red' if (row['Set'] == 'Z') & (row['Type'] == 'B') else 'green' for index, row in in df.iterrows()]
Jan 1, 2019 at 6:42 comment added Matti Can the list comprehension method be used if the condition needs information from multiple columns? I am looking for something like this (this does not work): df['color'] = ['red' if (x['Set'] == 'Z') & (x['Type'] == 'B') else 'green' for x in df]
Aug 16, 2017 at 16:49 history edited stackoverflowuser2010 CC BY-SA 3.0
added 20 characters in body
Apr 20, 2017 at 16:45 comment added boot-scootin Note that, with much larger dataframes (think pd.DataFrame({'Type':list('ABBC')*100000, 'Set':list('ZZXY')*100000})-size), numpy.where outpaces map, but the list comprehension is king (about 50% faster than numpy.where).
Jul 2, 2015 at 0:27 history answered cheekybastard CC BY-SA 3.0