python - pandas.DataFrame 中的 NaN 不打印到 Excel

标签 python excel numpy pandas

我有一个 pandas.DataFrame,其中包含 numpy.nan float 。然而,当使用 Excel 编写器时,应该有 numpy.nan float 的字段是空白的。我希望至少有一个字符串表示而不是什么都没有。

对为什么会这样有任何想法吗?

编写器代码如下:

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

frame 看起来像这样(注意 2013-01-1 上的 NaN):

        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

但 Excel 文件有空白(见附图)。

enter image description here

最佳答案

来自documentation ,您应该使用您喜欢的字符串在 to_excel 中设置选项 na_rep。例如:

frame.to_excel(writer,'tab name', na_rep='NA')

关于python - pandas.DataFrame 中的 NaN 不打印到 Excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17623019/

相关文章:

python - 在 Python/numpy 中对容器进行排序

python - Zenity 与 iPython?

python - AttributeError : 'unicode' object has no attribute 'fromstring' . 如何解决这个问题?

python - Numpy where() 使用随数组中项目位置而变化的条件

vba - 如何比较 Excel-VBA 中两个单独的工作表列之间的字符串差异?

vba - 如何在Excel中获取VBA函数以返回事件单元格的值?

excel - 向下拖动公式并更改行引用而不是列

python - numpy.power 用于负数数组

python - 使用 Stream api 跟踪 Twitter 上的特定帐户

python - 单索引到 numpy 结构化数组子类