How to resample a dataframe with different functions applied to each column?

后端 未结 4 1362
旧巷少年郎
旧巷少年郎 2020-12-07 16:04

I have a times series with temperature and radiation in a pandas dataframe. The time resolution is 1 minute in regular steps.

import datetime
im         


        
4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-07 16:48

    With pandas 0.18 the resample API changed (see the docs). So for pandas >= 0.18 the answer is:

    In [31]: frame.resample('1H').agg({'radiation': np.sum, 'tamb': np.mean})
    Out[31]: 
                             tamb   radiation
    2012-04-05 08:00:00  5.161235  279.507182
    2012-04-05 09:00:00  4.968145  290.941073
    2012-04-05 10:00:00  4.478531  317.678285
    2012-04-05 11:00:00  4.706206  335.258633
    2012-04-05 12:00:00  2.457873    8.655838
    

    Old Answer:

    I am answering my question to reflect the time series related changes in pandas >= 0.8 (all other answers are outdated).

    Using pandas >= 0.8 the answer is:

    In [30]: frame.resample('1H', how={'radiation': np.sum, 'tamb': np.mean})
    Out[30]: 
                             tamb   radiation
    2012-04-05 08:00:00  5.161235  279.507182
    2012-04-05 09:00:00  4.968145  290.941073
    2012-04-05 10:00:00  4.478531  317.678285
    2012-04-05 11:00:00  4.706206  335.258633
    2012-04-05 12:00:00  2.457873    8.655838
    

提交回复
热议问题