Python pandas.read_excel store empy cells as 'None' instead of Nan values

Viewed 697

Currently I'm working with pandas and Excel. I've got excel sheet to process with lots of empy cells. Pandas recognize them as Nan values when parsing. I know how to get rid of them by passing this into pandas excel reading method.

pandas.read_excel(keep_default_na=False)

or

pandas.read_excel(na_filter=False)

But I would like to store empy cells as 'None' string instead. Is there any way to do that when reading a Excel file? Regards, Ed.

1 Answers

Load the data and hold it into a data frame named let's say 'df'. And the next step is to use df.fillna('value of your choice')

df = pandas.read_excel(keep_default_na=False)
df = df.fillna('None')

if you print df now, it will show as per your requirement.

Example :

df = pd.DataFrame([[1, 3], [3, 2, 4], [5]])
df = df.fillna('None')
print(df)

      0     1     2
   0  1     3  None
   1  3     2     4
   2  5  None  None

Hope this helps. Please comment if you think this is not what you wanted.

Related