我有一个指向内容的URL,我需要获得其中一列中包含的最高值.是否有任何聚合函数可以实现,或者我必须手动执行此操作?
我有这张桌子messages;
sender_id recipient_id
1 2
1 3
1 3
2 1
3 1
2 3
Run Code Online (Sandbox Code Playgroud)
我希望选择以下行:
sender_id或.receiver_idcurrent_user.id即我想从表中选择唯一的sender_id = 2或者recipient_id = 2我需要这个结果:
sender_id recipient_id
2 1
2 3
Run Code Online (Sandbox Code Playgroud)
怎么做?
为什么?因为我希望建立一个类似于Facebook的收件箱,其中汇总了已发送和已接收的邮件,此查询是目前为止的瓶颈.
我使用的是rails 3.2和Postgres 9.3.
我正在处理用户可以选择他/她想要在屏幕上看到的列以及要分组或聚合的列的应用程序.因此,在我的LINQ部分中,我实际上应该将包含列名的变量传递给group by和aggregate子句.请记住,DataTable dt每次可能包含不同的数据(例如员工信息,采购订单,绩效统计等).我只能通过dt.Columns[i].ColumnName和运行时获取有关数据的信息dt.Columns[i].DataType.Name.任何人都可以建议如何做到这一点,我需要的是这样的事情:
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
var query = from row in dt.AsEnumerable()
group row by new
{
foreach(DataColumn column in dt.Columns)
{
row[column.ColumnName];
}
} into grp
select new
{
foreach(DataColumn column in dt.Columns)
{
if(column.DataType.Name == "Decimal")
{
Sum(grp[column.ColumnName]);
}else{
grp[column.ColumnName];
}
}
};
Run Code Online (Sandbox Code Playgroud) 我的数据,
Id|date1|date2
1|2008-10-01|NA
1|NA|2008-10-02
1|NA|2008-10-03
2|2008-10-02|NA
2|NA|2008-10-03
Run Code Online (Sandbox Code Playgroud)
我想用这种方式输出,
Id|date1|date2|date3
1|2008-10-01|2008-10-02|2008-10-03
2|2008-10-02|2008-10-03
Run Code Online (Sandbox Code Playgroud)
我尝试使用聚合和dcast,但他们将日期转换为数字格式,并且仍然无法避免使用na.
我有兴趣找到一种有效的方式来获取包含以下内容的组表的摘要:
例如,在生成描述性统计信息的情况下,我使用以下代码:
data("mtcars")
require(dplyr)
mt_sum <- mtcars %>%
group_by(cyl) %>%
summarise_each(funs(min,max), hp, wt, disp)
Run Code Online (Sandbox Code Playgroud)
这会产生所需的输出:
> head(mt_sum)
Source: local data frame [3 x 7]
cyl hp_min wt_min disp_min hp_max wt_max disp_max
(dbl) (dbl) (dbl) (dbl) (dbl) (dbl) (dbl)
1 4 52 1.513 71.1 113 3.190 146.7
2 6 105 2.620 145.0 175 3.460 258.0
3 8 150 3.170 275.8 335 5.424 472.0
Run Code Online (Sandbox Code Playgroud)
我有兴趣用数字来丰富数据,这个数字可以反映每个组的值计数.关于计数,这可以简单地完成:
mt_sum2 <- mtcars %>%
group_by(cyl) %>%
summarise(countObs = n())
Run Code Online (Sandbox Code Playgroud)
这将生成所需的数据:
> …Run Code Online (Sandbox Code Playgroud) 我有一个pandas排序的数据框(基于时间)是这样的:
from datetime import datetime
df = pd.DataFrame({ 'ActivityDateTime' : [datetime(2016,5,13,6,14),datetime(2016,5,13,6,16),
datetime(2016,5,13,6,20),datetime(2016,5,13,6,27),datetime(2016,5,13,6,31),
datetime(2016,5,13,6,32),
datetime(2016,5,13,17,34),datetime(2016,5,13,17,36),
datetime(2016,5,13,17,38),datetime(2016,5,13,17,45),datetime(2016,5,13,17,47),
datetime(2016,5,16,13,3),datetime(2016,5,16,13,6),
datetime(2016,5,16,13,10),datetime(2016,5,16,13,14),datetime(2016,5,16,13,16)],
'Value1' : [0.0,2.0,3.0,4.0,0.0,0.0,0.0,7.0,8.0,4.0,0.0,0.0,3.0,9.0,1.0,0.0],
'Value2' : [0.0,2.0,3.0,4.0,0.0,0.0,0.0,7.0,8.0,4.0,0.0,0.0,3.0,9.0,1.0,0.0]
})
Run Code Online (Sandbox Code Playgroud)
结果是这样的:
ActivityDateTime Value1 Value2
0 2016-05-13 06:14:00 0.0 0.0
1 2016-05-13 06:16:00 2.0 2.0
2 2016-05-13 06:20:00 3.0 3.0
3 2016-05-13 06:27:00 4.0 4.0
4 2016-05-13 06:31:00 0.0 0.0
5 2016-05-13 06:32:00 0.0 0.0
6 2016-05-13 17:34:00 0.0 0.0
7 2016-05-13 17:36:00 7.0 7.0
8 2016-05-13 17:38:00 8.0 8.0
9 2016-05-13 17:45:00 4.0 4.0 …Run Code Online (Sandbox Code Playgroud) 我已经在网上搜了一个多小时,找不到我需要的东西.
我有两列包含人名; Contact和Created By.两者的格式相同.
基本上我需要计算这两列组合的不同值.例如,名称可以在每列数据中多次,但我只希望名称计数一次.
我尝试使用下面的内容,但它返回的数字高于两列之间的实际不同值.
=Sum(Aggr(Count(Distinct [Created By]),[Contact]))
Run Code Online (Sandbox Code Playgroud)
也尝试了这个和上面返回的相同数字.
=Count(Distinct [Contact] & [Created By])
Run Code Online (Sandbox Code Playgroud)
提前致谢!
所以dask现在已经更新,以支持groupby的自定义聚合功能.(感谢开发团队和@chmp的工作!).我目前正在尝试构建一个模式函数和相应的计数函数.基本上我所设想的是,模式为每个分组返回特定列的最常见值的列表(即[4,1,2]).此外,还有一个相应的计数函数,它返回这些值的实例数,即.3.
现在我正在尝试在代码中实现它.根据groupby.py文件,自定义聚合的参数如下:
Parameters
----------
name : str
the name of the aggregation. It should be unique, since intermediate
result will be identified by this name.
chunk : callable
a function that will be called with the grouped column of each
partition. It can either return a single series or a tuple of series.
The index has to be equal to the groups.
agg : callable
a function that will be called to aggregate the results of each chunk.
Again the …Run Code Online (Sandbox Code Playgroud) 为什么在rolling多索引DataFrame 时不能使用偏移量? 例如,使用:
rng = pd.date_range('2017-01-03', periods=20, freq='8D')
i = pd.MultiIndex.from_product([['A','B','C'], rng], names=['Name','Date'])
df = pd.DataFrame(np.random.randn(60), i, columns=['Vals'])
Run Code Online (Sandbox Code Playgroud)
如果我尝试使用偏移量进行分组和滚动,则会出现“ ValueError:窗口必须为整数 ”:
df['Avg'] = df.groupby(['Name'])['Vals'].rolling('30D').mean() # << Why doesn't this work?
Run Code Online (Sandbox Code Playgroud)
并不是以下这些变体可以满足我的需求,但请注意对int作品进行分组和滚动:
df['Avg'] = df.groupby(['Name'])['Vals'].rolling(4).mean()
Run Code Online (Sandbox Code Playgroud)
而且我可以在DataFrame的单索引子集上使用偏移量滚动:
d = df.loc['A']
d['Avg'] = d['Vals'].rolling('30D').mean()
Run Code Online (Sandbox Code Playgroud)
如果确实不可能在多索引DataFrame上进行偏移滚动,那么将零应用于每个级别0索引项的最有效的解决方法是什么?
我正在尝试创建同一字段的多个聚合。我正在python3.7中的熊猫中工作。根据文档,语法似乎非常简单:
https://pandas-docs.github.io/pandas-docs-travis/user_guide/groupby.html#named-aggregation
我看不到为什么在下面出现错误。有人可以指出问题并告诉我如何解决吗?
码:
qt_dy.groupby('date').agg(std_qty=('qty','std'),mean_qty=('qty','mean'),)
Run Code Online (Sandbox Code Playgroud)
错误:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-62-6bb3aabf313f> in <module>
5
6 qt_dy.groupby('date')\
----> 7 .agg(std_qty=('qty','std'),mean_qty=('qty','mean'))
TypeError: aggregate() missing 1 required positional argument: 'arg'
Run Code Online (Sandbox Code Playgroud) aggregate ×10
python ×4
pandas ×3
dataframe ×2
group-by ×2
r ×2
sql ×2
analysis ×1
android ×1
c# ×1
count ×1
dask ×1
database ×1
dplyr ×1
duplicates ×1
dynamic-linq ×1
linq ×1
multi-index ×1
postgresql ×1
python-3.x ×1
qlikview ×1
time-series ×1