emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Dov Grobgeld <dov.grobgeld@gmail.com>
To: Eric Schulte <schulte.eric@gmail.com>
Cc: emacs-orgmode <emacs-orgmode@gnu.org>, Rasmus <rasmus@gmx.us>
Subject: Re: org-mode and python pandas
Date: Tue, 28 Apr 2015 11:36:53 +0300	[thread overview]
Message-ID: <CA++fsGFxXECGspTfqEW-T+FQZpzF4B3kG9_ATbiV6hhmEOms0A@mail.gmail.com> (raw)
In-Reply-To: <8761wrpwew.fsf@gmail.com>

[-- Attachment #1: Type: text/plain, Size: 5615 bytes --]

I returned to this issue recently and tried to get the ob-python to output
a table with a header, but didn't manage. Here is the code:

#+BEGIN_SRC python :colnames true
return [['','A','B','C'],
        [0,0.628365,0.424279,0.619791],
        [1,0.799666,0.527572,0.132928]]
#+END_SRC

#+RESULTS:
|   |        A |        B |        C |
| 0 | 0.628365 | 0.424279 | 0.619791 |
| 1 | 0.799666 | 0.527572 | 0.132928 |

What I want is:

|   |        A |        B |        C |
|---+----------+----------+----------|
| 0 | 0.628365 | 0.424279 | 0.619791 |
| 1 | 0.799666 | 0.527572 | 0.132928 |

Is there any way to do that besides using the :results raw option?

Thanks!
Dov


On Wed, Jul 3, 2013 at 5:09 PM, Eric Schulte <schulte.eric@gmail.com> wrote:

> Dov Grobgeld <dov.grobgeld@gmail.com> writes:
>
> > Thanks for the answers, but there is still something missing in order
> > to get it to work. Part of it seems to be connected to the python
> > parsing. E.g. the following translation of Eric's sh example doesn't
> > output correctly with python:
> >
> >
> > #+BEGIN_SRC python :results output
> > print """,A,B,C
> > 0,0.628365,0.424279,0.619791
> > 1,0.799666,0.527572,0.132928
> > 2,0.837255,0.138906,0.408233
> > 3,0.388080,0.146212,0.575346
> > """
> > #+END_SRC
> >
> > #+RESULTS:
> > : ,A,B,C
> > : 0,0.628365,0.424279,0.619791
> > : 1,0.799666,0.527572,0.132928
> > : 2,0.837255,0.138906,0.408233
> > : 3,0.388080,0.146212,0.575346
> >
> >
> > #+BEGIN_SRC python :results table
> > return """,A,B,C
> > 0,0.628365,0.424279,0.619791
> > 1,0.799666,0.527572,0.132928
> > 2,0.837255,0.138906,0.408233
> > 3,0.388080,0.146212,0.575346
> > """
> > #+END_SRC
> >
> > #+RESULTS:
> > |
> ,A,B,C\n\n0,0.628365,0.424279,0.619791\n\n1,0.799666,0.527572,0.132928\n\n2,0.837255,0.138906,0.408233\n\n3,0.388080,0.146212,0.575346
> > |
> >
> > It seems that the only way to get a table from python is by outputting
> > a two dimensional python structure:
> >
> > #+BEGIN_SRC python
> > return [[0,0.628365,0.424279,0.619791],
> >         [1,0.799666,0.527572,0.132928]]
> > #+END_SRC
> >
> > #+RESULTS:
> > | 0 | 0.628365 | 0.424279 | 0.619791 |
> > | 1 | 0.799666 | 0.527572 | 0.132928 |
> >
> > This seems quite limiting....
> >
>
> In most cases this is what one wants when returning data from python
> code.  The following elisp defined a "panda" code block, which is just
> like python, only it assumes that the results will be these sort of
> human readable strings instead of python code.
>
>     ;; -*- emacs-lisp -*-
>     (defun org-babel-execute:panda (body params)
>       (let ((results
>              (org-babel-execute:python
>               body (org-babel-merge-params '((:results . "scalar"))
> params))))
>         (org-babel-result-cond (cdr (assoc :result-params params))
>           results
>           (let ((tmp-file (org-babel-temp-file "sh-")))
>             (with-temp-file tmp-file (insert results))
>             (org-babel-import-elisp-from-file tmp-file)))))
>
> With the above evaluated the following works
>
>     #+BEGIN_SRC panda
>     return """,A,B,C
>     0,0.628365,0.424279,0.619791
>     1,0.799666,0.527572,0.132928
>     2,0.837255,0.138906,0.408233
>     3,0.388080,0.146212,0.575346
>     """
>     #+END_SRC
>
>     #+RESULTS:
>     |   |        A |        B |        C |
>     | 0 | 0.628365 | 0.424279 | 0.619791 |
>     | 1 | 0.799666 | 0.527572 | 0.132928 |
>     | 2 | 0.837255 | 0.138906 | 0.408233 |
>     | 3 |  0.38808 | 0.146212 | 0.575346 |
>
> >
> > Another related question is if there is any support for header tables?
> > I.e. instead of this:
> >
> > |   |        A |        B |        C |
> > | 0 | 0.827817 | 0.664009 | 0.089161 |
> > | 1 | 0.170031 | 0.729214 | 0.110918 |
> > | 2 | 0.575918 | 0.863924 | 0.757536 |
> > | 3 | 0.682722 | 0.774445 | 0.992041 |
> >
> > I want this:
> >
> > |   |        A |        B |        C |
> > |---+----------+----------+----------|
> > | 0 | 0.827817 | 0.664009 | 0.089161 |
> > | 1 | 0.170031 | 0.729214 | 0.110918 |
> > | 2 | 0.575918 | 0.863924 | 0.757536 |
> > | 3 | 0.682722 | 0.774445 | 0.992041 |
> >
> > I guess that if I start playing around with the python ob module, it
> > should be possible to get this working?
> >
>
> See the :colnames header argument in the manual.
>
> Best,
>
> >
> > Regards,
> > Dov
> >
> > On Mon, Jul 1, 2013 at 8:04 PM, Rasmus <rasmus@gmx.us> wrote:
> >> Achim Gratz <Stromeko@nexgo.de> writes:
> >>
> >>>>> 2. Add to pandas the option of globally influencing the text
> >>>>> formatting so that it outputs something more parsable by org-mode.
> >>>>
> >>>> This sounds promising, if pandas support csv output that will be
> >>>> correctly parsed by Org-mode.
> >>>
> >>> The package already has CSV export, so one could use that.  I don't
> know
> >>> if you could echo the result directly to the output, all examples
> >>> revolve around putting the CSV into a file.  For Org, TSV output would
> >>> be more natural.
> >>
> >> Something like:
> >>
> >> from pandas import DataFrame
> >> from numpy.random import rand
> >> from sys import stdout
> >> df = DataFrame(rand(10,3), columns = list('abc'))
> >> df
> >> df.to_csv(stdout, sep="\t", header = True, cols=(1,2))
> >>
> >> I was completely unable to get ob-python working this morning, so I
> >> haven't tested it.  I'm using python3, build in python mode and elpy.
> >>
> >> In any case, the csv route might be better, as Pandas doesn't print
> >> the table if it's too big (try changing 10 to 1000 above).
> >>
> >> --
> >> Powered by magic pixies!
> >>
> >>
> >
>
> --
> Eric Schulte
> http://cs.unm.edu/~eschulte
>

[-- Attachment #2: Type: text/html, Size: 8155 bytes --]

  reply	other threads:[~2015-04-28  8:36 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-06-28  5:26 org-mode and python pandas Dov Grobgeld
2013-06-30 23:15 ` Eric Schulte
2013-07-01 16:34   ` Achim Gratz
2013-07-01 17:04     ` Rasmus
2013-07-03  9:15       ` Dov Grobgeld
2013-07-03 10:31         ` Rasmus
2013-07-03 14:09         ` Eric Schulte
2015-04-28  8:36           ` Dov Grobgeld [this message]
  -- strict thread matches above, loose matches on Subject: below --
2015-04-29  7:12 Dror Atariah

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CA++fsGFxXECGspTfqEW-T+FQZpzF4B3kG9_ATbiV6hhmEOms0A@mail.gmail.com \
    --to=dov.grobgeld@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    --cc=rasmus@gmx.us \
    --cc=schulte.eric@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).