Read Latex Table in Pandas DataFrame

Is there an easy way to read the Latex table created by the DataFrame to_latex () method back into another DataFrame ?. In particular, I'm looking for something that Multiindex handles. For example, if we have the following test.out file:

\begin{tabular}{llllrrr}
\toprule
   &      &     &       1 &       2 &          3 \\
\midrule
a  &  1   & 1.0 &    1898 &    1681 &   1.129090 \\
   &      & 0.1 &    1898 &    1349 &   1.406968 \\
   &  10  & 1.0 &    8965 &    5193 &   1.726362 \\
   &      & 0.1 &    8965 &    1669 &   5.371480 \\
   &  100 & 1.0 &   47162 &   22049 &   2.138963 \\
   &      & 0.1 &   47162 &    5732 &   8.227844 \\
b  &  1   & 1.0 &    8316 &    7200 &   1.155000 \\
   &      & 0.1 &    8316 &    5458 &   1.523635 \\
   &  10  & 1.0 &   43727 &   24654 &   1.773627 \\
   &      & 0.1 &   43727 &    6945 &   6.296184 \\
   &  100 & 1.0 &  284637 &  137391 &   2.071730 \\
   &      & 0.1 &  284637 &   26364 &  10.796427 \\
\bottomrule
\end{tabular}

My first attempt was to read it as

df = pd.read_csv('test.out',
                 sep='&',
                 header=None,
                 index_col=(0,1,2),
                 skiprows=4,
                 skipfooter=3,
                 engine='python')

which does not work correctly, because it read_csv()selects empty fields as the new Multiindex levels:

In [4]: df.index
Out[4]:
MultiIndex(levels=[[u'       ', u'a      ', u'b      '], [u'      ', u'  1   
', u'  10  ', u'  100 '], [0.1, 1.0]],
       labels=[[1, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0], [1, 0, 2, 0, 3, 0, 1, 
0, 2, 0, 3, 0], [1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0]],
       names=[0, 1, 2])

Is there any way to do this?

+4
source share
2 answers

The astropy module has a LaTeX table reader. But it does not support all LaTeX expressions. I had to remove \ toprule, \ midrule and \ bottomrule. This is suitable for me.

from astropy.table import Table
tab = Table.read('table.tex').to_pandas()

enter image description here

+3

:

:

df = pd.read_csv('table.tex',
                 sep='&',
                 header=None,
                 skiprows=4,
                 skipfooter=3,
                 engine='python')

"" np.nan:

df.loc[df.loc[:,0].str.strip() == "", 0] = np.nan
df.loc[df.loc[:,1].str.strip() == "", 1] = np.nan

pandas 'fillna 0 2 :

df = df.fillna(method='ffill', axis=0).set_index([0,1,2])
+1

Source: https://habr.com/ru/post/1676391/


All Articles