How to update values in a specific row in a Python Pandas DataFrame?

后端 未结 4 1336
猫巷女王i
猫巷女王i 2020-12-02 12:09

With the nice indexing methods in Pandas I have no problems extracting data in various ways. On the other hand I am still confused about how to change data in an existing Da

4条回答
  •  执念已碎
    2020-12-02 12:48

    If you have one large dataframe and only a few update values I would use apply like this:

    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)
    

提交回复
热议问题