-3

Using the code below I need to place the values ("Right Data, Incorrect Data...) into a column "Assessment" when the if condition satisfies based on x and y values. But I am getting the below error

AttributeError: 'DataFrame' object has no attribute 'map'

def variable(x,y):
    x = df['Volume']
    y = df['Turnover']
    if df[(x==0) & (y==0)]:
        return 'Right Data'
    if df[(x>0) & (y>0)]:
        return 'Right Data'
    if df[(x<0) & (y<0)]:
        return 'Right Data'
    if df[(x>0) & (y<0)]:
        return 'Incorrect Data'
    if df[(x<0) & (y>0)]:
        return 'Incorrect Data'
    if df[(x!=0) & (y==0)]:
        return 'Incorrect Data'
    if df[(x==0) & (y!=0)]:
        return 'Incorrect Data'
    if df[(x==0) & (y.isnull())]:
        return 'Missing Data'
    if df[(x=='Nan') & (y!=0)]:
        return 'Missing Data'
test = df[['Volume','Turnover']]
test2 = test.map(variable)
df['Assessment'] = test2
dennlinger
  • 9,890
  • 1
  • 42
  • 63
Harsha
  • 11
  • 2
  • use `test.applymap(variable)` https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.applymap.html – Piotrek Aug 01 '18 at 08:54
  • it is giving below TypeError: ("variable() missing 1 required positional argument: 'y'", 'occurred at index Volume') – Harsha Aug 01 '18 at 08:59
  • yes I did the way you suggested I got the above mentioned error after doing that, I am not sure how to call both x and y using the map function? – Harsha Aug 01 '18 at 09:02
  • have a look here: https://stackoverflow.com/questions/12182744/python-pandas-apply-a-function-with-arguments-to-a-series – Piotrek Aug 01 '18 at 09:04
  • if it is only one column variable X then we can do it the way you suggested, but in this case I need to check 2 columns simultaneously and then change the values in new column when both columns satisfies the condition – Harsha Aug 01 '18 at 09:15
  • yup, i know, see the answer below – Piotrek Aug 01 '18 at 09:20

1 Answers1

0

You can try using lambda for this:

df['Assessment'] = df.apply(lambda x: variable(x['Volume'], x['Turnover']), axis=1)

Source:

python pandas- apply function with two arguments to columns

Piotrek
  • 1,400
  • 9
  • 16