Home > Software design >  Python - Panel data create indicator with if statement
Python - Panel data create indicator with if statement

Time:08-24

I am trying to create an indicator equal to 1 if my meeting_date variable matches my date variable, and zero otherwise. I am getting an error in my code that consists of the following: ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Please let me know what I am doing wrong! Here is my code:

    if crsp_12['meeting_date'] == crsp_12['date']:
        crsp_12['i_meeting_date_dayof'] == 1
    else:
        crsp_12['i_meeting_date_dayof'] == 0

CodePudding user response:

You should always avoid classical if/for constructs with pandas. Use vectorial code:

crsp_12['i_meeting_date_dayof'] = crsp_12['meeting_date'].eq(crsp_12['date']).astype(int)
  • Related