简体   繁体   中英

Pandas to_excel corrupts '='

I have troubles writing equal signs from a DataFrame to an Excel file:
I read in a Excel file, (which I would like to modify in future), and write the DataFrame back to an Excel file. The Dataframe contains equal signs (=), which pop up as 0 in the ouput excel file.

I already checked the data type of the equal sign, it's unicode: u'=' . I attached the code I use, plus some additional debug output (which I hope helps).

fname = os.path.join(baseDIR, PARAMETERS_FILE)
base_params = read_excel (fname, PARAMETERS_SHEET)
base_params.override = False
>>> base_params.operator
0      rev
1      rev
2      rev
3      rev
4      rev
5      rev
6      rev
7      rev
8      rev
9       >=
10       =
11      <=
12      <=
13      <=
14      <=
15     NaN
16       =
17       =
18       =
19     NaN
Name: operator, dtype: object
out_file = os.path.join(onDIR, PARAMETERS_FILE)
writer = ExcelWriter(out_file)
base_params.to_excel(writer, PARAMETERS_SHEET, index=False, encoding='utf-8')
writer.save()    
>>> pd.show_versions()

INSTALLED VERSIONS
------------------
commit: None
python: 2.7.10.final.0
python-bits: 64
OS: Windows
OS-release: 7
machine: AMD64
processor: Intel64 Family 6 Model 58 Stepping 9, GenuineIntel
byteorder: little
LC_ALL: None
LANG: de_AT

pandas: 0.16.2
nose: 1.3.7
Cython: 0.22.1
numpy: 1.9.2
scipy: 0.15.1
statsmodels: None
IPython: 3.2.0
sphinx: 1.3.1
patsy: 0.3.0
dateutil: 2.4.2
pytz: 2015.4
bottleneck: None
tables: 3.2.0
numexpr: 2.4.3
matplotlib: 1.4.3
openpyxl: None
xlrd: 0.9.3
xlwt: None
xlsxwriter: 0.7.3
lxml: 3.4.4
bs4: 4.3.2
html5lib: None
httplib2: None
apiclient: None
sqlalchemy: 1.0.5
pymysql: None
psycopg2: None

In the output Excel file I get; the '=' is replaced with 0.

To get excel to display literally what is in the cell, you need to prepend the contents with a single quote: ' , this will stop it from trying to evaluate the contents as a formula.

So, instead of writing "=" , write "'=" etc.

Or, of course, change the cell format to Text . The default will be General which will attempt to evaluate everything.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM