-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
6
  • use test.applymap(variable) pandas.pydata.org/pandas-docs/stable/generated/… Commented Aug 1, 2018 at 8:54
  • it is giving below TypeError: ("variable() missing 1 required positional argument: 'y'", 'occurred at index Volume') Commented Aug 1, 2018 at 8: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? Commented Aug 1, 2018 at 9:02
  • have a look here: stackoverflow.com/questions/12182744/… Commented Aug 1, 2018 at 9: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 Commented Aug 1, 2018 at 9:15

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.