在具有外部条件的数据框上逐元素

问题描述 投票:0回答:2

我有两个具有相同形状和列的DataFrame。我想根据以下条件为第一个DataFrame应用映射:如果x

x = pd.DataFrame({'a' : [0.9, 8.1, 4.3], 'b' : [1.9, 1.4, 2.5], 'c' : [2.9, 1.2, 2.3]})
y = pd.DataFrame({'a' : [1,   2,   3  ], 'b' : [2,   1,   5],    'c' : [2,   0,   3]})

作为输出,我想接收以下DataFrame:

z = pd.DataFrame({'a' : [1,   8,   4  ], 'b' : [2,   1,   3],    'c' : [2,   1,   3]})

请帮助我,我该怎么做...

python pandas
2个回答
0
投票

您可以在这里使用np.where

np.where

x[:] = np.where(x<y, np.ceil(x), np.floor(x))

0
投票

您可以进行print(x) a b c 0 1.0 2.0 2.0 1 8.0 1.0 1.0 2 4.0 3.0 3.0

np.where
© www.soinside.com 2019 - 2024. All rights reserved.