Home > Mobile >  Creating two lists derived from an analysed dataframe
Creating two lists derived from an analysed dataframe

Time:12-16

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

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')

CodePudding user response:

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

CodePudding user response:

Try using numpy.select

import numpy as np

# create a list of condtions
conditions = [
    (df['Swap or Sell'] == 'Sell') & (df['Current Value'] > 0),
    (df['Swap or Sell'] == 'Sell') & (df['Current Value'] < 0),
    (df['Swap or Sell'] == 'Swap') & (df['Current Value'] > 0),
    (df['Swap or Sell'] == 'Swap') & (df['Current Value'] < 0),
]
# create a list of chices that mtach a statisifed condition
choices = [
    df['Product_Label'],
    'There are no Sell recommendations today',
    'There are no Swap recommendations today',
    df['Product_Label'],
]

# create a new by using numpy.select
df['data'] = np.select(conditions, choices, 'Keep')

Then just access the info you want using boolean indexing

df.loc[df['Swap or Sell'] == 'Sell', 'data']
  • Related