以最有效的方式比较两个熊猫数据帧



让我们考虑两个panda数据帧:

import numpy as np
import pandas as pd
df = pd.DataFrame([1, 2, 3, 2, 5, 4, 3, 6, 7])
check_df = pd.DataFrame([3, 2, 5, 4, 3, 6, 4, 2, 1])

如果想做以下事情:

  1. 如果df[1] > check_df[1]df[2] > check_df[1]df[3] > check_df[1],则我们赋值给df 1,否则赋值为0
  2. 如果df[2] > check_df[2]df[3] > check_df[2]df[4] > check_df[2],则我们分配给df1,否则分配给0
  3. 我们将相同的算法应用于DataFrame的末尾

我的基元代码如下:

df_copy = df.copy()
for i in range(len(df) - 3):
moving_df = df.iloc[i:i+3]
if (moving_df >check_df.iloc[i]).any()[0]:
df_copy.iloc[i] = 1
else:
df_copy.iloc[i] = -1
df_copy

0
0   -1
1   1
2   -1
3   1
4   1
5   -1
6   3
7   6
8   7

如果有可能在没有循环的情况下这样做,你能给我一个建议吗?

IIUC,使用rolling.min:可以轻松完成

df['out'] = np.where(df[0].rolling(N, min_periods=1).max().shift(1-N).gt(check_df[0]),
1, -1)

输出:

0  out
0  1   -1
1  2    1
2  3   -1
3  2    1
4  5    1
5  4   -1
6  3    1
7  6   -1
8  7   -1

将最后一项保持原样:

m = df[0].rolling(N).max().shift(1-N)
df['out'] = np.where(m.gt(check_df[0]),
1, -1)
df['out'] = df['out'].mask(m.isna(), df[0])

输出:

0  out
0  1   -1
1  2    1
2  3   -1
3  2    1
4  5    1
5  4   -1
6  3    1
7  6    6
8  7    7

虽然@mozway已经提供了一个非常智能的解决方案,但我也想分享我的方法,这是受这篇文章的启发。

您可以创建自己的对象,将序列与滚动序列进行比较。该比较可以由典型的运算符执行,即>, < or ==。如果至少有一个比较成立,则对象将返回预定义的值(在列表returns_tf中给出,其中如果比较为真,则返回第一个元素,如果比较为假,则返回第二个元素(。

可能的代码:

import numpy as np
import pandas as pd
df = pd.DataFrame([1, 2, 3, 2, 5, 4, 3, 6, 7])
check_df = pd.DataFrame([3, 2, 5, 4, 3, 6, 4, 2, 1])
class RollingComparison:

def __init__(self, comparing_series: pd.Series, rolling_series: pd.Series, window: int):
self.comparing_series = comparing_series.values[:-1*window]
self.rolling_series = rolling_series.values
self.window = window

def rolling_window_mask(self, option: str = "smaller"):
shape = self.rolling_series.shape[:-1] + (self.rolling_series.shape[-1] - self.window + 1, self.window)
strides = self.rolling_series.strides + (self.rolling_series.strides[-1],)
rolling_window = np.lib.stride_tricks.as_strided(self.rolling_series, shape=shape, strides=strides)[:-1]
rolling_window_mask = (
self.comparing_series.reshape(-1, 1) < rolling_window if option=="smaller" else (
self.comparing_series.reshape(-1, 1) > rolling_window if option=="greater" else self.comparing_series.reshape(-1, 1) == rolling_window
)
)
return rolling_window_mask.any(axis=1)

def assign(self, option: str = "rolling", returns_tf: list = [1, -1]):
mask = self.rolling_window_mask(option)
return np.concatenate((np.where(mask, returns_tf[0], returns_tf[1]), self.rolling_series[-1*self.window:]))

分配可以实现如下:

roller = RollingComparison(check_df[0], df[0], 3)
check_df["rolling_smaller_checking"] = roller.assign(option="smaller")
check_df["rolling_greater_checking"] = roller.assign(option="greater")
check_df["rolling_equals_checking"] = roller.assign(option="equal")

输出(列rolling_smaller_checking等于您想要的输出(:

0   rolling_smaller_checking  rolling_greater_checking  rolling_equals_checking
0   3   -1                        1                         1
1   2    1                       -1                         1
2   5   -1                        1                         1
3   4    1                        1                         1
4   3    1                       -1                         1
5   6   -1                        1                         1
6   4    3                        3                         3
7   2    6                        6                         6
8   1    7                        7                         7

最新更新