pandas 用python划分两个数据帧

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39454542/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 01:59:45  来源:igfitidea点击:

Divide two dataframes with python

pythonpandasdataframemultiple-columnsdivision

提问by Poisson

I have two dataframes : df1and df2

我有两个数据框:df1df2

df1:

df1

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

df2:

df2

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

I would like to divide df1by df2: each column of df1by all column of df2to get this result df3:

我想分裂df1df2:每列df1的所有列df2来得到这样的结果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)

Any idea please?

请问有什么想法吗?

回答by jezrael

You can use div, but before set_indexfrom both columns TIMESTAMP:

您可以使用div, 但在set_index两列之前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

EDIT by comment:

通过评论编辑:

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

回答by Alexander

This should work if TIMESTAMPis not the index:

如果TIMESTAMP不是索引,这应该有效:

>>> df1.set_index('TIMESTAMP').div(df2.set_index('TIMESTAMP').sum()) 
                     eq1   eq2  eq3
TIMESTAMP                          
2016-05-10 13:20:00    2  0.75  0.2
2016-05-10 13:40:00    2  0.25  0.4

If TIMESTAMPis the index, then simply this:

如果TIMESTAMP是索引,那么简单地是这样的:

df1.div(df2.sum())