How get monthly mean in pandas using groupby

后端 未结 1 1641
深忆病人
深忆病人 2020-12-19 14:30

I have the next DataFrame:

data=pd.read_csv(\'anual.csv\', parse_dates=\'Fecha\', index_col=0)
data

DatetimeIndex: 290 entries, 2011-01-01 00:00:00 to 2011-         


        
相关标签:
1条回答
  • 2020-12-19 15:04

    Your date column is being misinterpreted, because it's in DD/MM/YYYY format. Set dayfirst=True instead:

    >>> df = pd.read_csv('anual.csv', parse_dates='Fecha', dayfirst=True, index_col=0, sep="\s+")
    >>> df['PreciAcu']['2011-01'].count()
    31
    >>> df.resample("M").mean()
                       HR   PreciAcu    RadSolar          T  Presion       Tmax  \
    Fecha                                                                         
    2011-01-31  68.774194   0.000000  162.354839  16.535484        0  25.393548   
    2011-02-28  67.000000   0.000000  193.481481  15.418519        0  25.696296   
    2011-03-31  59.083333   0.850000  254.541667  21.295833        0  32.325000   
    2011-04-30  61.200000   1.312000  260.640000  24.676000        0  34.760000   
    2011-05-31        NaN        NaN         NaN        NaN      NaN        NaN   
    2011-06-30  68.428571   8.576190  236.619048  25.009524        0  32.028571   
    2011-07-31  81.518519  11.488889  185.407407  22.429630        0  27.681481   
    2011-08-31  76.451613   0.677419  219.645161  23.677419        0  30.719355   
    2011-09-30  77.533333   2.883333  196.100000  21.573333        0  28.723333   
    2011-10-31  73.120000   1.260000  196.280000  19.552000        0  27.636000   
    2011-11-30  71.277778 -79.333333  148.555556  18.250000        0  26.511111   
    2011-12-31  73.741935   0.067742  134.677419  15.687097        0  24.019355   
    
                    HRmax  Presionmax       Tmin  
    Fecha                                         
    2011-01-31  92.709677           0  10.909677  
    2011-02-28  92.111111           0   8.325926  
    2011-03-31  89.291667           0  13.037500  
    2011-04-30  89.400000           0  17.328000  
    2011-05-31        NaN         NaN        NaN  
    2011-06-30  92.095238           0  19.761905  
    2011-07-31  97.185185           0  18.774074  
    2011-08-31  96.903226           0  18.670968  
    2011-09-30  97.200000           0  16.373333  
    2011-10-31  97.000000           0  13.412000  
    2011-11-30  94.555556           0  11.877778  
    2011-12-31  94.161290           0  10.070968  
    
    [12 rows x 9 columns]
    

    (Note, though - I'd forgotten this -- that dayfirst=True isn't strict, see here. Maybe using date_parser would be safer.)

    0 讨论(0)
提交回复
热议问题