How to access multi-level index in pandas data frame?

2024/10/10 20:17:15

I would like to call those row with same index.

so this is the example data frame,

arrays = [np.array(['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux']),
np.array(['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two'])]df = pd.DataFrame(np.random.randn(8, 4), index=arrays)In [16]: df
Out[16]: 0         1         2         3
bar one -0.424972  0.567020  0.276232 -1.087401two -0.673690  0.113648 -1.478427  0.524988
baz one  0.404705  0.577046 -1.715002 -1.039268two -0.370647 -1.157892 -1.344312  0.844885
foo one  1.075770 -0.109050  1.643563 -1.469388two  0.357021 -0.674600 -1.776904 -0.968914
qux one -1.294524  0.413738  0.276662 -0.472035two -0.013960 -0.362543 -0.006154 -0.923061

I would like to select

                0         1         2         3
bar one -0.424972  0.567020  0.276232 -1.087401
baz one  0.404705  0.577046 -1.715002 -1.039268
foo one  1.075770 -0.109050  1.643563 -1.469388
qux one -1.294524  0.413738  0.276662 -0.472035

or even as this format

            0         1         2         3
one -0.424972  0.567020  0.276232 -1.087401
one  0.404705  0.577046 -1.715002 -1.039268
one  1.075770 -0.109050  1.643563 -1.469388
one -1.294524  0.413738  0.276662 -0.472035

I have tried df['bar','one] and it's not working. I am now sure how should I access the multi-level index.

Answer

You can use MultiIndex slicing (use slice(None) instead of colon):

df = df.loc[(slice(None), 'one'), :]

Result:

                0         1         2         3
bar one -0.424972  0.567020  0.276232 -1.087401
baz one  0.404705  0.577046 -1.715002 -1.039268
foo one  1.075770 -0.109050  1.643563 -1.469388
qux one -1.294524  0.413738  0.276662 -0.472035

Finally you can drop the first index column:

df.index = df.index.droplevel(0)

Result:

            0         1         2         3
one -0.424972  0.567020  0.276232 -1.087401
one  0.404705  0.577046 -1.715002 -1.039268
one  1.075770 -0.109050  1.643563 -1.469388
one -1.294524  0.413738  0.276662 -0.472035
https://en.xdnf.cn/q/69850.html

Related Q&A

How to represent graphs with IPython

Recently I discovered IPython notebook which is a powerful tool. As an IT student, I was looking for a way to represent graphs in Python. For example, I would like to know if theres a library (like num…

How to check TypeVars Type at runtime

I have a generic class Graph[Generic[T], object]. My question, is there any function which returns type passed as generic to the class Graph>>> g = Graph[int]() >>> magic_func(g) <…

Can django-pagination do multiple paginations per page?

If it cant then are there any other alternatives (either Djangos native pagination or an alternate package) that allows multiple paginations per page?I would like to display a list of about 5 objects …

List directory file contents in a Django template

Im just learning Python & Django. (Thanks to everyone who contributes here -- its been an invaluable resource!)One seemingly basic thing that Im having trouble with is rendering a simple list of s…

Produce PDF files, draw polygons with rounded corners

Whats the right tool for the job if I want to write a Python script that produces vector graphics in PDF format? In particular, I need to draw filled polygons with rounded corners (i.e., plane figures…

How can I unit test this Flask app?

I have a Flask app that is using Flask-Restless to serve an API.I have just written some authentication that checksIf the consumers host is recognised The request includes a hash (calculated by encrypt…

How to completely reset Python stdlib logging module in an ipython session?

Id like to make repeated calls to Python scripts using %run in an ipython session, and for each of those scripts to log based on cmdline arguments passed via %run.For example while debugging cmd.py I m…

I think Librosa.effect.split has some problem?

firstly, this function is to remove silence of an audio. here is the official description:https://librosa.github.io/librosa/generated/librosa.effects.split.htmllibrosa.effects.split(y, top_db=10, *karg…

Replace None in list with leftmost non none value

Givena = [None,1,2,3,None,4,None,None]Id likea = [None,1,2,3,3,4,4,4]Currently I have brute forced it with:def replaceNoneWithLeftmost(val):last = Noneret = []for x in val:if x is not None:ret.append(x…

Generating lists/reports with in-line summaries in Django

I am trying to write a view that will generate a report which displays all Items within my Inventory system, and provide summaries at a certain point. This report is purely just an HTML template by the…