有没有办法将numpy掩码数组中的掩码值替换为null或None值?这就是我尝试过的方法,但不起作用。
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')具体来说
(photometry_dataframe.iloc[stars,2] = ma.filled(photometry_dataframe.iloc[stars,2], fill_value=None)) 产生
'MaskedConstant' object has no attribute '_fill_value'当我将数据帧输出为csv文件时,我想用'‘替换掩码值'--’。一种解决方法是将输出的csv文件读回python,并将'--‘替换为'',但这是一个可怕的解决方案。一定有更好的解决方案。我不希望掩码值在csv文件中显示为'--‘。
发布于 2016-01-27 10:16:32
使用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
<Table masked=True length=3>
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,您可以使用fill_values参数(http://docs.astropy.org/en/stable/io/ascii/write.html#parameters-for-write)指定从表值到输出值的任意转换。
https://stackoverflow.com/questions/35026040
复制相似问题