Search code examples
pythonpandasplotlyaveragemean

how to take the average for certain rows (python)


As you can see in the outcome picture there is a column called 'dt', for every month in the year the temperature of lands/sea is measured. But i want to make the graph shorter so i want to take the average of the 12 months and make the measurements yearly and not monthly.

this is the outcome:

This is what i want:what i want


Solution

  • You can use:

    import pandas as pd
    import matplotlib.pyplot as plt
    
    df = pd.read_csv('GlobalTemperatures.csv', parse_dates=['dt'])
    out = df.groupby(df['dt'].dt.year).mean()
    
    out.plot()
    plt.show()
    

    Output:

    >>> out
          LandAverageTemperature  LandAverageTemperatureUncertainty  LandMaxTemperature  ...  LandMinTemperatureUncertainty  LandAndOceanAverageTemperature  LandAndOceanAverageTemperatureUncertainty
    dt                                                                                   ...                                                                                                          
    1750                8.719364                           2.637818                 NaN  ...                            NaN                             NaN                                        NaN
    1751                7.976143                           2.781143                 NaN  ...                            NaN                             NaN                                        NaN
    1752                5.779833                           2.977000                 NaN  ...                            NaN                             NaN                                        NaN
    1753                8.388083                           3.176000                 NaN  ...                            NaN                             NaN                                        NaN
    1754                8.469333                           3.494250                 NaN  ...                            NaN                             NaN                                        NaN
    ...                      ...                                ...                 ...  ...                            ...                             ...                                        ...
    2011                9.516000                           0.082000           15.284833  ...                       0.136583                       15.769500                                   0.059000
    2012                9.507333                           0.083417           15.332833  ...                       0.145333                       15.802333                                   0.061500
    2013                9.606500                           0.097667           15.373833  ...                       0.149833                       15.854417                                   0.064667
    2014                9.570667                           0.090167           15.313583  ...                       0.139000                       15.913000                                   0.063167
    2015                9.831000                           0.092167           15.572667  ...                       0.141750                       16.058583                                   0.060833
    
    [266 rows x 8 columns]
    

    enter image description here