You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In presence of a MultiIndex, rename() should support the tupelized representation of a column.
df = pd.DataFrame([[1,2,3],[3,4,5],[5,6,7], [7,8,9]])
df.columns = pd.MultiIndex.from_tuples([('i','a'),('i','b'),('ii','a')])
# Rename using tuples:
df.rename(columns={('i','b'):('ii','b')})
# Rename using current column name (equivalent to 1a)
col = df[('i','b')]
df.rename(columns={col.name: ('ii','b')})
This would improve the consistency of the API, since many methods accept a tuple to address the name of a multi-index element:
@jreback Is it possible that you misinterpreted my question? It's not about names of index levels. It's about the indices (=name of column). I don't see how the below could be done with set_names nor how this is related to #20421.
# Input:
i ii
a b a
0 1 2 3
1 3 4 5
2 5 6 7
3 7 8 9
# Output: same df, except that one column has different index
i ii
a c a
0 1 2 3
1 3 4 5
2 5 6 7
3 7 8 9
It's currently quite difficult to rename a single column (or row) in a multi-indexed DataFrame. The problem is illustrated also in this SO article.
The current approach requires at least 3 steps:
Enhancement:
In presence of a MultiIndex,
rename()
should support the tupelized representation of a column.This would improve the consistency of the API, since many methods accept a tuple to address the name of a multi-index element:
API breaking implications
I regard this improvement as an extension of the current API.
The text was updated successfully, but these errors were encountered: