Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NaNs in pandas.DataFrame not printing to Excel

I have a pandas.DataFrame which contains numpy.nan floats. When using the Excel writer however, the fields where there should be numpy.nan floats are blank. I would expected at least a string representation instead of nothing.

Any thoughts on why this might be?

Writer code is as follows:

writer=pandas.ExcelWriter('output.xls')
frame.to_excel(writer,'tab name')
writer.save()

Where frame looks something like this (note the NaN on 2013-01-1):

        Series ID Risk Bucket   Contract  PX Last  Contract Value (Local)  Currency X  Contract Value (USD)    Currency
2013-01-01  Future_ES          EQ  ES1 Index      NaN                     NaN           1                   NaN  USD Curncy
2013-01-02  Future_ES          EQ  ES1 Index  1447.16                 72362.5           1               72362.5  USD Curncy
2013-01-03  Future_ES          EQ  ES1 Index  1443.68                 72187.5           1               72187.5  USD Curncy
2013-01-04  Future_ES          EQ  ES1 Index  1447.90                 72400.0           1               72400.0  USD Curncy

But the Excel file has blanks (see attached image).

enter image description here

like image 718
Jason Strimpel Avatar asked Jul 12 '13 19:07

Jason Strimpel


People also ask

How do I export pandas DataFrame to excel?

Export a Pandas DataFrame Into Excel File by Using the to_excel() Function. When we export a pandas DataFrame to an excel sheet using the dataframe. to_excel() function, it writes an object into the excel sheet directly. To implement this method, create a DataFrame and then specify the name of the excel file.

How do you fix NaN errors in Python?

We can replace NaN values with 0 to get rid of NaN values. This is done by using fillna() function. This function will check the NaN values in the dataframe columns and fill the given value.

How do you handle NaN in a data frame?

In order to check missing values in Pandas DataFrame, we use a function isnull() and notnull(). Both function help in checking whether a value is NaN or not. These function can also be used in Pandas Series in order to find null values in a series.

How do I get rid of NANS in pandas?

By using dropna() method you can drop rows with NaN (Not a Number) and None values from pandas DataFrame. Note that by default it returns the copy of the DataFrame after removing rows. If you wanted to remove from the existing DataFrame, you should use inplace=True .


1 Answers

From the documentation, you should set the option na_rep in to_excel with a string of your liking. E.g.:

frame.to_excel(writer,'tab name', na_rep='NA')
like image 66
tiago Avatar answered Oct 11 '22 17:10

tiago