Skip to content
Advertisement

Setting column to true/false based on comparison of two other columns in pandas?

I have the following dataframe and I want to compare column value and predicted, if they match then I want to set the value of a column “provided” to False. I’m having difficulty doing this.

Here’s my data:

    ticker  periodDate  value   predicted
0   ibm     2017    150079.080  150079.080
1   ibm     2016    49799.140   49799.140
2   ibm     2015    459.016     45949.016

I want a new column to just have a True/False if value and predicted match. I tried this but to no avail:

def provideOrPredicted(df):
  if df['value'] == df['predicted']:
    df['provided'] = False
  elif df['value'] != df['predicted']:
    df['provided'] = False
  print(df)

provideOrPredicted(MergedDF)

I get this error:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Advertisement

Answer

Basically, below line will check each row and boolean result will be assigned into the new column of provided as:

 df['provided'] = df['value'] == df['predicted']
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement