thombashi / pytablewriter

pytablewriter is a Python library to write a table in various formats: AsciiDoc / CSV / Elasticsearch / HTML / JavaScript / JSON / LaTeX / LDJSON / LTSV / Markdown / MediaWiki / NumPy / Excel / Pandas / Python / reStructuredText / SQLite / TOML / TSV.
https://pytablewriter.rtfd.io/
MIT License
611 stars 43 forks source link

Markdown output alignment #2

Closed jpoa closed 6 years ago

jpoa commented 6 years ago

Greetings!

I am outputting a pandas dataframe to markdown and I fail to see (and find in the documentation) how I can define the alignment of the columns in the markdown table.

I have found some references in the code, so there is some logic to define which columns are left or right aligned, but how to define that when doing the conversion?

Best regards!

thombashi commented 6 years ago

Hi @jpoa , pytablewriter will automatically determine alignment for each of the columns from the data type. Integer or real-numbers are left aligned, other data types will right aligned.

For example:

df = pd.read_csv(six.StringIO(dedent("""\
    "i","f","c","if","ifc","bool","inf","nan","mix_num","time"
    1,1.10,"aa",1.0,"1",True,Infinity,NaN,1,"2017-01-01 00:00:00+09:00"
    22,2.20,"bbb",2.2,"2.2",False,Infinity,NaN,Infinity,"2017-01-02 03:04:05+09:00"
    333,3.33,"cccc",-3.0,"ccc",True,Infinity,NaN,NaN,"2017-01-01 00:00:00+09:00"
    """)), sep=',')

writer = pytablewriter.MarkdownTableWriter()
writer.from_dataframe(df)
writer.write_table()
| i | f  | c  | if |ifc|bool |  inf   |nan|mix_num |          time           |
|--:|---:|----|---:|---|-----|--------|---|-------:|-------------------------|
|  1|1.10|aa  | 1.0|  1|True |Infinity|NaN|       1|2017-01-01 00:00:00+09:00|
| 22|2.20|bbb | 2.2|2.2|False|Infinity|NaN|Infinity|2017-01-02 03:04:05+09:00|
|333|3.33|cccc|-3.0|ccc|True |Infinity|NaN|     NaN|2017-01-01 00:00:00+09:00|
i f c if ifc bool inf nan mix_num time
1 1.10 aa 1.0 1 True Infinity NaN 1 2017-01-01 00:00:00+09:00
22 2.20 bbb 2.2 2.2 False Infinity NaN Infinity 2017-01-02 03:04:05+09:00
333 3.33 cccc -3.0 ccc True Infinity NaN NaN 2017-01-01 00:00:00+09:00

Let me confirm. What you trying is to specify the alignment of columns in the table with manually?

jpoa commented 6 years ago

Yes, I give you a use case.

In Switzerland the thousands separator is an apostrophe, this (as far as I could attain) is not supported despite PEP378 (the apostrophe in particular is not supported).

So in the very final step I set another thousands separator, set the number as a string and replace the separator, problem solved. But when I output the alignment is gone because its no longer a number (I have also observed the behaviour you mention).

Therefore would be nice to define the alignment specifically (I am sure for other cases would also be interesting).

Cheers!

On Sat, Jul 21, 2018 at 1:43 AM Tsuyoshi Hombashi notifications@github.com wrote:

Hi @jpoa https://github.com/jpoa , pytablewriter will automatically determine alignment for each of the columns from the data type. Integer or real-numbers are left aligned, other data types will right aligned.

For example:

df = pd.read_csv(six.StringIO(dedent("""\ "i","f","c","if","ifc","bool","inf","nan","mix_num","time" 1,1.10,"aa",1.0,"1",True,Infinity,NaN,1,"2017-01-01 00:00:00+09:00" 22,2.20,"bbb",2.2,"2.2",False,Infinity,NaN,Infinity,"2017-01-02 03:04:05+09:00" 333,3.33,"cccc",-3.0,"ccc",True,Infinity,NaN,NaN,"2017-01-01 00:00:00+09:00" """)), sep=',')

writer = pytablewriter.MarkdownTableWriter() writer.from_dataframe(df) writer.write_table()

i f c if ifc bool inf nan mix_num time
1 1.10 aa 1.0 1 True Infinity NaN 1 2017-01-01 00:00:00+09:00
22 2.20 bbb 2.2 2.2 False Infinity NaN Infinity 2017-01-02 03:04:05+09:00
333 3.33 cccc -3.0 ccc True Infinity NaN NaN 2017-01-01 00:00:00+09:00

i f c if ifc bool inf nan mix_num time 1 1.10 aa 1.0 1 True Infinity NaN 1 2017-01-01 00:00:00+09:00 22 2.20 bbb 2.2 2.2 False Infinity NaN Infinity 2017-01-02 03:04:05+09:00 333 3.33 cccc -3.0 ccc True Infinity NaN NaN 2017-01-01 00:00:00+09:00

Let me confirm. What you trying is to specify the alignment of columns in the table with manually?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/thombashi/pytablewriter/issues/2#issuecomment-406752391, or mute the thread https://github.com/notifications/unsubscribe-auth/ABEHfEE0xluUVduYoeVWjc1_x9yOxrk-ks5uImsugaJpZM4VX_WO .

thombashi commented 6 years ago

@jpoa Thank you for your additional information.

I had implement the interface at pytablewriter 0.31.0. You can set alignment for each column manually by align_list attribute. Usage is as follows:

from pytablewriter import Align, MarkdownTableWriter

writer = MarkdownTableWriter()
writer.table_name = "specify alignment for each column manually"
writer.header_list = ["left", "right", "center", "auto (int)", "auto (str)", "None (auto)"]
writer.value_matrix = [
    [0, "r", "center align", 0, "a", "n"],
    [11, "right align", "c", 11, "auto", "none"],
]
writer.align_list = [Align.LEFT, Align.RIGHT, Align.CENTER, Align.AUTO, Align.AUTO, None]
writer.write_table()
# specify alignment for each column manually
|left|   right   |   center   |auto (int)|auto (str)|None (auto)|
|----|----------:|:----------:|---------:|----------|-----------|
|0   |          r|center align|         0|a         |n          |
|11  |right align|     c      |        11|auto      |none       |

specify alignment for each column manually

left right center auto (int) auto (str) None (auto)
0 r center align 0 a n
11 right align c 11 auto none

Any feedback or comments would be appreciated.

thombashi commented 6 years ago

I'll close the issue. Feel free to reopen if you still have any problems about the issue.

thombashi commented 5 years ago

I integrated align_list attribute into style_list attribute at pytablewriter 0.37.0. align_list can still be used (may be removed in the future release)

https://pytablewriter.readthedocs.io/en/latest/pages/examples/style/index.html