两个 DataFrame 中每一行和每一列之间的差异(Python / Pandas)

2024-02-25

是否有更有效的方法来比较一个 DF 中每一行中的每一列与另一个 DF 中每一行中的每一列?这对我来说很草率,但我的循环/应用尝试要慢得多。

df1 = pd.DataFrame({'a': np.random.randn(1000),
                   'b': [1, 2] * 500,
                   'c': np.random.randn(1000)},
                   index=pd.date_range('1/1/2000', periods=1000))
df2 = pd.DataFrame({'a': np.random.randn(100),
                'b': [2, 1] * 50,
                'c': np.random.randn(100)},
               index=pd.date_range('1/1/2000', periods=100))
df1 = df1.reset_index()
df1['embarrassingHackInd'] = 0
df1.set_index('embarrassingHackInd', inplace=True)
df1.rename(columns={'index':'origIndex'}, inplace=True)
df1['df1Date'] = df1.origIndex.astype(np.int64) // 10**9
df1['df2Date'] = 0
df2 = df2.reset_index()
df2['embarrassingHackInd'] = 0
df2.set_index('embarrassingHackInd', inplace=True)
df2.rename(columns={'index':'origIndex'}, inplace=True)
df2['df2Date'] = df2.origIndex.astype(np.int64) // 10**9
df2['df1Date'] = 0
timeit df3 = abs(df1-df2)

10 个循环,3 个循环中最好的:每个循环 60.6 毫秒

我需要知道进行了哪个比较,因此将每个相反索引丑陋地添加到比较 DF 中,以便它将最终出现在最终 DF 中。

预先感谢您的任何帮助。


您发布的代码显示了生成减法表的巧妙方法。然而,它并没有发挥 Pandas 的优势。 Pandas DataFrame 将底层数据存储在基于列的块中。因此,按列检索数据的速度最快,而不是按行检索。由于所有行都具有相同的索引,因此减法是按行执行的(将每一行与其他每一行配对),这意味着存在大量基于行的数据检索df1-df2。这对于 Pandas 来说并不理想,特别是当并非所有列都具有相同的数据类型时。

减法表是 NumPy 所擅长的:

In [5]: x = np.arange(10)

In [6]: y = np.arange(5)

In [7]: x[:, np.newaxis] - y
Out[7]: 
array([[ 0, -1, -2, -3, -4],
       [ 1,  0, -1, -2, -3],
       [ 2,  1,  0, -1, -2],
       [ 3,  2,  1,  0, -1],
       [ 4,  3,  2,  1,  0],
       [ 5,  4,  3,  2,  1],
       [ 6,  5,  4,  3,  2],
       [ 7,  6,  5,  4,  3],
       [ 8,  7,  6,  5,  4],
       [ 9,  8,  7,  6,  5]])

你可以想到x作为一列df1, and y作为一列df2。您将在下面看到 NumPy 可以处理df1和所有列df2以基本相同的方式,使用基本相同的语法。


下面的代码定义了orig and using_numpy. orig是您发布的代码,using_numpy是使用 NumPy 数组执行减法的另一种方法:

In [2]: %timeit orig(df1.copy(), df2.copy())
10 loops, best of 3: 96.1 ms per loop

In [3]: %timeit using_numpy(df1.copy(), df2.copy())
10 loops, best of 3: 19.9 ms per loop

import numpy as np
import pandas as pd
N = 100
df1 = pd.DataFrame({'a': np.random.randn(10*N),
                   'b': [1, 2] * 5*N,
                   'c': np.random.randn(10*N)},
                   index=pd.date_range('1/1/2000', periods=10*N))
df2 = pd.DataFrame({'a': np.random.randn(N),
                'b': [2, 1] * (N//2),
                'c': np.random.randn(N)},
               index=pd.date_range('1/1/2000', periods=N))

def orig(df1, df2):
    df1 = df1.reset_index() # 312 µs per loop
    df1['embarrassingHackInd'] = 0 # 75.2 µs per loop
    df1.set_index('embarrassingHackInd', inplace=True) # 526 µs per loop
    df1.rename(columns={'index':'origIndex'}, inplace=True) # 209 µs per loop
    df1['df1Date'] = df1.origIndex.astype(np.int64) // 10**9 # 23.1 µs per loop
    df1['df2Date'] = 0

    df2 = df2.reset_index()
    df2['embarrassingHackInd'] = 0
    df2.set_index('embarrassingHackInd', inplace=True)
    df2.rename(columns={'index':'origIndex'}, inplace=True)
    df2['df2Date'] = df2.origIndex.astype(np.int64) // 10**9
    df2['df1Date'] = 0
    df3 = abs(df1-df2) # 88.7 ms per loop  <-- this is the bottleneck
    return df3

def using_numpy(df1, df2):
    df1.index.name = 'origIndex'
    df2.index.name = 'origIndex'
    df1.reset_index(inplace=True) 
    df2.reset_index(inplace=True) 
    df1_date = df1['origIndex']
    df2_date = df2['origIndex']
    df1['origIndex'] = df1_date.astype(np.int64) 
    df2['origIndex'] = df2_date.astype(np.int64) 

    arr1 = df1.values
    arr2 = df2.values
    arr3 = np.abs(arr1[:,np.newaxis,:]-arr2) # 3.32 ms per loop vs 88.7 ms 
    arr3 = arr3.reshape(-1, 4)
    index = pd.MultiIndex.from_product(
        [df1_date, df2_date], names=['df1Date', 'df2Date'])
    result = pd.DataFrame(arr3, index=index, columns=df1.columns)
    # You could stop here, but the rest makes the result more similar to orig
    result.reset_index(inplace=True, drop=False)
    result['df1Date'] = result['df1Date'].astype(np.int64) // 10**9
    result['df2Date'] = result['df2Date'].astype(np.int64) // 10**9
    return result

def is_equal(expected, result):
    expected.reset_index(inplace=True, drop=True)
    result.reset_index(inplace=True, drop=True)

    # expected has dtypes 'O', while result has some float and int dtypes. 
    # Make all the dtypes float for a quick and dirty comparison check
    expected = expected.astype('float')
    result = result.astype('float')
    columns = ['a','b','c','origIndex','df1Date','df2Date']
    return expected[columns].equals(result[columns])

expected = orig(df1.copy(), df2.copy())
result = using_numpy(df1.copy(), df2.copy())
assert is_equal(expected, result)

How x[:, np.newaxis] - y works:

该表达式利用了 NumPy 广播。 要了解广播(通常是 NumPy),需要了解数组的形状:

In [6]: x.shape
Out[6]: (10,)

In [7]: x[:, np.newaxis].shape
Out[7]: (10, 1)

In [8]: y.shape
Out[8]: (5,)

The [:, np.newaxis]添加一个新轴x on the right,所以形状是(10, 1). So x[:, np.newaxis] - y是形状数组的减法(10, 1)具有形状数组(5,).

从表面上看,这没有意义,但是 NumPy 数组播送他们的形状按照一定的规则 http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html尝试使它们的形状兼容。

第一条规则是可以在left。所以一个形状数组(5,)可以广播自己来塑造(1, 5).

下一个规则是长度为 1 的轴可以将自身广播到任意长度。数组中的值只是根据需要沿额外维度重复。

所以当形状数组(10, 1) and (1, 5)放在一起进行 NumPy 算术运算,它们都被广播到形状数组(10, 5):

In [14]: broadcasted_x, broadcasted_y = np.broadcast_arrays(x[:, np.newaxis], y)

In [15]: broadcasted_x
Out[15]: 
array([[0, 0, 0, 0, 0],
       [1, 1, 1, 1, 1],
       [2, 2, 2, 2, 2],
       [3, 3, 3, 3, 3],
       [4, 4, 4, 4, 4],
       [5, 5, 5, 5, 5],
       [6, 6, 6, 6, 6],
       [7, 7, 7, 7, 7],
       [8, 8, 8, 8, 8],
       [9, 9, 9, 9, 9]])

In [16]: broadcasted_y
Out[16]: 
array([[0, 1, 2, 3, 4],
       [0, 1, 2, 3, 4],
       [0, 1, 2, 3, 4],
       [0, 1, 2, 3, 4],
       [0, 1, 2, 3, 4],
       [0, 1, 2, 3, 4],
       [0, 1, 2, 3, 4],
       [0, 1, 2, 3, 4],
       [0, 1, 2, 3, 4],
       [0, 1, 2, 3, 4]])

So x[:, np.newaxis] - y相当于broadcasted_x - broadcasted_y.

现在,有了这个更简单的例子,我们可以看看arr1[:,np.newaxis,:]-arr2.

arr1有形状(1000, 4) and arr2有形状(100, 4)。我们想要减去长度为 4 的轴中的项目,对于沿 1000 长度轴的每一行和沿 100 长度轴的每一行。换句话说,我们希望减法形成一个形状数组(1000, 100, 4).

重要的是,我们不希望1000-axis100-axis. 我们希望它们位于不同的轴上.

所以如果我们添加一个轴arr1像这样:arr1[:,np.newaxis,:],那么它的形状就变成了

In [22]: arr1[:, np.newaxis, :].shape
Out[22]: (1000, 1, 4)

现在,NumPy 广播将两个数组提升为常见的形状(1000, 100, 4)。瞧,减法表。

将值调整为形状的 2D DataFrame(1000*100, 4), 我们可以用reshape:

arr3 = arr3.reshape(-1, 4)

The -1告诉 NumPy 替换-1使重塑有意义所需的任何正整数。自从arr有 1000*100*4 个值,-1被替换为1000*100. Using -1比写作更好1000*100但是,即使我们更改中的行数,它也允许代码工作df1 and df2.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

两个 DataFrame 中每一行和每一列之间的差异(Python / Pandas) 的相关文章

随机推荐