首页 > 解决方案 > 使用 apply 更新 Dataframe 中的行不起作用

问题描述

我似乎无法从应用函数内部更新数据帧上的行

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)

标签: pythonpandas

解决方案


推荐阅读