用python划分两个数据框 [英] Divide two dataframes with python

查看:116
本文介绍了用python划分两个数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数据框:df1df2

df1:

TIMESTAMP           eq1 eq2 eq3
2016-05-10 13:20:00  40  30  10
2016-05-10 13:40:00  40  10  20

df2:

TIMESTAMP           eq1 eq2 eq3
2016-05-10 13:20:00  10  20  30
2016-05-10 13:40:00  10  20  20

我想将df1除以df2:将df1的每一列除以df2的所有列以得到此结果df3:

I would like to divide df1 by df2 : each column of df1 by all column of df2 to get this result df3 :

TIMESTAMP           eq1        eq2        eq3
2016-05-10 13:20:00  40/(10+10) 30/(20+20) 10/(30+20)
2016-05-10 13:40:00  40/(10+10) 10/(20+20) 20/(30+20)

请问有什么主意吗?

推荐答案

您可以使用TIMESTAMP中的"nofollow"> set_index :

You can use div, but before set_index from both columns TIMESTAMP:

df1.set_index('TIMESTAMP', inplace=True)
df2.set_index('TIMESTAMP', inplace=True)

print (df1.div(df2).reset_index())
            TIMESTAMP  eq1  eq2       eq3
0 2016-05-10 13:20:00  4.0  1.5  0.333333
1 2016-05-10 13:40:00  4.0  0.5  1.000000

通过评论

df1.set_index('TIMESTAMP', inplace=True)
df2.set_index('TIMESTAMP', inplace=True)
print (df2.sum())
eq1    20
eq2    40
eq3    50
dtype: int64

print (df1.div(df2.sum()).reset_index())
            TIMESTAMP  eq1   eq2  eq3
0 2016-05-10 13:20:00  2.0  0.75  0.2
1 2016-05-10 13:40:00  2.0  0.25  0.4

这篇关于用python划分两个数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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