2

I have a dataframe df which looks like this

min max value
3 9 7
3 4 10
4 4 4
4 10 3

I want to create a new column df['accuracy'] which tells me the accuracy if the df['value'] is in between df['min'] and df['max'] such that the new dataframe looks like

min max value Accuracy
3 9 7 Accurate
3 4 10 Not Accurate
4 4 4 Accurate
4 10 3 Not Accurate

1 Answer 1

4

Use apply() method of pandas, refer link

def accurate(row):  
    if row['value'] >= row['min'] and row['value'] <= row['max']:
        return 'Accurate'
    return 'Not Accurate'

df['Accuracy'] = df.apply(lambda row: accurate(row), axis=1)
print(df)
Sign up to request clarification or add additional context in comments.

1 Comment

Worth mentioning that there's no need to use a lambda function here. Instead, just pass it like so: df.apply(accurate, axis=1)

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.