【发布时间】:2019-02-11 19:42:08
【问题描述】:
给定下表:
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
我想通过在将修改(更新)表的列子集(one 和three)上广播熊猫系列来应用操作。所以..
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
有没有一种方法可以使用广播方法来保留列 two 和 four 的原始值?
【问题讨论】:
-
所以你想更新
data? -
是的,想法是通过操作改变表
标签: python pandas dataframe broadcasting