I want to get both levels of a multi-index dataframe so I can re-index a dataframe based on this.
Take a dataframe:
import pandas as pd
import numpy as np
dates = pd.date_range('20070101',periods=3200)
df = pd.DataFrame(data=np.random.randint(0,100,(3200,1)), columns =list('A'))
df['date'] = dates
df = df[['date','A']]
Apply season function to the datetime index
def get_season(row):
if row['date'].month >= 3 and row['date'].month <= 5:
return '2'
elif row['date'].month >= 6 and row['date'].month <= 8:
return '3'
elif row['date'].month >= 9 and row['date'].month <= 11:
return '4'
else:
return '1'
Apply the function
df['Season'] = df.apply(get_season, axis=1)
Create a 'Year' column for indexing
df['Year'] = df['date'].dt.year
Multi-index by Year and Season
df = df.set_index(['Year', 'Season'], inplace=False)
Group the data
df2 = df['A'].groupby(level=['Year','Season']).mean()
When I query this for the first level:
df2.index.get_level_values(0)
I get the years:
Out[4]:
Int64Index([2007, 2007, 2007, 2007, 2008, 2008, 2008, 2008, 2009, 2009, 2009,
2009, 2010, 2010, 2010, 2010, 2011, 2011, 2011, 2011, 2012, 2012,
2012, 2012, 2013, 2013, 2013, 2013, 2014, 2014, 2014, 2014, 2015,
2015, 2015, 2015],
dtype='int64', name=u'Year')
and the second level:
df2.index.get_level_values(1)
I get the seasons:
Out[6]:
Index([u'1', u'2', u'3', u'4', u'1', u'2', u'3', u'4', u'1', u'2', u'3', u'4',
u'1', u'2', u'3', u'4', u'1', u'2', u'3', u'4', u'1', u'2', u'3', u'4',
u'1', u'2', u'3', u'4', u'1', u'2', u'3', u'4', u'1', u'2', u'3', u'4'],
dtype='object', name=u'Season')
But I want both years and seasons associated with eachother - so I can re-index a dataframe based on both year and season, both levels of the multi-index.
That is - I want ([2007;1 , 2007;2 , 2007;3])
etc.
Is this possible to do? Thanks.
>>> df2.index.tolist()
[(2007, '1'),
(2007, '2'),
(2007, '3'),
(2007, '4'),
(2008, '1'),
(2008, '2'),
(2008, '3'),
(2008, '4'),
...
]
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.