Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
保存到excel时出现奇怪的问题_Excel_Pandas - Fatal编程技术网

保存到excel时出现奇怪的问题

保存到excel时出现奇怪的问题,excel,pandas,Excel,Pandas,我在追求卓越方面有些问题。我的数据框中有15列。我只希望将其中7个文件写入excel,并在此过程中使用另一个名称作为标题 这是我的密码 cols = ['SN', 'Date_x','Material_x', 'Batch_x', 'Qty_x', 'Booked_x', 'State_x'] headers = ['SN', 'Date', 'Material', 'Batch', 'Qty', 'Booked', 'State'] df.style.apply(highlight_ch

我在追求卓越方面有些问题。我的数据框中有15列。我只希望将其中7个文件写入excel,并在此过程中使用另一个名称作为标题

这是我的密码

cols = ['SN',  'Date_x','Material_x', 'Batch_x',  'Qty_x', 'Booked_x', 'State_x']
headers = ['SN', 'Date', 'Material', 'Batch',  'Qty', 'Booked', 'State']
df.style.apply(highlight_changes_ivt2, axis=None).to_excel(writer, columns =cols, header=headers, sheet_name="temp", index = False)
但我有以下错误

  File "/home/week/anaconda3/envs/SC/lib/python3.7/site-packages/pandas/io/formats/style.py", line 235, in to_excel
engine=engine,
  File "/home/week/anaconda3/envs/SC/lib/python3.7/site-packages/pandas/io/formats/excel.py", line 735, in write
freeze_panes=freeze_panes,
  File "/home/week/anaconda3/envs/SC/lib/python3.7/site-packages/pandas/io/excel/_xlsxwriter.py", line 214, in write_cells
for cell in cells:
  File "/home/week/anaconda3/envs/SC/lib/python3.7/site-packages/pandas/io/formats/excel.py", line 684, in get_formatted_cells
for cell in itertools.chain(self._format_header(), self._format_body()):
 File "/home/week/anaconda3/envs/SC/lib/python3.7/site-packages/pandas/io/formats/excel.py", line 513, in _format_header_regular
f"Writing {len(self.columns)} cols but got {len(self.header)} "
ValueError: Writing 15 cols but got 7 aliases
我试着做调试。。和设置pdb.set_trace()

这段代码在我家的笔记本电脑上运行良好。。。只是想知道怎么了。。。区别仅在于python在本版本中使用了3.7,而在国内使用了3.8


谢谢

让我在评论中举例说明我的想法:

df = pd.DataFrame(np.arange(16).reshape(4,-1))

# this is the reference dataframe
np.random.seed(1)
ref_df = pd.DataFrame(np.random.randint(1,10,(4,4)))

# this is the function
def highlight(col, ref_df=None):
    return ['background-color: yellow' if c>r else '' 
                for c,r in zip(col, ref_df[col.name])]

# this works
df[[0,1,3]].style.apply(highlight, ref_df=ref_df).to_excel('style.xlsx', header=list('abc'))
输出:


也许可以
df[cols].style.apply(…).to_excel(…)
?@Quang Hoang。。。嗨,我不认为我可以使用上面的方法…因为在我的函数highlight_changes_ivt2中,在Qty_x和Qty_y之间有计算。。。感谢您的回答。您可以重写您的函数,使其接受引用数据框:
def highlight\u changes(col,ref\u dataframe=df):
然后传入包含相应列的数据框。看起来索引中包含了所有列。尝试将
之前的
reset_index()
添加到\u excel
@DStanley 1.0.1版。。。我以前也曾尝试安装莫丁来加速计算……但最终放弃了……托特·莫丁可能与这个问题有关。(以前它正在工作)。。。。必须尝试在pycharm启动一个新项目。。但是仍然存在同样的问题,我使用了你的方法,它是有效的……但是旧方法到底有什么错误呢?
df = pd.DataFrame(np.arange(16).reshape(4,-1))

# this is the reference dataframe
np.random.seed(1)
ref_df = pd.DataFrame(np.random.randint(1,10,(4,4)))

# this is the function
def highlight(col, ref_df=None):
    return ['background-color: yellow' if c>r else '' 
                for c,r in zip(col, ref_df[col.name])]

# this works
df[[0,1,3]].style.apply(highlight, ref_df=ref_df).to_excel('style.xlsx', header=list('abc'))