将一个 Pandas 数据帧除以另一个 - 忽略索引但尊重列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30327427/
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-13 23:22:13  来源:igfitidea点击:

Divide One Pandas Dataframe by Another - Ignore index but respect columns

pythonpandasdivide

提问by Dickster

I have 2 dataframes. I would like to broadcast a divide operation

我有 2 个数据框。我想广播一个除法操作

df1= pd.DataFrame([[1.,2.,3.,4.], [5.,6.,7.,8.], [9.,10.,11.,12.]],
                  columns=['A','B','C','D'], index=['x','y','z'])

df2= pd.DataFrame([[0.,1.,2.,3.]], columns=['A','B','D','C'], index=['q'])

Notice that the columns are aligned slightly differently in df2.

请注意,列在 df2 中的对齐方式略有不同。

I would like to divide df1 by df2 where the row is broadcast but the column labels are respected.

我想将 df1 除以 df2,其中广播行但尊重列标签。

   A   B   C   D
x  1   2   3   4
y  5   6   7   8
z  9  10  11  12


   A  B  D  C
q  0  1  2  3

This would be wrong.

这是错误的。

df1.values/df2.values

[[         inf   2.           1.5          1.33333333]
 [         inf   6.           3.5          2.66666667]
 [         inf  10.           5.5          4.        ]]

Answer I desire is:

我想要的答案是:

   A    B   C      D
x  inf  2   1      2
y  inf  6   2.33   4
z  inf  10  3.66   6

采纳答案by joris

If you divide by a Series (by selecting that one row of the second dataframe), pandas will align this series on the columns of the first dataframe, giving the desired result:

如果您除以一个系列(通过选择第二个数据帧的那一行),pandas 将在第一个数据帧的列上对齐这个系列,给出所需的结果:

In [75]: df1 / df2.loc['q']
Out[75]:
     A   B         C  D
x  inf   2  1.000000  2
y  inf   6  2.333333  4
z  inf  10  3.666667  6

If you don't know/want to use the name of that one row, you can use squeezeto convert the one-column dataframe to a series: df1 / df2.squeeze()(see answer of @EdChum).

如果您不知道/想使用该行的名称,您可以使用squeeze将一列数据df1 / df2.squeeze()框转换为一系列:(请参阅@EdChum 的回答)。

回答by Zero

May be, you could order your df2columns same of df1and then divide on values

可能是,您可以对df2列进行相同的排序df1,然后对值进行除法

In [53]: df1.values/df2[df1.columns].values
Out[53]:
array([[         inf,   2.        ,   1.        ,   2.        ],
       [         inf,   6.        ,   2.33333333,   4.        ],
       [         inf,  10.        ,   3.66666667,   6.        ]])

回答by EdChum

You can reorder the column and then call squeezeto flatten the array and then call div:

您可以对列重新排序,然后调用squeeze以展平数组,然后调用div

In [114]:

df1= pd.DataFrame( [[1.,2.,3.,4.],[5.,6.,7.,8.],[9.,10.,11.,12.]] ,columns = ['A','B','C','D'], index = ['x','y','z'])
df2= pd.DataFrame( [[0.,1.,2.,3.]] ,columns = ['A','B','D','C'], index = ['q'])    ?
df1.div(df2.ix[:,df1.columns].squeeze())

Out[114]:
     A   B         C  D
x  inf   2  1.000000  2
y  inf   6  2.333333  4
z  inf  10  3.666667  6

df1/df2.ix[:,df1.columns].squeeze()also works but @Joris's answer is much nicer

df1/df2.ix[:,df1.columns].squeeze()也有效,但@Joris 的回答要好得多

EDIT

编辑

As pointed out by @joris the column reordering is unnecessary as pandas will naturally align against the columns anyway so:

正如@joris 所指出的,列重新排序是不必要的,因为无论如何Pandas都会自然地与列对齐:

df1.div(df2squeeze())

or

或者

df1./df2squeeze()

would work

会工作