data struncture
col1 col2
A 2021-01-01
A 2022-01-01
B 2021-01-01
B 2022-01-01
This is a dataframe with multiindex(ts_code
, date
).
Goal
I want to get min date for ts_code
. So I have to run df.reset_index().groupby('ts_code')['date'].min()
. Is it any method not to reset index to achieve it?
CodePudding user response:
You can either rename your axis for your code to work, or you can pass level=0
into groupby()
Option 1:
df.rename_axis(['ts_code','date']).groupby('ts code')
Option 2:
df.groupby(level=0)
CodePudding user response:
You can use pandas.MultiIndex.get_level_values
df.groupby(df.index.get_level_values('ts_code'))