-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
set_levels for MultiIndex columns with blank strings #16214
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
A bit simpler of an example: In [44]: columns = pd.MultiIndex([['a', 'b', 'c'], ['', '', 'c1']], [[0, 1, 2], [1, 0, 2]])
In [45]: pd.DataFrame(columns=columns).reset_index() Presumably because your |
@TomAugspurger |
BTW, import pandas as pd
def rename_columns(df, template):
for i, columns in enumerate(df.columns.levels):
columns = columns.tolist()
for j, row in enumerate(columns):
if template in row:
columns[j] = ""
df.columns.set_levels(columns, level=i, inplace=True)
df = pd.DataFrame([[1,2,3], [10,20,30]])
df.columns = pd.MultiIndex.from_tuples([("a", "a1"), ("b", "b1"), ("c", "c1")])
rename_columns(df, "a1")
df["d"] = 1
df works well, but df = pd.DataFrame([[1,2,3], [10,20,30]])
df.columns = pd.MultiIndex.from_tuples([("a", "a1"), ("b", ""), ("c", "c1")])
rename_columns(df, "a1")
df["d"] = 1
df returns
Is the reason is the same? |
I don't recall if / where it's enforced. In the docstring for MI, we do say
In general, directly manipulating the labels / levels can get you into sticky situations. And as you say, it's not just that there are duplicates. It seems like the empty string matters. |
xref #11424 - empty string MI labels sometimes get special treatment, guessing it could be related. |
cc @toobaz |
Hi everyone ! This issue is very old and is still open. Is the problem still relevant ? Should we label it asking for help ? |
Uh oh!
There was an error while loading. Please reload this page.
I want to rename column in MultiIndex with
and
reset_index()
This code
works well
But if I use
[("a", "a1"), ("b", ""), ("c", "c1")]
istead of[("a", "a1"), ("b", "b1"), ("c", "c1")]
fro columns namesthe code returns
Output of
pd.show_versions()
The text was updated successfully, but these errors were encountered: