IF no pandas returning The Truth value of a Series is ambiguous. Use a.Empty, a.bool(), a.item(), a.any() or a.all()

Asked

Viewed 22 times

0

I have a DS with two columns, one with a month from 1 to 12, and another with a year from 2015 to 2021, and I’m trying to create a column called Fiscal Year.

I am creating a function so that when the month is between 4 and 12 within the year 2017 return FY17/18.

def fiscal_year(x,y):
    if ((x >= 4.0) & (x <= 12.0)) & (y == 2017.0):
        return 'FY 17/18'
    else:
        return 'nada'

And when will I apply:

instal_final_csa['Fiscal Year'] = instal_final_csa.apply(fiscal_year(instal_final_csa['MÊS'],instal_final_csa['ANO']))

Returns the error:

Valueerror: The Truth value of a Series is ambiguous. Use a.Empty, a. bool(), a.item(), a.any() or a.all().

  • Try: instal_final_csa['Fiscal Year'] = instal_final_csa.apply(fiscal_year(instal_final_csa['MÊS'],instal_final_csa['ANO']), axis=1). Note that I added the axis=1

  • I tried, but returned the same error.

1 answer

0

I was able to do by np select.

instal_final_csa2 =instal_final_csa
conditions = [((instal_final_csa2['MÊS'] >= 4 )&(instal_final_csa2['MÊS'] <= 12))&(instal_final_csa2['ANO'] == 2016),
              ((instal_final_csa2['MÊS'] >= 1 )&(instal_final_csa2['MÊS'] <= 3))&(instal_final_csa2['ANO'] == 2017),
              ((instal_final_csa2['MÊS'] >= 4 )&(instal_final_csa2['MÊS'] <= 12))&(instal_final_csa2['ANO'] == 2017),
              ((instal_final_csa2['MÊS'] >= 1 )&(instal_final_csa2['MÊS'] <= 3))&(instal_final_csa2['ANO'] == 2018),
              ((instal_final_csa2['MÊS'] >= 4 )&(instal_final_csa2['MÊS'] <= 12))&(instal_final_csa2['ANO'] == 2018),
              ((instal_final_csa2['MÊS'] >= 1 )&(instal_final_csa2['MÊS'] <= 3))&(instal_final_csa2['ANO'] == 2019),
              ((instal_final_csa2['MÊS'] >= 4 )&(instal_final_csa2['MÊS'] <= 12))&(instal_final_csa2['ANO'] == 2019),
              ((instal_final_csa2['MÊS'] >= 1 )&(instal_final_csa2['MÊS'] <= 3))&(instal_final_csa2['ANO'] == 2020),
              ((instal_final_csa2['MÊS'] >= 4 )&(instal_final_csa2['MÊS'] <= 12))&(instal_final_csa2['ANO'] == 2020),
              ((instal_final_csa2['MÊS'] >= 1 )&(instal_final_csa2['MÊS'] <= 3))&(instal_final_csa2['ANO'] == 2021),
              ((instal_final_csa2['MÊS'] >= 4 )&(instal_final_csa2['MÊS'] <= 12))&(instal_final_csa2['ANO'] == 2021),]
choices = ['FY16/17','FY16/17','FY17/18','FY17/18','FY18/19','FY18/19','FY19/20','FY19/20','FY20/21','FY20/21','FY21/22']
instal_final_csa['Fiscal Year'] = np.select(conditions,choices,default = 'nada')

Browser other questions tagged

You are not signed in. Login or sign up in order to post.