具有列特定聚合功能的Pandas df.resample [英] Pandas df.resample with column-specific aggregation function

查看:232
本文介绍了具有列特定聚合功能的Pandas df.resample的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 pandas.DataFrame.resample 我可以对DataFrame进行下采样:

With pandas.DataFrame.resample I can downsample a DataFrame:

df.resample("3s", how="mean")

这将使用类似于日期时间的索引对数据帧进行重新采样,以使3秒内的所有值都汇总到一行中.列中的值是平均值.

This resamples a data frame with a datetime-like index such that all values within 3 seconds are aggregated into one row. The values of the columns are averaged.

问题:我有一个包含多列的数据框.是否可以为不同的列指定不同的聚合函数,例如我要"sum"x"mean"y并为z列选择"last"?我怎样才能达到那个效果?

Question: I have a data frame with multiple columns. Is it possible to specify a different aggregation function for different columns, e.g. I want to "sum" column x, "mean" column y and pick the "last" for column z? How can I achieve that effect?

我知道我可以创建一个新的空数据框,然后调用resample 3次,但是我希望有一个更快的就地解决方案.

I know I could create a new empty data frame, and then call resample three times, but I would prefer a faster in-place solution.

推荐答案

您可以使用

You can use .agg after resample. With a dictionary, you can aggregate different columns with various functions.

尝试一下:

df.resample("3s").agg({'x':'sum','y':'mean','z':'last'})

此外,不推荐使用how:

C:\ Program Files \ Anaconda3 \ lib \ site-packages \ ipykernel__main __.py:1: FutureWarning:在.resample()中如何弃用新的语法是 .resample(...).mean()

C:\Program Files\Anaconda3\lib\site-packages\ipykernel__main__.py:1: FutureWarning: how in .resample() is deprecated the new syntax is .resample(...).mean()

这篇关于具有列特定聚合功能的Pandas df.resample的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆