Pandas df.resample with column-specific aggregation function Pandas df.resample with column-specific aggregation function pandas pandas

Pandas df.resample with column-specific aggregation function


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

Try this:

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

Also, how is deprecated:

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


Consider the dataframe df

np.random.seed([3,1415])tidx = pd.date_range('2017-01-01', periods=18, freq='S')df = pd.DataFrame(np.random.rand(len(tidx), 3), tidx, list('XYZ'))print(df)                            X         Y         Z2017-01-01 00:00:00  0.444939  0.407554  0.4601482017-01-01 00:00:01  0.465239  0.462691  0.0165452017-01-01 00:00:02  0.850445  0.817744  0.7779622017-01-01 00:00:03  0.757983  0.934829  0.8311042017-01-01 00:00:04  0.879891  0.926879  0.7215352017-01-01 00:00:05  0.117642  0.145906  0.1998442017-01-01 00:00:06  0.437564  0.100702  0.2787352017-01-01 00:00:07  0.609862  0.085823  0.8369972017-01-01 00:00:08  0.739635  0.866059  0.6912712017-01-01 00:00:09  0.377185  0.225146  0.4352802017-01-01 00:00:10  0.700900  0.700946  0.7964872017-01-01 00:00:11  0.018688  0.700566  0.9007492017-01-01 00:00:12  0.764869  0.253200  0.5480542017-01-01 00:00:13  0.778883  0.651676  0.1360972017-01-01 00:00:14  0.544838  0.035073  0.2750792017-01-01 00:00:15  0.706685  0.713614  0.7760502017-01-01 00:00:16  0.542329  0.836541  0.5381862017-01-01 00:00:17  0.185523  0.652151  0.746060

Use agg

df.resample('3S').agg(dict(X='sum', Y='mean', Z='last'))                            X         Y         Z2017-01-01 00:00:00  1.760624  0.562663  0.7779622017-01-01 00:00:03  1.755516  0.669204  0.1998442017-01-01 00:00:06  1.787061  0.350861  0.6912712017-01-01 00:00:09  1.096773  0.542220  0.9007492017-01-01 00:00:12  2.088590  0.313316  0.2750792017-01-01 00:00:15  1.434538  0.734102  0.746060