在Python和PySpark中相当于R data.table滚动连接

tjb*_*305 4 python r data.table pyspark pyspark-sql

有谁知道如何在PySpark中进行R data.table滚动连接?

从Ben 那里借用例子和滚动连接的很好的解释;

sales<-data.table(saleID=c("S1","S2","S3","S4","S5"), 
              saleDate=as.Date(c("2014-2-20","2014-5-1","2014-6-15","2014-7- 1","2014-12-31")))

commercials<-data.table(commercialID=c("C1","C2","C3","C4"), 
                    commercialDate=as.Date(c("2014-1-1","2014-4-1","2014-7-1","2014-9-15")))

setkey(sales,"saleDate")
setkey(commercials,"commercialDate")

sales[commercials, roll=TRUE]
Run Code Online (Sandbox Code Playgroud)

结果是;

saleDate saleID commercialID
1: 2014-01-01     NA    C1
2: 2014-04-01     S1    C2
3: 2014-07-01     S4    C3
4: 2014-09-15     S4    C4
Run Code Online (Sandbox Code Playgroud)

非常感谢您的帮助。

Mak*_*e42 5

滚动加入不是 join +fillna

所有滚动加盟首先是一样的一个join和一个fillna!仅当与之联接的表的键(就data.table而言,将是左表和右联接)在主表中具有等效项时,才是这种情况。data.table滚动联接不需要这样做。

据我所知,没有直接的对等物,我搜索了很长时间。甚至还有一个问题https://github.com/pandas-dev/pandas/issues/7546。然而:

大熊猫解决方案:

熊猫有解决方案。假设您的右数据表是表A,左数据表是表B。

  1. 按键对表A和B进行排序。
  2. tag向全为0的A 添加一列,向tag全为1的B 添加一列。
  3. tag从B 删除键和B 之外的所有列(可以省略,但是这样更清楚),然后调用表B'。将B保留为原始格式-我们稍后将需要它。
  4. 将A与B'连接到C,而忽略来自B'的行具有许多NA的事实。
  5. 按键对C排序。
  6. 制作一个新的cumsum列 C = C.assign(groupNr = np.cumsum(C.tag))
  7. 对所有B'行使用过滤(querytag
  8. groupNr在原始B上添加一个运行计数器列(整数从0到N-1或从1到N,取决于您是要向前还是向后滚动联接)。
  9. 将B与C一起加入groupNr

程式码

#0. 'date' is the key for the rolling join. It does not have to be a date.
A = pd.DataFrame.from_dict(
    {'date': pd.to_datetime(["2014-3-1", "2014-5-1", "2014-6-1", "2014-7-1", "2014-12-1"]),
     'value': ["a1", "a2", "a3", "a4", "a5"]})
B = pd.DataFrame.from_dict(
    {'date': pd.to_datetime(["2014-1-15", "2014-3-15", "2014-6-15", "2014-8-15", "2014-11-15", "2014-12-15"]),
     'value': ["b1", "b2", "b3", "b4", "b5", "b6"]})

#1. Sort the table A and and B each by key.
A = A.sort_values('date')
B = B.sort_values('date')

#2. Add a column tag to A which are all 0 and a column tag to B that are all 1.
A['tag'] = 0
B['tag'] = 1

#3. Delete all columns except the key and tagfrom B (can be omitted, but it is clearer this way) and call the table B'. Keep B as an original - we are going to need it later.
B_ = B[['date','tag']] # You need two [], because you get a series otherwise.

#4. Concatenate A with B' to C and ignore the fact that the rows from B' has many NAs.
C = pd.concat([A, B_])

#5. Sort C by key.
C = C.sort_values('date')

#6. Make a new cumsum column with C = C.assign(groupNr = np.cumsum(C.tag))
C = C.assign(groupNr = np.cumsum(C.tag))

#7. Using filtering (query) on tag get rid of all B'-rows.
C = C[C.tag == 0]

#8. Add a running counter column groupNr to the original B (integers from 0 to N-1 or from 1 to N, depending on whether you want forward or backward rolling join).
B['groupNr'] = range(len(B)+1)[1:] # B's values are carried forward to A's values
B['groupNr'] = range(len(B))       # B's values are carried backward to A's values

#9. Join B with C on groupNr to D.
D = C.set_index('groupNr').join(B.set_index('groupNr'), lsuffix='_A', rsuffix='_B')
Run Code Online (Sandbox Code Playgroud)