[Python] Python definition of a function which accepts any dataframe and any of the first...

Discussão em 'Python' iniciado por Stack, Fevereiro 28, 2025.

  1. Stack

    Stack Membro Participativo

    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

    Continue reading...

Compartilhe esta Página