如何在列的子集上使用广播系列修改熊猫数据框

时间:2019-02-11 19:42:09

标签: python pandas dataframe broadcasting

给出下表:

 import numpy as np
 import pandas as pd
 data = pd.DataFrame(data = np.arange(16).reshape((4, 4)),
                     index = ['Chile', 'Argentina', 'Peru', 'Bolivia'],
                     columns = ['one', 'two', 'three', 'four'])


           one  two three  four
 Chile      0    1   2      3
 Argentina  4    5   6      7
 Peru       8    9   10     11
 Bolivia    12   13  14     15

我想通过在将修改(更新)表格的列的子集(onethree)上广播熊猫系列来应用一项操作。所以..

ser_to_broad = pd.Series([1, 2], index = ['one', 'three'])
data + ser_to_broad

           one  two three  four
Chile       1   NaN   4     NaN
Argentina   5   NaN   8     NaN    
Peru        9   NaN   12    NaN    
Bolivia     13  NaN   16    NaN 

是否存在一种使用广播方法保留列twofour的原始值的方法?

2 个答案:

答案 0 :(得分:1)

使用reindex,由于ser_to_broad中有未命中的列,因此它将返回NaNNaN + somevalue = NaN

data+ser_to_broad.reindex(data.columns,fill_value=0)
Out[106]: 
           one  two  three  four
Chile        1    1      4     3
Argentina    5    5      8     7
Peru         9    9     12    11
Bolivia     13   13     16    15

答案 1 :(得分:1)

如果您想更新数据,我认为可以这样做:

ser_to_broad = pd.Series([1, 2], index=['one', 'three'])
data[ser_to_broad.index] += ser_to_broad

print(data)

输出

           one  two  three  four
Chile        1    1      4     3
Argentina    5    5      8     7
Peru         9    9     12    11
Bolivia     13   13     16    15