Pandas Df.resample With Column-specific Aggregation Function
With pandas.DataFrame.resample I can downsample a DataFrame: df.resample('3s', how='mean') This resamples a data frame with a datetime-like index such that all values within 3 sec
Solution 1:
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()
Solution 2:
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)XYZ2017-01-01 00:00:00 0.4449390.4075540.4601482017-01-01 00:00:01 0.4652390.4626910.0165452017-01-01 00:00:02 0.8504450.8177440.7779622017-01-01 00:00:03 0.7579830.9348290.8311042017-01-01 00:00:04 0.8798910.9268790.7215352017-01-01 00:00:05 0.1176420.1459060.1998442017-01-01 00:00:06 0.4375640.1007020.2787352017-01-01 00:00:07 0.6098620.0858230.8369972017-01-01 00:00:08 0.7396350.8660590.6912712017-01-01 00:00:09 0.3771850.2251460.4352802017-01-01 00:00:10 0.7009000.7009460.7964872017-01-01 00:00:11 0.0186880.7005660.9007492017-01-01 00:00:12 0.7648690.2532000.5480542017-01-01 00:00:13 0.7788830.6516760.1360972017-01-01 00:00:14 0.5448380.0350730.2750792017-01-01 00:00:15 0.7066850.7136140.7760502017-01-01 00:00:16 0.5423290.8365410.5381862017-01-01 00:00:17 0.1855230.6521510.746060
Use agg
df.resample('3S').agg(dict(X='sum',Y='mean',Z='last'))XYZ2017-01-01 00:00:00 1.7606240.5626630.7779622017-01-01 00:00:03 1.7555160.6692040.1998442017-01-01 00:00:06 1.7870610.3508610.6912712017-01-01 00:00:09 1.0967730.5422200.9007492017-01-01 00:00:12 2.0885900.3133160.2750792017-01-01 00:00:15 1.4345380.7341020.746060
Post a Comment for "Pandas Df.resample With Column-specific Aggregation Function"