1

I have the reverse problem as described in Prevent pandas from interpreting 'NA' as NaN in a string.

I work with older English text data and want to write the word "nan" (i.e. Modern English 'non(e)') into an Excel file.

I want Excel to show this word as "nan" for a particular column. However, I don't want empty cells elsewhere in my dataframe be filled with any other Excel NaN-replacements.

Instead, what I get when I use df.to_excel() is an empty cell.

2
  • Can you give a minimum reproducible example of what your code is. Commented Jul 29 at 12:46
  • chk replace or fillna Commented Jul 29 at 12:49

2 Answers 2

0

[Updated]

  1. Assuming show "nan" for a particular column for the NaN values
df["particular column"] = df["particular column"].fillna("nan")
  1. Assuming show empty for rest of NaN values
df.toexcel("df.xlsx") # normal behavior already write as empty

[Old]

To Excel

df.toexcel("df.xlsx", na_rep="None") # or "nan" 

From Excel

retain "None"

pd.read_excel("df.xlsx", na_filter=False)

or convert "None" values to NaN

pd.read_excel("df.xlsx"), na_values="None") # or "nan"
Sign up to request clarification or add additional context in comments.

Comments

0

There is a fix to this, you can try and replace the nan with something the excel sheet would display as text instead.

df["word"] = df["word"].replace("nan", "nan_value")
df.to_excel("output.xlsx", index=False)

If you do that then excel will no longer think of it as an actual nan value but rather a string that it will display normally.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.