- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在尝试为数据集中的每个 fruit
列创建平均收入列。
数据集如下所示:
Time England Apples ... England Watermelons England Price
0 1/01/2011 0:30 6135.998518 ... 0.000000 25.00
1 1/01/2011 1:00 5711.638352 ... 0.000000 24.43
2 1/01/2011 1:30 5455.901902 ... 0.000000 25.02
3 1/01/2011 2:00 5130.634418 ... 0.000000 22.82
4 1/01/2011 2:30 4854.064390 ... 0.000000 21.19
5 1/01/2011 3:00 4654.938155 ... 0.000000 22.28
6 1/01/2011 3:30 4413.649635 ... 0.000000 19.64
7 1/01/2011 4:00 4153.377478 ... 0.000000 19.83
8 1/01/2011 4:30 4099.620177 ... 0.000000 19.80
9 1/01/2011 5:00 4041.403822 ... 0.000000 18.85
10 1/01/2011 5:30 4097.059952 ... 0.000000 19.49
11 1/01/2011 6:00 4074.397538 ... 0.000000 18.68
12 1/01/2011 6:30 4141.839692 ... 0.000000 20.03
13 1/01/2011 7:00 4463.231217 ... 0.000000 21.92
14 1/01/2011 7:30 4727.591175 ... 0.000000 21.48
15 1/01/2011 8:00 4842.730830 ... 0.000000 20.88
16 1/01/2011 8:30 5206.647033 ... 0.000000 24.87
17 1/01/2011 9:00 5533.648183 ... 0.000000 25.24
18 1/01/2011 9:30 5921.572143 ... 0.000000 25.31
19 1/01/2011 10:00 6279.324155 ... 0.000000 25.32
20 1/01/2011 10:30 6709.511942 ... 0.000000 25.31
21 1/01/2011 11:00 6978.742550 ... 0.000000 25.54
22 1/01/2011 11:30 7110.139363 ... 0.000000 27.86
23 1/01/2011 12:00 7063.761970 ... 0.000000 24.49
24 1/01/2011 12:30 6992.549385 ... 0.000000 25.31
25 1/01/2011 13:00 6961.793427 ... 0.000000 25.26
26 1/01/2011 13:30 7055.875967 ... 0.000000 25.31
27 1/01/2011 14:00 7142.211047 ... 0.000000 25.31
28 1/01/2011 14:30 7228.536090 ... 0.000000 26.35
29 1/01/2011 15:00 7299.410813 ... 0.000000 27.52
... ... ... ... ... ...
142002 6/02/2019 9:30 7676.377063 ... 330.175727 111.45
142003 6/02/2019 10:00 7670.922868 ... 331.714652 114.43
142004 6/02/2019 10:30 7658.970773 ... 315.955275 115.47
142005 6/02/2019 11:00 7654.404070 ... 331.450534 118.27
142006 6/02/2019 11:30 7634.777022 ... 329.376822 130.77
142007 6/02/2019 12:00 7663.339550 ... 308.338850 127.27
142008 6/02/2019 12:30 7668.300007 ... 308.836712 128.69
142009 6/02/2019 13:00 7633.525948 ... 313.522324 156.85
142010 6/02/2019 13:30 7614.107300 ... 317.741907 165.16
142011 6/02/2019 14:00 7647.885410 ... 318.575012 139.67
142012 6/02/2019 14:30 7758.311397 ... 300.859020 129.19
142013 6/02/2019 15:00 7792.523983 ... 288.397673 265.37
142014 6/02/2019 15:30 7849.658337 ... 268.816729 262.73
142015 6/02/2019 16:00 7962.783263 ... 260.514448 257.19
142016 6/02/2019 16:30 8008.872848 ... 217.321907 164.39
142017 6/02/2019 17:00 8001.217682 ... 196.016162 129.90
142018 6/02/2019 17:30 8002.191668 ... 155.652355 106.81
142019 6/02/2019 18:00 8051.317657 ... 79.418596 112.66
142020 6/02/2019 18:30 8079.327247 ... 36.547664 103.34
142021 6/02/2019 19:00 8056.183235 ... 9.403131 110.64
142022 6/02/2019 19:30 8060.892678 ... 0.306932 115.63
142023 6/02/2019 20:00 8083.306235 ... 0.000000 109.97
142024 6/02/2019 20:30 7928.332383 ... 0.000000 108.33
142025 6/02/2019 21:00 7736.462477 ... 0.000000 92.86
142026 6/02/2019 21:30 7439.131347 ... 0.000000 88.37
142027 6/02/2019 22:00 7080.748895 ... 0.000000 82.93
142028 6/02/2019 22:30 6991.127062 ... 0.000000 90.36
142029 6/02/2019 23:00 6922.695807 ... 0.000000 77.94
142030 6/02/2019 23:30 6850.425935 ... 0.000000 83.39
142031 7/02/2019 0:00 6666.447972 ... 0.000000 82.67
[142032 rows x 7 columns]
我正在尝试为每个fruit
添加一个新列,这将是200个时间段内的平均收入(相当于Excel中的SUMPRODUCT(Apples:Price)/SUM(Apples) )
我必须在 python 中执行此操作的代码对于小型数据集运行良好,但是对于我的大型数据集,运行时间很长(超过 20 分钟)。
我的代码如下:
import pandas as pd
import numpy as np
df = pd.read_csv("england_raw.csv")
size = 200
max_size = df.shape[0]
for a in [' Apples',' Oranges',' Pears',' Apricots',' Watermelons']:
e = 'England' + a + '_W'
df[e] = np.empty(max_size)
for i in range(max_size-size):
df[e][i] = np.average(df['England Price'][i:i+size], weights=df['England'+a][i:i+size])
df.to_csv("england_done.csv",index=False)
有什么方法可以修改我的代码以加快处理时间,甚至使用不同的方法来实现我想要的结果?
谢谢。
期望结果(Excel 等效结果):
编辑:
最佳答案
这是您要找的吗?它为您提供每列中 200 个点的窗口的滚动平均值。
# Intermediate columns for calculations
df['revenue'] = 0
df['roll_rev_sum'] = 0
df['roll_qty_sum'] = 0
# Please adjust your column index accordingly. This is quite a brute solution
for col in df.columns[1:-1]:
e = 'England' + col + '_W'
df['revenue'] = df[col] * df['England Price']
df['roll_rev_sum'] = df.loc[:,'revenue'].rolling(200).sum()
df['roll_qty_sum'] = df.loc[:,col].rolling(200).sum()
df[e] = df['roll_rev_sum']/df['roll_qty_sum']
编辑:更新为包括注释中描述的中间列,还包括 OP 指定的更多详细信息。
关于python - Sumproduct 等效的 python 代码运行时间太长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54642380/
我有一个如下所示的数据框: import pandas as pd d = {'decil': ['1. decil','1. decil','2. decil','2. decil','3. dec
我有一些数据想要添加到我的应用中...大约 650 个类别(包括名称 + ID 号),每个类别平均有 85 个项目(每个都有一个名称/ID 号)。 iPhone会支持这么大的plist吗?我想首先在
我目前正在使用 Python 从头开始实现决策树算法。我在实现树的分支时遇到了麻烦。在当前的实现中,我没有使用深度参数。 发生的情况是,要么分支结束得太快(如果我使用标志来防止无限递归),要么如果
我在 Stack 上发现了这个问题 - Measuring the distance between two coordinates in PHP 这个答案在很多方面似乎对我来说都是完美的,但我遇到了
我目前正在清理一个具有 2 个索引和 2.5 亿个事件行以及大约同样多(或更多)的死行的表。我从我的客户端计算机(笔记本电脑)向我的服务器发出命令 VACCUM FULL ANALYZE。在过去的 3
这一切都有点模糊,因为该计划是相当深入的,但坚持我,因为我会尽量解释它。我编写了一个程序,它接受一个.csv文件,并将其转换为MySQL数据库的INSERT INTO语句。例如: ID Numbe
我有一个地址示例:0x003533,它是一个字符串,但要使用它,我需要它是一个 LONG,但我不知道该怎么做:有人有解决方案吗? s 字符串:“0x003533”到长 0x003533 ?? 最佳答案
请保持友善 - 这是我的第一个问题。 =P 基本上作为一个暑期项目,我一直在研究 wikipedia page 上的数据结构列表。并尝试实现它们。上学期我参加了 C++ 类(class),发现它非常有
简单的问题。想知道长 IN 子句是否是一种代码味道?我真的不知道如何证明它。除了我认为的那样,我不知道为什么它会闻起来。 select name, code, capital, pop
我正在尝试基于 C# 中的种子生成一个数字。唯一的问题是种子太大而不能成为 int32。有什么方法可以像种子一样使用 long 吗? 是的,种子必须很长。 最佳答案 这是我移植的 Java.Util.
我一直想知道这个问题有一段时间了。在 CouchDB 中,我们有一些相当的日志 ID……例如: “000ab56cb24aef9b817ac98d55695c6a” 现在,如果我们正在搜索此项目并浏览
列的虚拟列 c和一个给定的值 x等于 1如果 c==x和 0 其他。通常,通过为列创建虚拟对象 c , 一排除一个值 x选择,因为最后一个虚拟列不添加任何信息 w.r.t.已经存在的虚拟列。 这是我如
使用 tarantool,为什么我要记录这些奇怪的消息: 2016-03-24 16:19:58.987 [5803] main/493623/http/XXX.XXX.XXX.XXX:57295 t
我显然是 GitHub 的新手,想确保在开始之前我做的事情是正确的。 我想创建一个新的存储库,它使用来自 2 个现有项目的复刻/克隆。现有项目不是我的。 假设我想使用的 repo 被称为来自开发人员“
我的应用程序名称长度为 17 个字符。当安装在设备上时,它看起来像应用程序...名称。有没有办法在多行上显示应用程序名称?请帮忙。 最佳答案 不,你不能。我认为 iPad 支持 15 个字符来完整显示
我必须编写一个程序来读取文件中的所有单词,并确定每个单词使用了多少次。我的任务是使用多线程来加快运行时间,但是单线程程序的运行速度比多线程程序快。我曾尝试研究此问题的解决方案,但很多解释只会让我更加困
假设我在给定的范围内有一个位置pos,这样: 0 = newRange*newRange : "Case not supported yet"; // Never happens in my code
我试图在 Java 中将 unix 时间四舍五入到该月的第一天,但没有成功。示例: 1314057600 (Tue, 23 Aug 2011 00:00:00 GMT) 至 1312156800
我们的项目有在 CVS 中从现有分支创建新分支的历史。几年后,这导致了每次发布时更改的文件上的这种情况: 新版本:1.145.4.11.2.20.2.6.2.20.2.1.2.11.2.3.2.4.4
我有以下数据框: DAYS7 <- c('Monday','Tuesday','Wednesday','Thursday','Friday', 'Saturday', 'Sunday') DAYS
我是一名优秀的程序员,十分优秀!