How to define a Python function that takes in input a DataFrame df a string str that is one of its column names, so that
def fun(x,col):
return x.loc[x.col == 0]
I know this is quite pleonastic but it is didactical. Is it possible to use variables for dataframe columns? (apparently not...)
The following code does not work
df = pandas.DataFrame({'Name': ...list of Irish people names..., 'Height':.... list of people's height)
x = 'Height'
I saw the solution to the question in here: Applying function to a DataFrame using its columns as parameters
which I liked a lot as a fan of lambda whatever... but it does not apply (at least I cannot see and please help me on this, possibly).
Would it be something like:
lambda x,col: x.loc[x.col==0], DataFrame, x.col)?
Thank you in advance
df["Height"]instead ofdf.Height- so it seems obviousx[col]forx=dfandcol="Height"