Old-School Old-School - 1 month ago 13
Python Question

Construct pandas dataframe from a .fits file

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

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
data = fits.open('datafile')
df = pandas.DataFrame(data[0].data)
Comments