Open
Description
I want to rename column in MultiIndex with
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)
and reset_index()
This code
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.reset_index()
works well
But if I use [("a", "a1"), ("b", ""), ("c", "c1")]
istead of [("a", "a1"), ("b", "b1"), ("c", "c1")]
fro columns names
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.reset_index()
the code returns
C:\Users\USER\AppData\Local\Continuum\Miniconda2\lib\site-packages\pandas\formats\format.pyc in to_string(self)
565 text = info_line
566 else:
--> 567 strcols = self._to_str_columns()
568 if self.line_width is None: # no need to wrap around just print
569 # the whole frame
C:\Users\USER\AppData\Local\Continuum\Miniconda2\lib\site-packages\pandas\formats\format.pyc in _to_str_columns(self)
488 if self.header:
489 stringified = []
--> 490 for i, c in enumerate(frame):
491 cheader = str_columns[i]
492 max_colwidth = max(self.col_space or 0, *(self.adj.len(x)
C:\Users\USER\AppData\Local\Continuum\Miniconda2\lib\site-packages\pandas\core\generic.pyc in __iter__(self)
833 def __iter__(self):
834 """Iterate over infor axis"""
--> 835 return iter(self._info_axis)
836
837 # can we get a better explanation of this?
C:\Users\USER\AppData\Local\Continuum\Miniconda2\lib\site-packages\pandas\indexes\base.pyc in __iter__(self)
1344
1345 def __iter__(self):
-> 1346 return iter(self.values)
1347
1348 def __reduce__(self):
C:\Users\USER\AppData\Local\Continuum\Miniconda2\lib\site-packages\pandas\indexes\multi.pyc in values(self)
628 values.append(taken)
629
--> 630 self._tuples = lib.fast_zip(values)
631 return self._tuples
632
pandas\lib.pyx in pandas.lib.fast_zip (pandas\lib.c:11630)()
ValueError: all arrays must be same length
Output of pd.show_versions()
INSTALLED VERSIONS
------------------
commit: None
python: 2.7.13.final.0
python-bits: 64
OS: Windows
OS-release: 7
machine: AMD64
processor: Intel64 Family 6 Model 60 Stepping 3, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None
pandas: 0.19.2
nose: None
pip: 9.0.1
setuptools: 27.2.0
Cython: None
numpy: 1.12.1
scipy: 0.19.0
statsmodels: None
xarray: None
IPython: 5.3.0
sphinx: None
patsy: None
dateutil: 2.6.0
pytz: 2017.2
blosc: None
bottleneck: None
tables: None
numexpr: None
matplotlib: 2.0.0
openpyxl: 2.4.1
xlrd: 1.0.0
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: 0.999
httplib2: None
apiclient: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.9.6
boto: None
pandas_datareader: None