def _normalize_tabular_data(tabular_data, headers, sort=True):
if hasattr(tabular_data, "<STR_LIT>") and hasattr(tabular_data, "<STR_LIT>"):<EOL><INDENT>if hasattr(tabular_data.values, "<STR_LIT>"):<EOL><INDENT>keys = list(tabular_data.keys())<EOL>rows = list(izip_longest(*list(tabular_data.values())))<EOL><DEDENT>elif hasattr(tabular_data, "<STR_LIT:index>"):<EOL><INDENT>keys = list(tabular_data.keys())<EOL>vals = tabular_data.values<EOL>names = tabular_data.index<EOL>rows = [[v] + list(row) for v, row in zip(names, vals)]<EOL><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>if headers == "<STR_LIT>":<EOL><INDENT>headers = list(map(_text_type, keys))  <EOL><DEDENT><DEDENT>else:  <EOL><INDENT>rows = list(tabular_data)<EOL>if headers == "<STR_LIT>" and len(rows) > <NUM_LIT:0>:  <EOL><INDENT>headers = list(map(_text_type, list(range(len(rows[<NUM_LIT:0>])))))<EOL><DEDENT><DEDENT>if headers == "<STR_LIT>" and len(rows) > <NUM_LIT:0>:<EOL><INDENT>headers = list(map(_text_type, rows[<NUM_LIT:0>]))  <EOL>rows = rows[<NUM_LIT:1>:]<EOL><DEDENT>headers = list(headers)<EOL>rows = list(map(list, rows))<EOL>if sort and len(rows) > <NUM_LIT:1>:<EOL><INDENT>rows = sorted(rows, key=lambda x: x[<NUM_LIT:0>])<EOL><DEDENT>if headers and len(rows) > <NUM_LIT:0>:<EOL><INDENT>nhs = len(headers)<EOL>ncols = len(rows[<NUM_LIT:0>])<EOL>if nhs < ncols:<EOL><INDENT>headers = ["<STR_LIT>"] * (ncols - nhs) + headers<EOL><DEDENT><DEDENT>return rows, headers<EOL>
Transform a supported data type to a list of lists, and a list of headers.

Supported tabular data types:

* list-of-lists or another iterable of iterables

* 2D NumPy arrays

* dict of iterables (usually used with headers="keys")

* pandas.DataFrame (usually used with headers="keys")

The first row can be used as headers if headers="firstrow",
column indices can be used as headers if headers="keys".