Construct pandas dataframe from a .fits file

user2867798 picture user2867798 · Oct 18, 2016 · Viewed 6.9k times · Source

I have a .fits file that contains data.

I would like to construct a pandas dataframe from this particular file but I don't know how to do it.

data = fits.open('datafile')
data.info

gives:

No.    Name         Type      Cards   Dimensions   Format
0    PRIMARY     PrimaryHDU       6   (12, 250000)   float64 

and:

data[0].data.shape

gives:

(250000, 12)

Answer

Paul H picture Paul H · Oct 18, 2016

According to what you have in your question and the astropy docs (http://docs.astropy.org/en/stable/io/fits/), it looks like you just need to do:

from astropy.io import fits
import pandas
with fits.open('datafile') as data:
    df = pandas.DataFrame(data[0].data)

Edit: I don't have much experience we astropy, but other have mentioned that you can read the fits files into a Table object, which has a to_pandas() method:

from astropy.table import Table
dat = Table.read('datafile', format='fits')
df = dat.to_pandas()

Might be worth investigating.

http://docs.astropy.org/en/latest/table/pandas.html