[Python] Apply function to each cell in DataFrame

Discussão em 'Python' iniciado por Stack, Agosto 27, 2025.

  1. Stack

    Stack Membro Participativo

    I have a dataframe that may look like this:

    A B C
    foo bar foo bar
    bar foo foo bar


    I want to look through every element of each row (or every element of each column) and apply the following function to get the subsequent dataframe:

    def foo_bar(x):
    return x.replace('foo', 'wow')


    After applying the function, my dataframe will look like this:

    A B C
    wow bar wow bar
    bar wow wow bar


    Is there a simple one-liner that can apply a function to each cell?

    This is a simplistic example so there may be an easier way to execute this specific example other than applying a function, but what I am really asking about is how to apply a function in every cell within a dataframe.

    Continue reading...

Compartilhe esta Página