Resampling Within a Pandas MultiIndex

pd.Grouper allows you to specify a “groupby instruction for a target object”. In particular, you can use it to group by dates even if df.index is not a DatetimeIndex: df.groupby(pd.Grouper(freq=’2D’, level=-1)) The level=-1 tells pd.Grouper to look for the dates in the last level of the MultiIndex. Moreover, you can use this in conjunction with … Read more

pandas dataframe select columns in multiindex [duplicate]

There is a get_level_values method that you can use in conjunction with boolean indexing to get the the intended result. In [13]: df = pd.DataFrame(np.random.random((4,4))) df.columns = pd.MultiIndex.from_product([[1,2],[‘A’,’B’]]) print df 1 2 A B A B 0 0.543980 0.628078 0.756941 0.698824 1 0.633005 0.089604 0.198510 0.783556 2 0.662391 0.541182 0.544060 0.059381 3 0.841242 0.634603 0.815334 … Read more

Benefits of panda’s multiindex?

Hierarchical indexing (also referred to as “multi-level” indexing) was introduced in the pandas 0.4 release. This opens the door to some quite sophisticated data analysis and manipulation, especially for working with higher dimensional data. In essence, it enables you to effectively store and manipulate arbitrarily high dimension data in a 2-dimensional tabular structure (DataFrame), for … Read more

Nested dictionary to multiindex dataframe where dictionary keys are column labels

Pandas wants the MultiIndex values as tuples, not nested dicts. The simplest thing is to convert your dictionary to the right format before trying to pass it to DataFrame: >>> reform = {(outerKey, innerKey): values for outerKey, innerDict in dictionary.iteritems() for innerKey, values in innerDict.iteritems()} >>> reform {(‘A’, ‘a’): [1, 2, 3, 4, 5], (‘A’, … Read more