第一句子网 - 唯美句子、句子迷、好句子大全
第一句子网 > python写入excel文件出现nan_在python中使用Pandas ExcelWriter时处理Nan

python写入excel文件出现nan_在python中使用Pandas ExcelWriter时处理Nan

时间:2022-11-08 06:31:22

相关推荐

python写入excel文件出现nan_在python中使用Pandas ExcelWriter时处理Nan

我想你可以用^{}:df = df.fillna(0)

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

但最好使用^{}: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)

如果要省略索引和头,请添加参数index=False和header=False:df1.to_excel(writer, sheet_name='PnL', na_rep=0, index=False, header=False)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。