【问题标题】:python pandas dataframe write multi row headerpython pandas数据框写入多行标题
【发布时间】:2018-04-07 12:09:24
【问题描述】:

数据1

A  B  C  D  E   <--- columns
a  b  c  d  e
a  b  c  d  e
a  b  c  d  e


result what i want

A  B  C  D  E <--- columns
A  B  C  D  E <--- columns
A  B  C  D  E <--- columns
a  b  c  d  e
a  b  c  d  e
a  b  c  d  e

我搜索了这个,最后失败了:)

如何获得好的结果?我请求你的帮助。谢谢你


非常感谢

转换为 Excel 时,创建了一个新的空列。

test

a   b   c   d   e   f   g   h   i   j   k   l   m   n   o   p   q   r   s   t   u   v   w   x   y
1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1
2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2
3   3   3   3   3   3   3   3   3   3   3   3   3   3   3   3   3   3   3   3   3   3   3   3   3
4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4
4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4
5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5


dir_path = os.path.dirname(os.path.realpath(__file__))
df = pd.read_excel(dir_path + '/import/sample.xlsx', header=0)
headers = [list(df.columns), list(df.columns), list(df.columns)]
df.columns = pd.MultiIndex.from_arrays(headers)
writer = pd.ExcelWriter(dir_path + '/export/sample.xlsx', options={'strings_to_urls': False}, )
df.to_excel(writer, sheet_name='result')
writer.save()

result

    a   b   c   d   e   f   g   h   i   j   k   l   m   n   o   p   q   r   s   t   u   v   w   x   y
    a   b   c   d   e   f   g   h   i   j   k   l   m   n   o   p   q   r   s   t   u   v   w   x   y
    a   b   c   d   e   f   g   h   i   j   k   l   m   n   o   p   q   r   s   t   u   v   w   x   y
                                <<<<---- empty row  ---->>>>                                                    0   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1
1   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2
2   3   3   3   3   3   3   3   3   3   3   3   3   3   3   3   3   3   3   3   3   3   3   3   3   3
3   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4
4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4
5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5

我试图摆脱它但失败了。

df = df[df['a'] != ""]

【问题讨论】:

  • 它是bug,可能的解决方案是here

标签: pandas dataframe pandas.excelwriter


【解决方案1】:

使用MultiIndex.from_arrays:

df.columns = pd.MultiIndex.from_arrays([df.columns] * 3)
print (df)
   A  B  C  D  E
   A  B  C  D  E
   A  B  C  D  E
0  a  b  c  d  e
1  a  b  c  d  e
2  a  b  c  d  e

如果需要自定义MultiIndex:

L = [list('abcde'), list('fghij'), list('klmno')]
df.columns = pd.MultiIndex.from_arrays(L)
print (df)
   a  b  c  d  e
   f  g  h  i  j
   k  l  m  n  o
0  a  b  c  d  e
1  a  b  c  d  e
2  a  b  c  d  e

也可以使用接下来的 2 个函数来创建 MultiIndex

编辑:

空行是bug,解决方案是here

writer = pd.ExcelWriter("file1.xlsx")
headers = pd.DataFrame(df.columns.tolist()).T
headers.to_excel(writer, header=False, index=False)
df.columns = pd.Index(range(len(df.columns)))  
df.to_excel( writer, header=False, index=False, startrow=len(headers))
writer.save()

【讨论】:

  • @susim - 我只是测试它df.to_excel('file1.xlsx'),它工作得很好。
  • 非常感谢!!新添加的。你能确认一下吗?。
  • @susim - 欢迎您。请不要忘记接受和支持解决方案。谢谢。
猜你喜欢
  • 1970-01-01
  • 2014-11-21
  • 1970-01-01
  • 2017-02-17
  • 1970-01-01
  • 2019-02-12
  • 1970-01-01
  • 2021-02-17
  • 2019-08-23
相关资源
最近更新 更多