0

I appear to be unable to update rows on a dataframe from inside an apply function

import pandas as pd

df = pd.DataFrame({'filename':['test0.dat', 'test2.dat'], 
                   'm':[12, 13], 
                   'n':[None, None]})

data = {'filename':'test2.dat', 'n':16}

def update_vals(row, data=data):
    if row.filename == data['filename']:
        row.n = data['n']
    return row

df.apply(update_vals, axis=1)
  • problem exists in pandas-0.22.0 and pandas-0.24.1 – user495732 Why Me Feb 08 '19 at 22:04
  • 1
    Modifications to `row` will not affect the `pd.DataFrame`. The `.apply` method will pass a new `pd.Series` object that doesn't share an underlying buffer with the original `pd.DataFrame` object to the function you use. This isn't a problem, this is how it is designed to work. Anyway, you should definitely not be using `.apply` here to begin with. – juanpa.arrivillaga Feb 08 '19 at 22:05
  • 2
    [I do not recommend the use of `apply`.](https://stackoverflow.com/questions/54432583/when-should-i-ever-want-to-use-pandas-apply-in-my-code) – cs95 Feb 08 '19 at 22:13

0 Answers0