如何将 .fits 文件/ Astropy 表格转换为 Pandas 数据框并反向操作?

4

我经常需要将pandas df转换为.fits文件,也需要将给定的.fits文件转换为pandas df进行处理。最直接的方法是什么来完成这些文件类型之间的转换?

1个回答

3
# 1.)  how to convert a pandas df 
# (or almost any ascii data file) to astropy table
# and save the table as .fits file

from astropy.table import Table
import pandas as pd

df = pd.read_csv('some_file.dat', delim_whitespace=True, skiprows=7, names=['A', 'B', 'C'])

t = Table.from_pandas(df)
t.write('some_file.fits')




# 2.)  how to convert a fits file (or astropy table) to pandas df

dat = Table.read('some_file.fits')
df = dat.to_pandas()



# 3.)  it is like 1.) but without a df as intermediate step - 
# how to read a csv/ascii file directly as astropy table
# and save it as .fits   

table = Table.read('some_file.dat', format='ascii.tab')
table.write('some_file.fits', overwrite=True)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接