question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

loc() does not swap two rows in multi-index pandas dataframe

See original GitHub issue
df = pd.DataFrame(np.arange(12).reshape((4, 3)),
    index=[['a', 'a', 'b', 'b'], [1, 2, 1, 2]],
    columns=[['Ohio', 'Ohio', 'Colorado'],
    ['Green', 'Red', 'Green']])

df.loc[['b','a'],:]  # does not swap

# df.reindex(index=['b', 'a'], level=0) # This works

Problem description

df.loc[[‘b’,‘a’],:] does not swap the rows ‘a’ and ‘b’, nor does for a Series.

Expected Output

The output should be the same as the one obtained via df.reindex(index=[‘b’, ‘a’], level=0)

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None python: 3.6.3.final.0 python-bits: 64 OS: Windows OS-release: 10 machine: AMD64 processor: Intel64 Family 6 Model 94 Stepping 3, GenuineIntel byteorder: little LC_ALL: None LANG: None LOCALE: None.None

pandas: 0.20.3 pytest: 3.2.1 pip: 10.0.1 setuptools: 36.5.0.post20170921 Cython: 0.26.1 numpy: 1.14.0 scipy: 0.19.1 xarray: None IPython: 6.1.0 sphinx: 1.6.3 patsy: 0.4.1 dateutil: 2.6.1 pytz: 2017.2 blosc: None bottleneck: 1.2.1 tables: 3.4.2 numexpr: 2.6.2 feather: None matplotlib: 2.1.0 openpyxl: 2.4.8 xlrd: 1.1.0 xlwt: 1.3.0 xlsxwriter: 1.0.2 lxml: 4.1.0 bs4: 4.6.0 html5lib: 0.999999999 sqlalchemy: 1.1.13 pymysql: None psycopg2: None jinja2: 2.10 s3fs: None pandas_gbq: None pandas_datareader: 0.7.0

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
eisthfroyalbluecommented, Sep 22, 2018

For a single-indexed table, we can reindex it using .loc() instead of .reindex()

df = pd.DataFrame([[1,2,3],[4,5,6]], index=['a','b'])

df.loc[['b','a']]

Out[9]: 
   0  1  2
b  4  5  6
a  1  2  3

In his book, “Python for Data Analysis 2ed” Wes McKinney says “you can reindex more succinctly by label-indexing with loc, and many users prefer to use it”, although he used only a single-index table for illustration.

This may be not a bug. however it is natural for one to expect the same effect for the multi-index table.

1reaction
pl-phancommented, Jul 23, 2019

I too got mixed up, by using .loc[mylist] on a multi-index dataframe, it did not preserve the order of mylist. I realized it way later than I should have.

Surely it is strange that it preserves the order for single-index, but not multi-index.

Even if it doesn’t qualifies as a bug, is it possible to add a warning ? Only when .loc[key] is used on a multi-index structure, if key is an iterable, just to warn that the order might not be preserved (and maybe recommending using .reindex(index=key, level=0) instead)

I tried to locate in the code where the warning should be written, without success. 😦

Read more comments on GitHub >

github_iconTop Results From Across the Web

swapping two rows in multi-index pandas dataframe
we can swap two rows 'a' and 'b' by calling: df.loc[['b','a'],:] Out[21]: 0 1 2 b 4 5 6 a 1 2 3....
Read more >
MultiIndex / advanced indexing — pandas 1.5.2 documentation
The rename() method is used to rename the labels of a MultiIndex , and is typically used to rename the columns of a...
Read more >
Confused by Multi-Index in Pandas? 9 Essential Operations to ...
In a spreadsheet-like table, the simplest scenario is that row numbers ... a DataFrame that has no such multi-index, but some columns can...
Read more >
Pandas DataFrame Multi Index & Groupby Tutorial - DataCamp
This can be slightly confusing because this says is that df.columns is of type Index . This does not mean that the columns...
Read more >
Assign existing column to the DataFrame index with set_index()
By using set_index() , multiple columns can be assigned as multi-index. Specify by list. By specifying a list of column names in the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found