Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我可以从索引和组列表中重建GroupByObject吗?_Python_Pandas_Dataframe_Group By - Fatal编程技术网

Python 我可以从索引和组列表中重建GroupByObject吗?

Python 我可以从索引和组列表中重建GroupByObject吗?,python,pandas,dataframe,group-by,Python,Pandas,Dataframe,Group By,我有一个数据帧,其中索引是间隔1分钟的时间戳。我执行Groupby将数据分为10分钟窗口组: compression = datetime.timedelta(minutes=10) grouped_df = df.groupby(pd.Grouper(freq=compression)) 问题是pd.Grouper生成10分钟的组,其中任何一个组都可能不包含任何数据,这意味着该组为空 当我现在尝试聚合数据时,我收到一个错误: attribute_df[column_names] = grou

我有一个数据帧,其中索引是间隔1分钟的时间戳。我执行
Groupby
将数据分为10分钟窗口组:

compression = datetime.timedelta(minutes=10)
grouped_df = df.groupby(pd.Grouper(freq=compression))
问题是pd.Grouper生成10分钟的组,其中任何一个组都可能不包含任何数据,这意味着该组为空

当我现在尝试聚合数据时,我收到一个错误:

attribute_df[column_names] = grouped_df.agg(lambda x: np.log10(x[9] / x[0]))
错误:

发生异常:索引器错误 索引9超出大小为0的轴0的界限

GroupBy
中是否有忽略空组的方法?或者在
Agg
功能中跳过空组?我不希望在lambda函数中检查组大小,因为这会大大降低此聚合函数和其他聚合函数的速度。我不想手动迭代每个组

有什么建议吗?

您可以尝试:

# don't forget the column name
attribute_df[column_names] = grouped_df['Col_Name'].agg(lambda x: np.log10(x[9] / x[0]) if len(x) else np.NaN)

谢谢,虽然我在问题的最后提到了这一点,并试图避免lambda中的条件,但我猜使用lambda表达式已经违反了熊猫的“合同”。谢谢