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
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.