我正在尝试在 data.table 中进行一些聚合,但我面临着一个无法找到解决方案的挑战。挑战真的很简单,我想沿着不止一个维度来总结 data.table 中的一些值。
让以下代码正常工作没有问题:
Export4R[,sum(units),by=Type]
Run Code Online (Sandbox Code Playgroud)
这给出了以下几点:
Type Value
foobar 45
barfoo 25
Run Code Online (Sandbox Code Playgroud)
但现在我想把它进一步分解一下,希望得到一张这样的表:
Type Month Value
foobar Mar 12
foobar Apr 7
....
Run Code Online (Sandbox Code Playgroud)
我试图用一行代码来做到这一点,但不幸的是,这似乎不起作用:
Export4R[,sum(units),by=Type,Month]
Run Code Online (Sandbox Code Playgroud)
这很可能是一个非常简单的问题,但我很难找到答案。
感谢您的帮助!
我有一个这样的查询:
SELECT
id
FROM
table1
INNER JOIN
...
WHERE
...
AND eventtype IN (2000120, 2000121, 2000122, 2000123, 2000130)
EXCEPT
SELECT
id
FROM
table1
INNER JOIN
...
WHERE
...
AND eventtype IN (2000123,2000130,2000134,2000135)
Run Code Online (Sandbox Code Playgroud)
请注意,除了最后一个 where 子句之外,这两个查询都是相同的。
我现在想计算上述查询返回的记录数。
如何?
非常感谢
我有一个DataFrame df, 组成(age, height). 我想看看身高的平均值如何随年龄变化,所以我分组df并age尝试形成一个新的由DataFrame new_df组成,(age, mean_height)代码如下:
groups = df.groupby('age')
new_df = groups.agg({'height' : np.mean,
'age' : # HOW to add age?})
Run Code Online (Sandbox Code Playgroud)
但我不知道如何附加age,new_df希望有人能给我一些建议。
我的问题比较简单。我确实有一个项目,它有一个典型的带有依赖管理的父 pom 和一个带有“正常”继承这些依赖的 pom 的模块。
如果我在继承的 pom 中明确命名父级,则项目构建成功,但如果我省略父级信息,则项目无法构建。
人们期望 maven 能够正常构建这个项目,因为聚合 pom 列出了所有模块以及依赖项管理。
为什么不是这样?
我有一张桌子看起来像:
date item_id store_id sale_num
1/1/15 33 1 10
1/1/15 33 2 12
1/1/15 33 3 15
1/1/15 44 1 54
1/1/15 44 3 66
1/2/15 33 1 14
....
Run Code Online (Sandbox Code Playgroud)
我想转换表,以便将store_id放入多列,而value是sale_num.该表应如下:
date item_id store1 store2 store3
1/1/15 33 10 12 15
1/1/15 44 54 NA 66
1/2/15 33 14 NA NA
......
Run Code Online (Sandbox Code Playgroud)
当我使用小规模的cast函数,原始表中的1000行,没有问题.
但是,原始表在R中有38,000,000行和comsumes 1.5 GB内存.当我使用强制转换功能时,该函数花费大约34 GB内存,并且它无休止地运行.
有什么问题呢?还有其他方法吗?
我对构建霍夫曼编码原型感兴趣。为此,我想首先生成组成输入 Java 的字符的直方图String。我已经在 SO 和其他地方看到了许多解决方案(例如:这里依赖于使用s的collect()方法Stream以及以非常具体和直观的方式使用Function.identity()和 的静态导入Collectors.counting()。
但是,当使用一段与我上面链接的代码非常相似的代码时:
private List<HuffmanTrieNode> getCharsAndFreqs(String s){
Map<Character, Long> freqs = s.chars().collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
return null;
}
Run Code Online (Sandbox Code Playgroud)
我从 Intellij 收到一个编译时错误,它基本上告诉我没有collect符合Supplier类型的参数,如其签名所要求的:
不幸的是,我是 Java 8Stream层次结构的新手,我不完全确定对我来说最好的行动方案应该是什么。事实上,Map对于我正在尝试做的事情来说,走这条路可能太多了;如果是这样,请告知。
我有一个带有两个索引的熊猫系列:
df_agg=df.groupby(['yearID','teamID']).sum()['Salary']
df_agg.head()
yearID teamID
1985 ATL 14807000
BAL 11560712
BOS 10897560
CAL 14427894
CHA 9846178
Run Code Online (Sandbox Code Playgroud)
我想将 int 转换为 Pandas 数据框,例如
yearID teamID Salary
1985 ATL 14807000
1985 BAL 11560712
1985 BOS 10897560
1985 CAL 14427894
1985 CHA 9846178
Run Code Online (Sandbox Code Playgroud)
我厌倦了使用:
df_new=df_agg.reset_index(inplace=True)
Run Code Online (Sandbox Code Playgroud)
但我收到以下错误:
TypeError Traceback(最近一次调用最后一次) in () ----> 1 df_new=df_agg.reset_index(inplace=True)
C:\Users\ameimand\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\core\series.py in reset_index(self, level, drop, name, inplace) 966
index=new_index).__finalize__(self)
967 elif inplace:
--> 968 raise TypeError('Cannot reset_index inplace on a Series '
969 'to create a DataFrame')
970 else:
TypeError: Cannot …Run Code Online (Sandbox Code Playgroud) 我正在通过 Amazon can service 使用 MySQL 服务器,使用默认设置。所涉及的表mytable属于InnoDB类型,大约有 10 亿行。查询是:
select count(*), avg(`01`) from mytable where `date` = "2017-11-01";
Run Code Online (Sandbox Code Playgroud)
这需要将近 10 分钟的时间来执行。我在 上有一个索引date。在EXPLAIN此查询的是:
+----+-------------+---------------+------+---------------+------+---------+-------+---------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------------+------+---------------+------+---------+-------+---------+-------+
| 1 | SIMPLE | mytable | ref | date | date | 3 | const | 1411576 | NULL |
+----+-------------+---------------+------+---------------+------+---------+-------+---------+-------+
Run Code Online (Sandbox Code Playgroud)
该表中的索引是:
+---------------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | …Run Code Online (Sandbox Code Playgroud) 在各种聚合管道阶段之后,我的第一个查询返回以下结果:
{
"group" : "A",
"count" : 6,
"total" : 20
},
{
"group" : "B",
"count" : 2,
"total" : 50
}
Run Code Online (Sandbox Code Playgroud)
在各种聚合管道阶段之后,我的第二个查询返回以下结果:
{
"group": "A",
"count": 4,
"total": 80
},
{
"group": "C",
"count": 12,
"total": 60
}
Run Code Online (Sandbox Code Playgroud)
两个查询都在同一个集合上执行,但根据管道阶段对数据进行不同的分组和转换。
我的两个查询都使用不同的
$match条件,具有各种管道阶段,包括$facet,$unwind,$group,$project和运算符,例如$map,$reduce,$zip,$subtract...
db.collection.aggregate([
{ $unwind...},
{ $match....},
{ $facet...},
...
])
Run Code Online (Sandbox Code Playgroud)
当我将$facet查询作为并行查询运行时,会出现以下错误(因为我已经$facet在现有查询中使用了):
$facet is not allowed to be used within a $facet stage
Run Code Online (Sandbox Code Playgroud)
预期输出:
我需要找到每个组的平均值。
为此,我需要组合两个查询的结果并对组合结果执行查询。
我的组合阶段应该是这样的:
{
"group" : …Run Code Online (Sandbox Code Playgroud) merge aggregation mongodb mongodb-query aggregation-framework
我有如下 DataFrame:
df = pd.DataFrame({"ID" : ["1", "1", "1", "2", "2", "2", "1"],
"status" : ["ac", "not", "not", "ac", np.NaN, "ac", "oth"]})
Run Code Online (Sandbox Code Playgroud)
我需要使用如下列构建 DataFrame:
你能帮我构建如下 DF 吗?
aggregation ×10
pandas ×3
dataframe ×2
python ×2
r ×2
data.table ×1
dependencies ×1
dimension ×1
group-by ×1
grouping ×1
histogram ×1
inheritance ×1
java-8 ×1
java-stream ×1
maven ×1
merge ×1
mongodb ×1
mysql ×1
sql ×1
sum ×1
t-sql ×1