I have the following dataframe that I'm wanting to groupby year and return the max value (but keep the index values as they are here):
import pandas as pd
dct = {
"date": ["2019-01-01", "2019-04-1", "2020-01-01"],
"high": [100, 150, 100],
}
df = pd.DataFrame(dct)
df.set_index("date",inplace=True)
df.index = [pd.Timestamp(i) for i in df.index]
df.index.name = "date"
# date high
# 2019-01-01 100
# 2019-04-01 150
# 2020-01-01 100
When using pandas groupby, Im able to group them by year, but not get the date that I want:
func = lambda x: x.year
df["high"].groupby(func).max()
# date high
# 2019 150
# 2020 100
My desired output is to use pandas groupby and get:
# NOTE : the date index is like the original
# date high
# 2019-04-01 150
# 2020-01-01 100