在 python 中使用 Pandas ExcelWriter 时处理 Nan

Handle Nan when using Pandas ExcelWriter in python

当我逐行遍历它并在 A 列中输出值时,我将如何更改以下代码以将 NaN 值处理为我的数据框中的空单元格?

excel = pd.ExcelWriter(f_name,engine='xlsxwriter')
wb = excel.book
ws = wb.add_worksheet('PnL')

for i in len(df):
  ws.write(0,i,df.iloc[i]['A'])

我想你可以使用 fillna:

df = df.fillna(0)

或:

df['A'] = df['A'].fillna(0)

但更好的是使用to_excel:

import pandas as pd
import numpy as np

# Create a Pandas dataframe from the data.
df = pd.DataFrame({'A': [10, 20, 30, 20, 15, 30, 45, np.nan], 
                   'B': [10, 20, 30, 20, 15, 30, 45, np.nan]})
print df
    A   B
0  10  10
1  20  20
2  30  30
3  20  20
4  15  15
5  30  30
6  45  45
7 NaN NaN

#create subset, because cannot write Series to excel
df1 = df[['A']]

# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter('f_name.xlsx', engine='xlsxwriter')

# Convert the dataframe to an XlsxWriter Excel object, instead NaN give 0
df1.to_excel(writer, sheet_name='PnL', na_rep=0)

如果要省略索引和header,请添加参数index=Falseheader=False:

df1.to_excel(writer, sheet_name='PnL', na_rep=0, index=False, header=False)

我建议复制数据帧,将 NAN 更改为空格,然后写入数据。您的原始数据框仍将包含 NAN。

此外,作为一种解决方法,Xlsxwriter 支持编写映射到产生错误代码的公式的 NAN。但是,这是一个构造函数选项,默认情况下不启用。参见docs

如果您的版本是 Pandas >= 0.16,您应该可以按如下方式打开该选项:

excel = pd.ExcelWriter(f_name,
                        engine='xlsxwriter',
                        options={'nan_inf_to_errors': True})