0

I can't find how to open an org-mode table as a pandas dataframe.

Here is my example:

#+NAME:tbl
| a | b |
|---|---|
| 1 | 2 |

#+BEGIN_SRC python :var tbl=electro :results replace output   :session test :exports both
import pandas as pd
df = pd.DataFrame(tbl)
print(df)
#+END_SRC

Here I get the dataframe, but the columns are 1 2

1 Answer 1

1

I am sorry, I found (probably) a solution (my actual example):

#+NAME:electro
| TIME                    |   low | high | heatpump | water | accurain | repy1 | repy2 | repy3 | repy4 |
|-------------------------+-------+------+----------+-------+----------+-------+-------+-------+-------|
| 2016-10-15 08:41:00.000 | 24830 | 3242 |     7308 |  2294 |      NaN |       |       |       |       |
| 2016-10-23 10:38:53.000 | 24963 | 3254 |     7362 |  2317 |          |       |       |       |       |
| 2016-10-30 19:36:38.000 | 25107 | 3265 |     7417 |  2342 |          |       |       |       |       |
|


#+BEGIN_SRC python :var tbl=electro :hlines no  :colnames no :results replace output   :session test :exports both
import pandas as pd
df = pd.DataFrame(tbl[1:], columns=tbl[0])

# and now more tricks...

df = df.replace('', np.nan)
df = df.replace('NaN', np.nan)
print(df)
df.to_hdf("test.h5",key="key1",mode='w')
#+END_SRC

The solution was to add :colnames no to prevent babel from removing the header.

1
  • This is the only way I know how to do it. Commented Feb 21, 2021 at 16:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.