Pandas从两个数据帧计算百分比

时间:2018-04-23 06:46:06

标签: python pandas

我有两个数据帧:

A   B   C   D           
X   Y   1.0 49.453125
        2.0 67.767857
        3.0 48.770833
        4.0 43.583333


A   E   F   G   C   H
X   Z   1.0 807 1.0 34.375000
        2.0 808 1.0 35.375000
        1.0 909 2.0 1.750000
        2.0 910 2.0 48.750000

现在,如果两个数据帧的C列相同,我想计算数据帧2中H列的相对百分比和D列数据帧1的对应值:

34.375000 * 100 / 49.453125 = 69.51
35.375000 * 100 / 49.453125 = 71.53
 1.750000 * 100 / 67.767857 = 2.58
48.750000 * 100 / 67.767857 = 71.94

我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:1)

如果df2['C']中存在df1['C']的所有值,然后map之后存在多个mul,则使用div

mapping = df1.set_index('C')['D']
s = df2['H'].mul(100).div(df2['C'].map(mapping))
print (s)
0    69.510269
1    71.532385
2     2.582345
3    71.936759
dtype: float64

另一个解决方案是使用merge与新DataFrame的内部联接:

df = df1.merge(df2, on='C')
s = df['H'].mul(100).div(df['D'])
print (s)
0    69.510269
1    71.532385
2     2.582345
3    71.936759
dtype: float64