[英]How to modify a pandas dataframe using broadcasting series on a subset of columns
給出下表:
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
列的原始值的方法?
使用reindex
,因為ser_to_broad
有未命中的列,則它將返回NaN
( NaN
+ 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
如果您想更新數據,我認為這可以做到:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.