1

I'm writing a function which has 3 parameters: country_metric(df, country, column)

this is there[parameters] individual function.

df - dataframe

country - a string (which can be assumed to be an entry which will be found in a column called location)

column - a string (which can be assumed to be a column (other than location) which will be found in df

The function should return the value from the row for the given country and the column labelled as per the second string:

My function does not work as I do not know how to phrase the function properly. this is is what I have so far:

def country_metric(df, country,column):
    for column in df:
        if df["location"] == country:
            return df[df["location"] == country]["column"]

I can't seem to find anything on stack about referencing columns within datasets whilst defining your own function. I tried using .items() as suggested by python and then it printed. and I'm struggling from here onwards. AttributeError: 'DataFrame' object has no attribute 'item'

any help would be appreciated.

1 Answer 1

1

Use DataFrame.loc with filter by column name, output is one eleemnt Series if only one country in location column, Series with 2 or more values if duplciated country or empty Series if not match country:

def country_metric(df, country,column):
    return df.loc[df["location"] == country, column]

If need first match value with no error if no matching country use iter with next trick:

def country_metric(df, country,column):
    return iter(next(df.loc[df["location"] == country, column]), 'no match')
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.