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.