Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas, how to access multiIndex dataframe?

Show my code

>>> df = pd.DataFrame({'key1': ['a', 'a', 'b', 'b', 'a'], \
                   'key2': ['one', 'two', 'one', 'two', 'one'], \
                   'data1': np.random.randn(5), \
                   'data2': np.random.randn(5)})

>>> new_df = df.groupby(['key1', 'key2']).mean().unstack()
>>> print new_df
         data1               data2
key2       one       two       one       two
key1
a    -0.070742 -0.598649 -0.349283 -1.272043
b    -0.109347 -0.097627 -0.641455  1.135560 
>>> print new_df.columns
MultiIndex(levels=[[u'data1', u'data2'], [u'one', u'two']],
       labels=[[0, 0, 1, 1], [0, 1, 0, 1]],
       names=[None, u'key2'])

As you can see, the MultiIndex dataframe is different with normal dataframes, so how to access the data in the MultiIndex dataframe.

like image 350
GoingMyWay Avatar asked Apr 23 '16 04:04

GoingMyWay


People also ask

Can a Dataframe have 2 indexes?

In this example, we will be creating multi-index from dataframe using pandas. We will be creating manual data and then using pd. dataframe, we will create a dataframe with the set of data. Now using the Multi-index syntax we will create a multi-index with a dataframe.

How do you slice in MultiIndex?

You can slice a MultiIndex by providing multiple indexers. You can provide any of the selectors as if you are indexing by label, see Selection by Label, including slices, lists of labels, labels, and boolean indexers. You can use slice(None) to select all the contents of that level.

What is multi-index in pandas Dataframe and groupby?

In this article, we will discuss Multi-index for Pandas Dataframe and Groupby operations. Multi-index allows you to select more than one row and column in your index. It is a multi-level or hierarchical object for pandas object.

What are multi-level columns in pandas Dataframe?

Multi-level columns are used when you wanted to group columns together. 1. Create MultiIndex pandas DataFrame (Multi level Index) A multi-level index DataFrame is a type of DataFrame that contains multiple level or hierarchical indexing. You can create a MultiIndex (multi-level index) in the following ways.

How to convert/transfer multiindex (multi-level Index) indexes to columns in pandas Dataframe?

Use pandas DataFrame.reset_index () function to convert/transfer MultiIndex (multi-level index) indexes to columns. The default setting for the parameter is drop=False which will keep the index values as columns and set the new index to DataFrame starting from zero. Yields below output.

What is a multi-index Dataframe?

However, things can get really hairy when multi-index dataframes are involved. A multi-index (also known as hierarchical index) dataframe uses more than one column as the index of the dataframe. A multi-index dataframe allows you to store your data in multi-dimension format, and opens up a lot of exciting to represent your data.


1 Answers

Accessing data in multiindex dataframe is similar to the way on a general dataframe. For example, if you want to read data at (a, data1.two), you can simply do: new_df['data1']['two']['a'] or new_df.loc['a', ('data1', 'two')]

Please read the official docs for more details.

like image 76
Zhenhao Chen Avatar answered Oct 22 '22 17:10

Zhenhao Chen