4.4.3. Using pandas DataFrame as a tabular data source

You can use pandas.DataFrame as a data source to write a table.

from_dataframe method of writer classes will set up tabular data from pandas.DataFrame:

Sample Code:
Write a markdown table from a pandas DataFrame
from textwrap import dedent
import pandas as pd
import io
from pytablewriter import MarkdownTableWriter

def main():
    csv_data = io.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"
        2,2.20,"bbb",2.2,"2.2",False,Infinity,NaN,Infinity,"2017-01-02 03:04:05+09:00"
        3,3.33,"cccc",-3.0,"ccc",True,Infinity,NaN,NaN,"2017-01-01 00:00:00+09:00"
        """))
    df = pd.read_csv(csv_data, sep=',')

    writer = MarkdownTableWriter(dataframe=df)
    writer.write_table()

if __name__ == "__main__":
    main()
Output:
| 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|
|  2|2.20|bbb | 2.2|2.2|False|Infinity|NaN|Infinity|2017-01-02 03:04:05+09:00|
|  3|3.33|cccc|-3.0|ccc|True |Infinity|NaN|     NaN|2017-01-01 00:00:00+09:00|

Adding a column of the DataFrame index if you specify add_index_column=True:

Sample Code:
Write a markdown table from a pandas DataFrame
import pandas as pd
import pytablewriter as ptw

def main():
    writer = ptw.MarkdownTableWriter(table_name="add_index_column")
    writer.from_dataframe(
        pd.DataFrame({"A": [1, 2], "B": [10, 11]}, index=["a", "b"]),
        add_index_column=True,
    )
    writer.write_table()

if __name__ == "__main__":
    main()
Output:
# add_index_column
|   | A | B |
|---|--:|--:|
|a  |  1| 10|
|b  |  2| 11|