gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 03:28:55 30 4
gpt4 key购买 nike

我需要经常将 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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com