Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
4 views
in Data Science by (18.4k points)
edited by

I am new to python, I want to sum the certain scores and when I type the code normally, it works perfectly:

select_df = df.filter(like = 'ABC')

select_df = select_df.loc[(df.REF == '000')]

select_df.sum(axis=1)

but when I define a function and try it like this, it is not working:

def sum_score(a, b):

    select_df = df.filter(like = a)

    select_df = select_df.loc[(df.REF == b)]

    select_df.sum(axis=1)

sum_score('ABC', '000')

And when I type it as:

print(sum_score('ABC', '000'))

The output says NONE

1 Answer

0 votes
by (36.8k points)

You have to return your df from the function:

def sum_score(qu, vc):

    select_df = df.filter(like = qu)

    select_df = select_df.loc[(df.REF == vc)]

    select_df.sum(axis=1)

    return select_df # Return the df

Then call the function like this:

print(sum_score('ABC', 'V000'))

Improve your knowledge in data science from scratch using data scientist 

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...