Search code examples
pythonpandasdataframenumpyapply

How can I use the apply() function for a single column?


I have a pandas dataframe with multiple columns. I want to change the values of the only the first column without affecting the other columns. How can I do that using apply() in pandas?


Solution

  • Given a sample dataframe df as:

       a  b
    0  1  2
    1  2  3
    2  3  4
    3  4  5
    

    what you want is:

    df['a'] = df['a'].apply(lambda x: x + 1)
    

    that returns:

       a  b
    0  2  2
    1  3  3
    2  4  4
    3  5  5