划分两个pandas DataFrames并保留非数字列 [英] Divide two pandas DataFrames and keep non-numeric columns

查看:46
本文介绍了划分两个pandas DataFrames并保留非数字列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个包含数值和非数值值的pandas DataFrame.我想一个除以另一个,但保留非数字列.这是MWE:

I have two pandas DataFrames that contain numeric and non-numeric values. I want to divide one by the other, but keep the non-numeric columns. Here is a MWE:

a = pd.DataFrame(
    [
        ['group1', 1., 2.],
        ['group1', 3., 4.],
        ['group1', 5., 6.]
    ], 
    columns=['Group', 'A', 'B']
)

b = pd.DataFrame(
    [
        ['group1', 7., 8.],
        ['group1', 9., 10.],
        ['group1', 11., 12.]
    ],
    columns=['Group', 'A', 'B']
)

尝试做:

b.div(a)

结果:

TypeError: unsupported operand type(s) for /: 'str' and 'str'

为了解决这个问题,我做了:

So to get around this, I have done:

result = b.drop(["Group"], axis=1).div(a.drop(["Group"], axis=1))
print(result)
#     A    B
#0  7.0  4.0
#1  3.0  2.5
#2  2.2  2.0

这是正确的,但我也想保留列"Group".

Which is correct, but I also wanted to keep the column "Group".

获得我想要的输出的一种方法是:

One way to get my desired output would be to do:

desired_output = b[["Group"]].join(result)
print(desired_output)
#    Group    A    B
#0  group1  7.0  4.0
#1  group1  3.0  2.5
#2  group1  2.2  2.0

但是我真正的DataFrames有很多非数字列.是否有一种更清洁/更快/更有效的方法来告诉熊猫只对数字列进行除法?

But my real DataFrames have many non-numeric columns. Is there a cleaner/faster/more efficient way to tell pandas to divide only the numeric columns?

推荐答案

您可以使用np.divide,将掩码传递给where参数.

You can use np.divide, passing a mask to the where parameter.

np.divide(b, a, where=a.dtypes.ne(object))

假设数据框中的非数字列相同,请使用combine_first/fillna将其取回:

Assuming the non-numeric columns are the same across DataFrames, use combine_first/fillna to get them back:

np.divide(b, a, where=a.dtypes.ne(object)).combine_first(a)


    Group    A    B
0  group1  7.0  4.0
1  group1  3.0  2.5
2  group1  2.2  2.0

这篇关于划分两个pandas DataFrames并保留非数字列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆