python怎么检查dataframe 含有none值,在Python中使用ma numpy中的fiil_value用Null或None值替换掩码值(-)...

Is there a way to replace a masked value in a numpy masked array as a null or None value? This is what I have tried but does not work.

for stars in range(length_masterlist_final):

....

star = customSimbad.query_object(star_names[stars])

#obtain stellar info.

photometry_dataframe.iloc[stars,0] = star_IDs[stars]

photometry_dataframe.iloc[stars,1] = star_names[stars]

photometry_dataframe.iloc[stars,2] = star['FLUX_U'][0]

#Replace "--" masked values with a Null (i.e., '') value.

photometry_dataframe.iloc[stars,2] = ma.filled(photometry_dataframe.iloc[stars,2], fill_value=None)

.....

photometry_dataframe.to_csv(output_dir + "simbad_photometry.csv", index=False, header=True, na_rep='NaN')

specifically

(photometry_dataframe.iloc[stars,2] = ma.filled(photometry_dataframe.iloc[stars,2], fill_value=None))

produces

'MaskedConstant' object has no attribute '_fill_value'

I want to replace masked values '--' with '' when I output the dataframe as a csv file. One work around is to read the outputted csv file back into python and replace '--' with '', but this is a horrible solution. There must be a better solution. I don't want masked values printed as '--' in the csv file.

解决方案

Use Astropy:

>>> from pandas import DataFrame

>>> from astropy.table import Table

>>> import numpy as np

>>>

>>> df = DataFrame()

>>> df['a'] = [1, np.nan, 2]

>>> df['b'] = [3, 4, np.nan]

>>> df

a b

0 1 3

1 NaN 4

2 2 NaN

>>> t = Table.from_pandas(df)

>>> t

a b

float64 float64

------- -------

1.0 3.0

-- 4.0

2.0 --

>>> t.write('photometry.csv', format='ascii.csv')

>>>

(astropy)neptune$ cat photometry.csv

a,b

1.0,3.0

,4.0

2.0,

You can specify arbitrary transformations from table values to output values using the fill_values parameter (http://docs.astropy.org/en/stable/io/ascii/write.html#parameters-for-write).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值