热线电话:13121318867

登录
2019-02-12 阅读量: 770
如何用pandas里的广播修改数据帧

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)的子集上广播pandas系列来应用操作。所以..

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广播方法的方法?

解决办法:

如果你想更新数据,我认为这样做:

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

0.0000
2
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子