Construct pandas dataframe from a .fits file
<p>I have a .fits file that contains data. </p>

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

<pre><code>data = fits.open('datafile')
data.info
</code></pre>

<p>gives: </p>

<pre><code>No.    Name         Type      Cards   Dimensions   Format
0    PRIMARY     PrimaryHDU       6   (12, 250000)   float64 
</code></pre>

<p>and: </p>

<pre><code>data[0].data.shape
</code></pre>

<p>gives:</p>

<pre><code>(250000, 12)
</code></pre>

<p>According to what you have in your question and the astropy docs (<a href="http://docs.astropy.org/en/stable/io/fits/" rel="nofollow">http://docs.astropy.org/en/stable/io/fits/</a>), it looks like you just need to do: </p>

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

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

<pre><code>from astropy.table import Table
dat = Table.read('datafile', format='fits')
df = dat.to_pandas()
</code></pre>

<p>Might be worth investigating.</p>

<p><a href="http://docs.astropy.org/en/latest/table/pandas.html" rel="nofollow">http://docs.astropy.org/en/latest/table/pandas.html</a></p>

python pandas dataframe astropy