emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* getting an hline in a python generated table
@ 2015-04-01 20:07 John Kitchin
  2015-04-01 20:50 ` Rasmus
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: John Kitchin @ 2015-04-01 20:07 UTC (permalink / raw)
  To: Orgmode Mailing List

Hi everyone,

In emacs-lisp, I can get a table as output that has a horizontal line
in it like this:

(append '((name scopus-id h-index n-docs n-citations))
        '(hline)
        (some expression that generates a list))

The first row is header names, then a horizontal line, followed by a row
for each thing of interest. This seems to work because the result is an
emacs-lisp "array".

I cannot figure out if this is possible in a Python block though. So far
my experiments have failed because I don't know how to make an hline
symbol in a Python array. Any kind of string just shows as a row. Any
thoughts on if this is possible?

thanks,

--
Professor John Kitchin
Doherty Hall A207F
Department of Chemical Engineering
Carnegie Mellon University
Pittsburgh, PA 15213
412-268-7803
@johnkitchin
http://kitchingroup.cheme.cmu.edu

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: getting an hline in a python generated table
  2015-04-01 20:07 getting an hline in a python generated table John Kitchin
@ 2015-04-01 20:50 ` Rasmus
  2015-04-03 18:44 ` Ken Mankoff
  2015-04-03 20:42 ` William Henney
  2 siblings, 0 replies; 7+ messages in thread
From: Rasmus @ 2015-04-01 20:50 UTC (permalink / raw)
  To: emacs-orgmode

Hi John,

John Kitchin <jkitchin@andrew.cmu.edu> writes:

> In emacs-lisp, I can get a table as output that has a horizontal line
> in it like this:
>
> (append '((name scopus-id h-index n-docs n-citations))
>         '(hline)
>         (some expression that generates a list))
>
> The first row is header names, then a horizontal line, followed by a row
> for each thing of interest. This seems to work because the result is an
> emacs-lisp "array".
>
> I cannot figure out if this is possible in a Python block though. So far
> my experiments have failed because I don't know how to make an hline
> symbol in a Python array. Any kind of string just shows as a row. Any
> thoughts on if this is possible?

I'd be lazy and just use the :post argument.  So something like the
following.  I'm pretty sure I once wrote a :post function that took
negative number (to add a final hline), but I couldn't find it...

    #+BEGIN_SRC python :post add-hline(tbl=*this*)
    return([ ["a"], [1], [2]])
    #+END_SRC

    #+RESULTS:
    |---|
    | a |
    |---|
    | 1 |
    | 2 |

    #+name: add-hline
    #+BEGIN_SRC emacs-lisp :var tbl='((a) (b) (c)) hlines='(0 1)
      (loop for hline in
            (mapcar* '+ hlines 
                     (number-sequence 0 (length hlines)))
            do (setq tbl (append (subseq tbl 0 hline)
                                 '(hline)
                                 (subseq tbl hline))))
    tbl
    #+END_SRC

Hope it helps,
Rasmus

-- 
With monopolies the cake is a lie!

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: getting an hline in a python generated table
  2015-04-01 20:07 getting an hline in a python generated table John Kitchin
  2015-04-01 20:50 ` Rasmus
@ 2015-04-03 18:44 ` Ken Mankoff
  2015-04-03 18:46   ` Ken Mankoff
  2015-04-03 20:42 ` William Henney
  2 siblings, 1 reply; 7+ messages in thread
From: Ken Mankoff @ 2015-04-03 18:44 UTC (permalink / raw)
  To: John Kitchin; +Cc: Orgmode Mailing List


I've been using the following to generate hline in Org Python blocks:

#+BEGIN_SRC python :results table :exports results
  from tabulate import tabulate
  import pandas as pd
  df = pd.DataFrame(np.random.rand(2,2), index=['foo','bar'])
  tab = tabulate(df, ['col1','col2'], tablefmt='orgtbl')
  return tab[1:-1]
#+END_SRC
#+RESULTS:
|     |       col1 |     col2 |
|-----+------------+----------|
| foo |   0.363568 | 0.647676 |
| bar | 0.00663499 | 0.100717 |

I'm compensating for some bugs here. The tab[1:-1] is from, I think, tabulate, or perhaps IPython. I don't get results without this being a 
Since I use Org + IPython, I find that my 

On 2015-04-01 at 16:07, John Kitchin <jkitchin@andrew.cmu.edu> wrote:
> Hi everyone,
>
> In emacs-lisp, I can get a table as output that has a horizontal line
> in it like this:
>
> (append '((name scopus-id h-index n-docs n-citations))
>         '(hline)
>         (some expression that generates a list))
>
> The first row is header names, then a horizontal line, followed by a row
> for each thing of interest. This seems to work because the result is an
> emacs-lisp "array".
>
> I cannot figure out if this is possible in a Python block though. So far
> my experiments have failed because I don't know how to make an hline
> symbol in a Python array. Any kind of string just shows as a row. Any
> thoughts on if this is possible?
>
> thanks,


#+OPTIONS: tex:imagemagick
#+OPTIONS: toc:0

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: getting an hline in a python generated table
  2015-04-03 18:44 ` Ken Mankoff
@ 2015-04-03 18:46   ` Ken Mankoff
  2015-04-03 20:18     ` John Kitchin
  0 siblings, 1 reply; 7+ messages in thread
From: Ken Mankoff @ 2015-04-03 18:46 UTC (permalink / raw)
  To: John Kitchin; +Cc: Orgmode Mailing List


Sorry about that. I switched out of Org mode in my email client so C-c C-c sent the email instead of executing the code block. I was mid-sentence...

#+BEGIN_SRC python :results table :exports results
  from tabulate import tabulate
  import pandas as pd
  df = pd.DataFrame(np.random.rand(2,2), index=['foo','bar'])
  tab = tabulate(df, ['col1','col2'], tablefmt='orgtbl')
  print tab[1:-1]
#+END_SRC
#+RESULTS:

> I'm compensating for some bugs here. The tab[1:-1] is from, I think, tabulate, or perhaps IPython. I don't get results without this being a 
> Since I use Org + IPython, I find that my 
>
> On 2015-04-01 at 16:07, John Kitchin <jkitchin@andrew.cmu.edu> wrote:
>> Hi everyone,
>>
>> In emacs-lisp, I can get a table as output that has a horizontal line
>> in it like this:
>>
>> (append '((name scopus-id h-index n-docs n-citations))
>>         '(hline)
>>         (some expression that generates a list))
>>
>> The first row is header names, then a horizontal line, followed by a row
>> for each thing of interest. This seems to work because the result is an
>> emacs-lisp "array".
>>
>> I cannot figure out if this is possible in a Python block though. So far
>> my experiments have failed because I don't know how to make an hline
>> symbol in a Python array. Any kind of string just shows as a row. Any
>> thoughts on if this is possible?
>>
>> thanks,
>
>
> #+OPTIONS: tex:imagemagick
> #+OPTIONS: toc:0

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: getting an hline in a python generated table
  2015-04-03 18:46   ` Ken Mankoff
@ 2015-04-03 20:18     ` John Kitchin
  0 siblings, 0 replies; 7+ messages in thread
From: John Kitchin @ 2015-04-03 20:18 UTC (permalink / raw)
  To: Ken Mankoff; +Cc: Orgmode Mailing List

cool, thanks for the tip. I was not awware of that library.
Ken Mankoff writes:

> Sorry about that. I switched out of Org mode in my email client so C-c C-c sent the email instead of executing the code block. I was mid-sentence...
>
> #+BEGIN_SRC python :results table :exports results :session
>   from tabulate import tabulate
>   import pandas as pd
>   df = pd.DataFrame(np.random.rand(2,2), index=['foo','bar'])
>   tab = tabulate(df, ['col1','col2'], tablefmt='orgtbl')
>   tab[1:-1]
> #+END_SRC
> #+RESULTS:
> |     |     col1 |     col2 |
> |-----+----------+----------|
> | foo | 0.223272 | 0.433449 |
> | bar | 0.855854 |  0.13685 |
>
> I'm compensating for some bugs here. The tab[1:-1] is from, I think, tabulate, or perhaps IPython. I don't get results without this being a :session. If you don't use IPython/elpy in Org, then your final statement may need to be different (add a 'return'?) to get this to work.
>
> Hope this helps with w/ tables + headers,
>
>   -k.

--
Professor John Kitchin
Doherty Hall A207F
Department of Chemical Engineering
Carnegie Mellon University
Pittsburgh, PA 15213
412-268-7803
@johnkitchin
http://kitchingroup.cheme.cmu.edu

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: getting an hline in a python generated table
  2015-04-01 20:07 getting an hline in a python generated table John Kitchin
  2015-04-01 20:50 ` Rasmus
  2015-04-03 18:44 ` Ken Mankoff
@ 2015-04-03 20:42 ` William Henney
  2015-04-03 20:47   ` John Kitchin
  2 siblings, 1 reply; 7+ messages in thread
From: William Henney @ 2015-04-03 20:42 UTC (permalink / raw)
  To: John Kitchin; +Cc: Orgmode Mailing List

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

A simpler solution is to just use None, which gets automatically converted
to an hline by org-babel:

#+BEGIN_SRC python :return mytable
  NROWS, NCOLS = 6, 4
  mytable = []
  mytable.append(['A', 'B', 'C', 'D'])  # Table header
  mytable.append(None)                  # hline
  for irow in range(NROWS):
      mytable.append([icol**irow for icol in range(NCOLS)])
  mytable.append(None)                  # hline
#+END_SRC

#+RESULTS:
| A | B |  C |   D |
|---+---+----+-----|
| 1 | 1 |  1 |   1 |
| 0 | 1 |  2 |   3 |
| 0 | 1 |  4 |   9 |
| 0 | 1 |  8 |  27 |
| 0 | 1 | 16 |  81 |
| 0 | 1 | 32 | 243 |
|---+---+----+-----|

Will


On Wed, Apr 1, 2015 at 2:07 PM, John Kitchin <jkitchin@andrew.cmu.edu>
wrote:

> Hi everyone,
>
> In emacs-lisp, I can get a table as output that has a horizontal line
> in it like this:
>
> (append '((name scopus-id h-index n-docs n-citations))
>         '(hline)
>         (some expression that generates a list))
>
> The first row is header names, then a horizontal line, followed by a row
> for each thing of interest. This seems to work because the result is an
> emacs-lisp "array".
>
> I cannot figure out if this is possible in a Python block though. So far
> my experiments have failed because I don't know how to make an hline
> symbol in a Python array. Any kind of string just shows as a row. Any
> thoughts on if this is possible?
>
> thanks,
>
> --
> Professor John Kitchin
> Doherty Hall A207F
> Department of Chemical Engineering
> Carnegie Mellon University
> Pittsburgh, PA 15213
> 412-268-7803
> @johnkitchin
> http://kitchingroup.cheme.cmu.edu
>
>


-- 

  Dr William Henney, Centro de Radioastronomía y Astrofísica,
  Universidad Nacional Autónoma de México, Campus Morelia

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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: getting an hline in a python generated table
  2015-04-03 20:42 ` William Henney
@ 2015-04-03 20:47   ` John Kitchin
  0 siblings, 0 replies; 7+ messages in thread
From: John Kitchin @ 2015-04-03 20:47 UTC (permalink / raw)
  To: William Henney; +Cc: Orgmode Mailing List

wow, that is some wizardry there! I did not know you could do a return
value that way! or get an hline from None! thanks!

William Henney writes:

> A simpler solution is to just use None, which gets automatically converted
> to an hline by org-babel:
>
> #+BEGIN_SRC python :return mytable
>   NROWS, NCOLS = 6, 4
>   mytable = []
>   mytable.append(['A', 'B', 'C', 'D'])  # Table header
>   mytable.append(None)                  # hline
>   for irow in range(NROWS):
>       mytable.append([icol**irow for icol in range(NCOLS)])
>   mytable.append(None)                  # hline
> #+END_SRC
>
> #+RESULTS:
> | A | B |  C |   D |
> |---+---+----+-----|
> | 1 | 1 |  1 |   1 |
> | 0 | 1 |  2 |   3 |
> | 0 | 1 |  4 |   9 |
> | 0 | 1 |  8 |  27 |
> | 0 | 1 | 16 |  81 |
> | 0 | 1 | 32 | 243 |
> |---+---+----+-----|
>
> Will
>
>
> On Wed, Apr 1, 2015 at 2:07 PM, John Kitchin <jkitchin@andrew.cmu.edu>
> wrote:
>
>> Hi everyone,
>>
>> In emacs-lisp, I can get a table as output that has a horizontal line
>> in it like this:
>>
>> (append '((name scopus-id h-index n-docs n-citations))
>>         '(hline)
>>         (some expression that generates a list))
>>
>> The first row is header names, then a horizontal line, followed by a row
>> for each thing of interest. This seems to work because the result is an
>> emacs-lisp "array".
>>
>> I cannot figure out if this is possible in a Python block though. So far
>> my experiments have failed because I don't know how to make an hline
>> symbol in a Python array. Any kind of string just shows as a row. Any
>> thoughts on if this is possible?
>>
>> thanks,
>>
>> --
>> Professor John Kitchin
>> Doherty Hall A207F
>> Department of Chemical Engineering
>> Carnegie Mellon University
>> Pittsburgh, PA 15213
>> 412-268-7803
>> @johnkitchin
>> http://kitchingroup.cheme.cmu.edu
>>
>>

--
Professor John Kitchin
Doherty Hall A207F
Department of Chemical Engineering
Carnegie Mellon University
Pittsburgh, PA 15213
412-268-7803
@johnkitchin
http://kitchingroup.cheme.cmu.edu

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2015-04-03 20:47 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-04-01 20:07 getting an hline in a python generated table John Kitchin
2015-04-01 20:50 ` Rasmus
2015-04-03 18:44 ` Ken Mankoff
2015-04-03 18:46   ` Ken Mankoff
2015-04-03 20:18     ` John Kitchin
2015-04-03 20:42 ` William Henney
2015-04-03 20:47   ` John Kitchin

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).