0

The code below gets the mean,median,max,min as a pandas table. I just want to print the Average portion of the table without the dates. How would I be able to do that and get the expected output?

import numpy as np
import pandas as pd
from pandas import DataFrame

date_list = ['2019-09-01 00:00:00', '2019-10-01 00:00:00', '2019-11-01 00:00:00',
 '2019-12-01 00:00:00', '2020-01-01 00:00:00', '2020-02-01 00:00:00', 
 '2020-03-01 00:00:00', '2020-04-01 00:00:00', '2020-05-01 00:00:00', 
 '2020-06-01 00:00:00', '2020-07-01 00:00:00', '2020-08-01 00:00:00',
 '2020-09-01 00:00:00','2020-10-01 00:00:00', '2020-11-01 00:00:00', 
 '2020-12-01 00:00:00','2021-01-01 00:00:00','2021-02-01 00:00:00', '2021-03-01 00:00:00', 
 '2021-04-01 00:00:00','2021-05-01 00:00:00', '2021-06-01 00:00:00', 
 '2021-07-01 00:00:00']
monthly_values = np.array([ 15., 39.6, 0.2, 34.3, 19.6, 26.8, 15.7, 26., 12.6, 15.5, 18.6, 2.3, 6.5,
   2.5, 12.2, 11.6, 93.9, 25.5, 26.5, -16.5, -1.4, -1.8, 5.])

data = pd.DataFrame({"Date": date_list, "Averages": monthly_values})
data["Date"] = pd.to_datetime(data["Date"])
out=(data.groupby(data["Date"].dt.year)
     .agg(['mean','median','max','min'])
     .droplevel(0,1)
     .rename(columns=lambda x:'Average' if x=='mean' else x.title()))
print(out['Average'])

Output:

Date
2019    22.275000
2020    14.158333
2021    18.742857

Expected Output:

22.275000
14.158333
18.742857

1 Answer 1

2

You have two columns named "Average", so print(out["Average"]) prints both of them. To print just values from the second one:

print(*out["Average"].iloc[:, 1], sep="\n")

Prints:

22.275
14.158333333333333
18.742857142857144
Sign up to request clarification or add additional context in comments.

3 Comments

It gives me an error IndexingError: Too many indexers
@georgehere I used the script you have in your question. Can you post the data that gives you this error?
hello there sorry for the delayed response I am using the same thing too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.