- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我正在使用我在工作中使用了很长时间的聚合函数。这个想法是,如果传递给函数的系列长度为 1(即该组只有一个观察值),则返回该观察值。如果传递的 Series 的长度大于 1,则观察结果以列表形式返回。
这对某些人来说可能看起来很奇怪,但这不是 X、Y 问题,我有充分的理由想要做与这个问题无关的事情。
这是我一直在使用的功能:
def MakeList(x):
""" This function is used to aggregate data that needs to be kept distinc within multi day
observations for later use and transformation. It makes a list of the data and if the list is of length 1
then there is only one line/day observation in that group so the single element of the list is returned.
If the list is longer than one then there are multiple line/day observations and the list itself is
returned."""
L = x.tolist()
if len(L) > 1:
return L
else:
return L[0]
现在由于某种原因,使用我正在处理的当前数据集,我得到一个 ValueError ,指出该函数没有减少。这是一些测试数据和我正在使用的剩余步骤:
import pandas as pd
DF = pd.DataFrame({'date': ['2013-04-02',
'2013-04-02',
'2013-04-02',
'2013-04-02',
'2013-04-02',
'2013-04-02',
'2013-04-02',
'2013-04-02',
'2013-04-02',
'2013-04-02'],
'line_code': ['401101',
'401101',
'401102',
'401103',
'401104',
'401105',
'401105',
'401106',
'401106',
'401107'],
's.m.v.': [ 7.760,
25.564,
25.564,
9.550,
4.870,
7.760,
25.564,
5.282,
25.564,
5.282]})
DFGrouped = DF.groupby(['date', 'line_code'], as_index = False)
DF_Agg = DFGrouped.agg({'s.m.v.' : MakeList})
在尝试调试时,我将打印语句置于 print L
和 print x.index
和输出如下:
[7.7599999999999998, 25.564]
Int64Index([0, 1], dtype='int64')
[7.7599999999999998, 25.564]
Int64Index([0, 1], dtype='int64')
由于某种原因,似乎 agg
将 Series 两次传递给函数。据我所知,这根本不正常,估计是我的功能没有减少的原因。
例如,如果我写一个这样的函数:
def test_func(x):
print x.index
return x.iloc[0]
这运行没有问题,打印语句是:
DF_Agg = DFGrouped.agg({'s.m.v.' : test_func})
Int64Index([0, 1], dtype='int64')
Int64Index([2], dtype='int64')
Int64Index([3], dtype='int64')
Int64Index([4], dtype='int64')
Int64Index([5, 6], dtype='int64')
Int64Index([7, 8], dtype='int64')
Int64Index([9], dtype='int64')
这表明每个组只作为一个系列传递给函数一次。
谁能帮我理解为什么会失败?我已经在我使用的许多数据集中成功地使用了这个函数......
谢谢
最佳答案
我无法真正解释为什么,但根据我的经验 pandas.DataFrame
中的 list
并不能很好地工作。
我通常使用 tuple
代替。这将起作用:
def MakeList(x):
T = tuple(x)
if len(T) > 1:
return T
else:
return T[0]
DF_Agg = DFGrouped.agg({'s.m.v.' : MakeList})
date line_code s.m.v.
0 2013-04-02 401101 (7.76, 25.564)
1 2013-04-02 401102 25.564
2 2013-04-02 401103 9.55
3 2013-04-02 401104 4.87
4 2013-04-02 401105 (7.76, 25.564)
5 2013-04-02 401106 (5.282, 25.564)
6 2013-04-02 401107 5.282
关于python - Pandas Groupby Agg 函数不减少,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27439023/
我有两种方法来汇总数据。 首先,我过滤mysql数据并进行汇总 其次,i aggs将数据过滤为aggs。 如下: 我发现了不同的结果,我不知道为什么。 有人可以解释吗? 最佳答案 从Docs By d
在一个具体问题上,假设我有一个 DataFrame DF word tag count 0 a S 30 1 the S 20 2 a T
我正在使用Python进行数据分析,但我遇到了部分CH的问题。9(数据聚合和分组操作)部分,介绍“使用函数分组”。。具体地说,如果我使用GroupBy对象方法或Numpy定义的函数,一切都会正常工作。
接听this question原来 df.groupby(...).agg(set) 和 df.groupby(...).agg(lambda x: set(x)) 正在产生不同的结果。 数据: df
如何在这样的字符串列中执行最大值? dataframe = pandas.DataFrame.from_dict( { "DEP
我有一个查询,用于查询给定日期时间窗口(即2017-02-17T15:00:00.000和2017-02-17T16:00:00.000之间)中的条目数。执行此查询时,我得到的结果不正确(最好说结果是
我正在尝试进行一些聚合查询并遇到一些问题。 GET /my_index/_search { "size" : 0, "aggs":{ "group_by":{ "terms": {
对于pandas agg,有没有办法根据数据类型指定聚合函数?例如,对象类型的所有列都获得“第一”,所有 float 获得“平均值”,等等?以避免必须输入所有列及其各自的聚合函数。 示例数据: imp
这是我当前的代码: pipe_exec_df_final_grouped = pipe_exec_df_final.groupBy("application_id").agg(collect_list
我有一个简单的 dataframe (df),如下所示: index Job Person 1 j1 Cathy 2 j2 Mark 3 j3 Cathy 4
我正在尝试对术语(count_bucket)进行AVG计数,但是出现错误: "buckets_path must reference either a number value or a single
我正在执行弹性查询并使用 REST 调用读取 java 代码中的响应。 当我阅读响应时,字段的顺序 - 200、204、4xx、5xx 不会按照响应中的顺序返回。 在下面找到示例请求 GET appl
我希望对文档下的属性值进行 Max 聚合,该属性是复杂对象(键和值)的列表。这是我的数据: [{ "id" : "1", "listItems" : [
我使用 Elasticsearch 来存储我的生物数据。 我尝试使用过滤后的 aggs 进行查询,但返回的数据不是我想要的。 问题来自这样一个事实,即我为每个样本都有一个“d_”属性,它是一个数组。我
当我尝试运行此查询时,elasticsearch无法回答,并且发生大量缓存逐出(与字段缓存有关)。 我不想在此查询中缓存任何字段,因为这是一个分析查询,我每天只运行一次。有什么办法可以在不使用字段缓存
我想将 DataFrame.agg 的输出转换为一个系列,其中索引是列名称和 agg 函数名称的组合。 看我有 In [132]: df = pd.DataFrame({ ...:
我想根据索引的第二级对具有多重索引的数据帧应用不同的函数。 例如,对于数据框: In [4]: df = pd.DataFrame({'a': [1,2,6,7],'b': [7,1,4,5]}, i
假设我有这样的代码: meanData = all_data.groupby(['Id'])[features].agg('mean') 这按'Id' 值对数据进行分组,选择所需的特征,并通过计算的'
下面是我的数据框的一个小样本,它有 25000 奇数行长: In [58]: df Out[58]: Send_Agent Send_Amount 0 ADR000264 361
假设我有一个 pandas dataFrame (data_stores) 类似于以下内容: store| item1 | item2 | item3 ------------------------
我是一名优秀的程序员,十分优秀!