Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Creating two lists derived from an analysed dataframe

I am in the process of creating a function that can check multiple conditions of my dataset and then return two lists.

  • List 1: Items to perform X

  • List 2: Items to perform Y

    MEDevel.com: Open-source for Healthcare and Education

    Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

    Visit Medevel

Sample data:

   Product   Variable     Value
0      A       Sell       0.1
1      B       Keep      -1.2
2      C       Sell       0.3
3      D       Swap       0.1
4      E       Swap       0.1

Ideal outcome:

Sell; A, C 
Swap; 'Nothing to swap this week'

Error when running:

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

My code so far:

def weekly_forcasting(df):

    a = df['Swap or Sell']
    b = df['Current Value']

    if (a == 'Sell') and (b > 0).any():
        return ('Product_Label')
        if (a == 'Sell') and (b < 0).any():
          return 'Nothing to sell this week'
        if (a == 'Swap') and (b > 0).any():
          return 'Nothing to swap this week'
        if (a == 'Swap') and (b < 0).any():
          return ('Product_Label')

>Solution :

b = df['Current Value'] makes b a series as the error message suggests. So ideally you want to use .loc, for instance you probably want

selling_list = (
    df.loc[
        (df["Swap or Sell"]=="Sell") & (df["Current Value"]>0)
    ]["Product_Label"].tolist() if 
    len(df.loc[
        (df["Swap or Sell"]=="Sell") & (df["Current Value"]>0)
    ]["Product_Label"].tolist()) > 0 
    else "nothing to sell this week"
)

and similarly you could define swapping_list and do a return selling_list, swapping_list

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading