代码:
import pandas as pd
from pandas import DataFrame
df3 = DataFrame({'key1': ['one', 'two', 'three', 'two'],
'key2': ['two', 'three', 'one', 'one'],
'data': [1, 2, 3, 4]})
df4 = DataFrame({'key1': ['two', 'two', 'three'],
'key2': ['one', 'two', 'one'],
'data': [1, 2, 3]})
data = pd.merge(df3, df4, on=['key1', 'key2'], how='outer')
print(data)
data.to_excel('D:\01.xls', na_rep=None)
结果:
key1 key2 data_x data_y
0 one two 1.0 NaN
1 two three 2.0 NaN
2 three one 3.0 3.0
3 two one 4.0 1.0
4 two two NaN 2.0
Traceback (most recent call last):
File "D:/python项目/02.py", line 232, in
data.to_excel('D:\01.xls', na_rep=None)
File "F:\python\lib\site-packages\pandas\core\generic.py", line 2256, in to_excel
engine=engine,
File "F:\python\lib\site-packages\pandas\io\formats\excel.py", line 742, in write
writer.save()
File "F:\python\lib\site-packages\pandas\io\excel_xlwt.py", line 32, in save
return self.book.save(self.path)
File "F:\python\lib\site-packages\xlwt\Workbook.py", line 710, in save
doc.save(filename_or_stream, self.get_biff_data())
File "F:\python\lib\site-packages\xlwt\CompoundDoc.py", line 262, in save
f = open(file_name_or_filelike_obj, 'w+b')
OSError: [Errno 22] Invalid argument: 'D:\x01.xls'
为什么会报错呢?两个DataFrame使用merge方法合并后不是变成一个DataFrame了吗,为什么使用to_excel方法会出现错误?