pandas - 如何将 .fits 文件/astropy 表转换为 pandas df 和反向?

标签 pandas dataframe astropy fits

我需要经常将 pandas df 转换为 .fits 文件,反之亦然 - 我得到了 .fits 文件,我更容易将它们作为 pandas dfs 处理。在这些文件类型之间进行转换的最直接方法是什么?

最佳答案

# 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)

关于pandas - 如何将 .fits 文件/astropy 表转换为 pandas df 和反向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71142383/

相关文章:

python - 在 Pandas 数据框中将两个公共(public)行的值分组为一个

python - 使用 Pandas 保存列中条目的总数

python - Pandas DataFrame 中匹配行的索引 [Python]

python - 根据现有列的部分字符串内容向新的 Pandas 数据框列添加值

Python - 使用正则表达式从 Pandas DataFrame 中的列(包含字符串)中提取权重并将其添加到新列中

python - 如何写入带有小数点后固定数字的 astropy Table 对象值?

python - 使用 Python 中 ma numpy 中的 fiil_value 将屏蔽值 (--) 替换为 Null 或 None 值

python - Astropy ICRS 到银河中心变换 Numpy AttributeError

python 对具有上限和下限的数据框进行缩放和评分

python - 从 pandas 数据帧创建字典,其中值是索引