From the docs for c it is used to
Combine Values into a Vector or List
In python you can combine values into a list using brackets so c(1, 2, 3) in R become [1, 2, 3] in Python. But if you didn't know this, I really urge you to stop porting the code and take two days to go through a basic Python tutorial. Understanding lists in Python is about as basic as you get.
The docs for tabulate state
tabulatetakes the integer-valued vectorbinand counts the number of times each integer occurs in it.
this sounds just like numpy's bincount
Count number of occurrences of each value in array of non-negative ints.
unless you have a requirement to also count negative ints? If not, it's pretty much a drop in replacement. So tabulate(c(2,3,3,5), nbins = 10) becomes np.bincount(np.array([2, 3, 3, 5]), minlength=10)
Which is the most powerful Python module for tabulating content in command line?
Python format tabular output - Stack Overflow
python - Printing Lists as Tabular Data - Stack Overflow
python - Nicely tabulate a list of strings for printing - Code Review Stack Exchange
Videos
It would be nice if the package allows the functionality to sort the table data alphabetically, and deal with the columns and row of the table separately.
It's not really hard to roll your own formatting function:
def print_table(table):
col_width = [max(len(x) for x in col) for col in zip(*table)]
for line in table:
print "| " + " | ".join("{:{}}".format(x, col_width[i])
for i, x in enumerate(line)) + " |"
table = [(str(x), str(f(x))) for x in mylist]
print_table(table)
For a more beautiful table, use the tabulate module:
Tabulate link
Example from the tabulate documentation:
>>> from tabulate import tabulate
>>> table = [["Sun",696000,1989100000],["Earth",6371,5973.6],
... ["Moon",1737,73.5],["Mars",3390,641.85]]
>>> print(tabulate(table))
----- ------ -------------
Sun 696000 1.9891e+09
Earth 6371 5973.6
Moon 1737 73.5
Mars 3390 641.85
----- ------ -------------
There are some light and useful python packages for this purpose:
1. tabulate
https://pypi.python.org/pypi/tabulate
from tabulate import tabulate
print(tabulate([['Alice', 24], ['Bob', 19]], headers=['Name', 'Age']))
Name Age
------ -----
Alice 24
Bob 19
tabulate has many options to specify headers and table format.
print(tabulate(
[['Alice', 24], ['Bob', 19]],
headers=['Name', 'Age'],
tablefmt='orgtbl'))
| Name | Age |
|--------+-------|
| Alice | 24 |
| Bob | 19 |
2. PrettyTable
https://pypi.python.org/pypi/PrettyTable
from prettytable import PrettyTable
t = PrettyTable(['Name', 'Age'])
t.add_row(['Alice', 24])
t.add_row(['Bob', 19])
print(t)
+-------+-----+
| Name | Age |
+-------+-----+
| Alice | 24 |
| Bob | 19 |
+-------+-----+
PrettyTable has options to read data from csv, html, sql database. Also you are able to select subset of data, sort table and change table styles.
3. texttable
https://pypi.python.org/pypi/texttable
from texttable import Texttable
t = Texttable()
t.add_rows([['Name', 'Age'], ['Alice', 24], ['Bob', 19]])
print(t.draw())
+-------+-----+
| Name | Age |
+=======+=====+
| Alice | 24 |
+-------+-----+
| Bob | 19 |
+-------+-----+
with texttable you can control horizontal/vertical align, border style and data types.
4. termtables
https://github.com/nschloe/termtables
import termtables as tt
string = tt.to_string(
[["Alice", 24], ["Bob", 19]],
header=["Name", "Age"],
style=tt.styles.ascii_thin_double,
# alignment="ll",
# padding=(0, 1),
)
print(string)
+-------+-----+
| Name | Age |
+=======+=====+
| Alice | 24 |
+-------+-----+
| Bob | 19 |
+-------+-----+
with texttable you can control horizontal/vertical align, border style and data types.
Other options
- terminaltables - Easily draw tables in terminal/console applications from a list of lists of strings. Supports multi-line rows.
- asciitable can read and write a wide range of ASCII table formats via built-in Extension Reader Classes.
Some ad-hoc code:
row_format ="{:>15}" * (len(teams_list) + 1)
print(row_format.format("", *teams_list))
for team, row in zip(teams_list, data):
print(row_format.format(team, *row))
This relies on str.format() and the Format Specification Mini-Language.
Β» pip install tabulate
I seem to be having good output with prettytable:
from prettytable import PrettyTable
x = PrettyTable(dat.dtype.names)
for row in dat:
x.add_row(row)
# Change some column alignments; default was 'c'
x.align['column_one'] = 'r'
x.align['col_two'] = 'r'
x.align['column_3'] = 'l'
And the output is not bad. There is even a border switch, among a few other options:
>>> print(x)
+------------+---------+-------------+
| column_one | col_two | column_3 |
+------------+---------+-------------+
| 0 | 0.0001 | ABCD |
| 1 | 1e-05 | ABCD |
| 2 | 1e-06 | long string |
| 3 | 1e-07 | ABCD |
+------------+---------+-------------+
>>> print(x.get_string(border=False))
column_one col_two column_3
0 0.0001 ABCD
1 1e-05 ABCD
2 1e-06 long string
3 1e-07 ABCD
The tabulate package works nicely for Numpy arrays:
import numpy as np
from tabulate import tabulate
m = np.array([[1, 2, 3], [4, 5, 6]])
headers = ["col 1", "col 2", "col 3"]
# Generate the table in fancy format.
table = tabulate(m, headers, tablefmt="fancy_grid")
# Show it.
print(table)
Output:
βββββββββββ€ββββββββββ€ββββββββββ
β col 1 β col 2 β col 3 β
βββββββββββͺββββββββββͺββββββββββ‘
β 1 β 2 β 3 β
βββββββββββΌββββββββββΌββββββββββ€
β 4 β 5 β 6 β
βββββββββββ§ββββββββββ§ββββββββββ
The package can be installed from PyPI using e.g.
$ pip install tabulate