**Calender_Date** **Customer_ID** **Amount**
01-Jan-21 20211003 59
01-Jan-21 20211005 100
27-Jan-21 20211003 25
25-Feb-21 20211003 188
23-Mar-21 20211005 67
24-Apr-21 20211009 43
25-May-21 20211013 21
26-May-21 20211013 89
11-Jul-21 20211009 65
28-Aug-21 20211003 90
Run Code Online (Sandbox Code Playgroud)
需要:考虑 Customer_ID - 滚动总和超过 90 日历回顾日期(从每个给定的当前日期)。
输出要求或期望的结果
**Calender_Date** **Customer_ID** **Amount** **SumofLookbackRolling90DayAmount**
1-Jan-21 20211003 59 59
1-Jan-21 20211005 100 100
27-Jan-21 20211003 25 84
25-Feb-21 20211003 188 272
23-Mar-21 20211005 67 167
24-Apr-21 20211009 43 43
25-May-21 20211013 21 21
26-May-21 20211013 89 110
11-Jul-21 20211009 65 …Run Code Online (Sandbox Code Playgroud) 我有一个如下所示的数据框。我想在每个组内进行分组device和排序start_time。然后,对于组中的每一行,从其前面 3 行(包括其自身)的窗口中获取最常出现的站点。
columns = ['device', 'start_time', 'station']
data = [("Python", 1, "station_1"), ("Python", 2, "station_2"), ("Python", 3, "station_1"), ("Python", 4, "station_2"), ("Python", 5, "station_2"), ("Python", 6, None)]
test_df = spark.createDataFrame(data).toDF(*columns)
rolling_w = Window.partitionBy('device').orderBy('start_time').rowsBetween(-2, 0)
Run Code Online (Sandbox Code Playgroud)
期望的输出:
columns = ['device', 'start_time', 'station']
data = [("Python", 1, "station_1"), ("Python", 2, "station_2"), ("Python", 3, "station_1"), ("Python", 4, "station_2"), ("Python", 5, "station_2"), ("Python", 6, None)]
test_df = spark.createDataFrame(data).toDF(*columns)
rolling_w = Window.partitionBy('device').orderBy('start_time').rowsBetween(-2, 0)
Run Code Online (Sandbox Code Playgroud)
由于 Pyspark 没有mode()函数,我知道如何获取静态中最常见的值,groupby如下所示, …
group-by apache-spark apache-spark-sql rolling-computation pyspark
我有一张这样的桌子
\nimport pandas as pd\nimport numpy as np\n\ndf = pd.DataFrame.from_dict({\'date\':[1,2,3,4,5,6,7,8,9,10] ,\'high\':[10,9,8,8,7,6,7,8,9,10],\'low\':[9,7,6,5,2,1,2,1,8,9],\'stock\':[\'A\']*5 + [\'B\']*5})\nRun Code Online (Sandbox Code Playgroud)\n| 日期 | 高的 | 低的 | 库存 |
|---|---|---|---|
| 1 | 10 | 9 | A |
| 2 | 9 | 7 | A |
| 3 | 8 | 6 | A |
| 4 | 8 | 5 | A |
| 5 | 7 | 2 | A |
| 6 | 6 | 1 | 乙 |
| 7 | 7 | 2 | 乙 |
| 8 | 8 | 1 | 乙 |
| 9 | 9 | 8 | 乙 |
| 10 | 10 | 9 | 乙 |
对于每只股票的每一天,我想知道今天的 \xe2\x80\x9chigh\xe2\x80\x9d 和最低价(之后或今天)之间的最大差异是多少。例如,在日期 1,股票 A 的最高价格为 10 美元。我查看日期 1-5,发现最高价和最低价之间的最大差异是在日期 5。日期 1 的结果将是 10-2=8。在日期 2,我应该只查看日期 2 之后的“最低价”。 …
我有一个数据框 games_h。这只是表格的一个片段,但它有很多球队,并按日期、球队、比赛编号排序。我正在尝试创建按团队分组的加权滚动平均值。我希望最近一场比赛的权重是两场以上之前的。因此权重将为 (Game_1 * 1+ Game_2 *2)/3 或权重等于 1,且比率相同,因此权重 = c(1-.667, .667)。
dput(games_h)
structure(list(GameId = c(16, 16, 37, 37, 57, 57), GameDate = structure(c(17905,
17905, 17916, 17916, 17926, 17926), class = "Date"), NeutralSite = c(0,
0, 0, 0, 0, 0), AwayTeam = c("Virginia Cavaliers", "Virginia Cavaliers",
"Florida State Seminoles", "Florida State Seminoles", "Syracuse Orange",
"Syracuse Orange"), HomeTeam = c("Boston College Eagles", "Boston College Eagles",
"Boston College Eagles", "Boston College Eagles", "Boston College Eagles",
"Boston College Eagles"), Team = c("Virginia Cavaliers", …Run Code Online (Sandbox Code Playgroud) 我有一个包含日期、ID 和值的数据框。
例如:
date id value
2016-08-28 A 1
2016-08-28 B 1
2016-08-29 C 2
2016-09-02 B 0
2016-09-03 A 3
2016-09-06 C 1
2017-01-15 B 2
2017-01-18 C 3
2017-01-18 A 2
Run Code Online (Sandbox Code Playgroud)
我想按元素应用滚动平均值,然后声明一个,以便结果如下:
date id value rolling_mean
2016-08-28 A 1 NaN
2016-08-28 B 1 NaN
2016-08-29 C 2 NaN
2016-09-02 B 0 0.5
2016-09-03 A 3 2.0
2016-09-06 C 1 1.5
2017-01-15 B 2 1.0
2017-01-18 C 3 2.0
2017-01-18 A 2 2.5
Run Code Online (Sandbox Code Playgroud)
我最接近的是:
grouped = df.groupby(["id", "value"])
df["rolling_mean"] …Run Code Online (Sandbox Code Playgroud) 我有一个包含两个变量的示例:ID 和 ym。ID id 是指每个交易者的特定 ID,ym 是指年月变量。我想创建一个变量来显示t月之前 10 年期间的年数,如下图所示。
ID ym Want
1 200101 0
1 200301 1
1 200401 2
1 200501 3
1 200601 4
1 200801 5
1 201201 5
1 201501 4
2 200001 0
2 200203 1
2 200401 2
2 200506 3
Run Code Online (Sandbox Code Playgroud)
我尝试使用by函数并fisrt.id计算数字。
data want;
set have;
want+1;
by id;
if first.id then want=1;
run;
Run Code Online (Sandbox Code Playgroud)
但是,ym 中的年份不是连续的。当时间间隔大于 10 年时,此方法不起作用。尽管我假设我需要计算滚动窗口(10 年)中的年数,但我不确定如何实现。请给我一些建议。谢谢。