Search code examples
pythonpandasanalysis

How to Find Year-wise Mean from Date-wise CSV Data In Pandas For Plotting bar chart


I have Sample Data as

Company,Date,Open,High,Low,Close,Adj Close,Volume
ADANIPORTS,5/6/2008,150,153.570007,147.820007,151.149994,134.313477,1782030
ADANIPORTS,5/7/2008,152,154.460007,150.240005,153.309998,136.232864,1180015
ADANIPORTS,5/8/2008,152.19996.759995,150.199997,155.889999,138.525497,1856245
ADANIPORTS,5/9/2008,155,160.600006,154.210007,156.520004,139.085312,3251375

For Different Company and Till 2018. Now, I want to Find Mean Of Open and Close Year-wise To plot Bar-Chart


Solution

  • this will help you groupby year and take the means:

    df.groupby(pd.Grouper(key='date', freq='Y'))['Open','Close'].mean()
    

    otherwise you can resample method:

    df.set_index('date').resample('Y')['Open','Close'].mean()