Skip to content

Instantly share code, notes, and snippets.

@drorata
Last active May 9, 2022 14:12
Show Gist options
  • Select an option

  • Save drorata/b05bfd59c45eec0470f6 to your computer and use it in GitHub Desktop.

Select an option

Save drorata/b05bfd59c45eec0470f6 to your computer and use it in GitHub Desktop.
Nice output of pandas.DataFrame in org-mode
#+BEGIN_SRC python :results raw
import pandas as pd
import numpy as np
from tabulate import tabulate
df = pd.DataFrame(np.random.random((4,3)), columns=['A','B','C'])
print("foo")
return(tabulate(df, headers="keys", tablefmt="orgtbl"))
#+END_SRC
#+RESULTS:
| | A | B | C |
|---+----------+----------+----------|
| 0 | 0.754799 | 0.492722 | 0.144595 |
| 1 | 0.198475 | 0.417721 | 0.083459 |
| 2 | 0.645011 | 0.444857 | 0.278874 |
| 3 | 0.314883 | 0.7521 | 0.789418 |
And ~:session~ version:
#+BEGIN_SRC python :results raw :session foo
import pandas as pd
import numpy as np
from tabulate import tabulate
df = pd.DataFrame(np.random.random((4,3)), columns=['A','B','C'])
print("foo")
tabulate(df, headers="keys" ,headers="keys",tablefmt="orgtbl")
#+END_SRC
#+RESULTS:
| | A | B | C |
|---+-----------+-----------+----------|
| 0 | 0.732506 | 0.508531 | 0.651031 |
| 1 | 0.0187486 | 0.467498 | 0.391717 |
| 2 | 0.231897 | 0.0988542 | 0.170519 |
| 3 | 0.742887 | 0.10542 | 0.271825 |
@nakami

nakami commented Jul 23, 2018

Copy link
Copy Markdown

see
tabulate(df, headers="keys" ,headers="keys",tablefmt="orgtbl")

cannot use same keyword twice (headers)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment