Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

221
Views
Error: object of type 'zip' has no len() after adding extra header by using zip

I try to follow this link pandas dataframe with 2-rows header and export to csv in order for me to created extra header without remove the original header, below is my coding:

df.columns = pd.MultiIndex.from_tuples((zip(df.columns,[uniquevaluesfirstcolumnww, '', uniquevaluesfirstcolumnww1, ' ',uniquevaluesfirstcolumnww2, '  '])))

I get the error such as this: object of type 'zip' has no len()

Anyone have any idea? even though I try to add list before zip but fail also.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I think problem is zip return object in python 3, so need convert to list:

df.columns = pd.MultiIndex.from_tuples((list(zip(df.columns,[uniquevaluesfirstcolumnww, '', uniquevaluesfirstcolumnww1, ' ',uniquevaluesfirstcolumnww2, '  ']))))

But is seems you need MultiIndex.from_arrays:

cols = list('abcdef')
df = pd.DataFrame([[1,1,1,1,1,1]], columns=cols)
print (df)
   a  b  c  d  e  f
0  1  1  1  1  1  1

uniquevaluesfirstcolumnww = 'r'
uniquevaluesfirstcolumnww1 = 's'
uniquevaluesfirstcolumnww2 = 't'
vals = [uniquevaluesfirstcolumnww, '', 
        uniquevaluesfirstcolumnww1, ' ',
        uniquevaluesfirstcolumnww2, '  ']
df.columns = pd.MultiIndex.from_arrays([df.columns, vals])
print (df)
   a  b  c  d  e  f
   r     s     t   
0  1  1  1  1  1  1

If columns have MultiIndex also:

cols = pd.MultiIndex.from_product([['a','b','c'], ['x','y']])
df = pd.DataFrame([[1,1,1,1,1,1]], columns=cols)
print (df)
   a     b     c   
   x  y  x  y  x  y
0  1  1  1  1  1  1

uniquevaluesfirstcolumnww = 'r'
uniquevaluesfirstcolumnww1 = 's'
uniquevaluesfirstcolumnww2 = 't'
vals = [uniquevaluesfirstcolumnww, '', 
        uniquevaluesfirstcolumnww1, ' ',
        uniquevaluesfirstcolumnww2, '  ']
df.columns = pd.MultiIndex.from_arrays([df.columns.get_level_values(0), 
                                        df.columns.get_level_values(1), 
                                        vals])
print (df)
   a     b     c   
   x  y  x  y  x  y
   r     s     t   
0  1  1  1  1  1  1
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!