class ToHTML(object): def setup(self): nrows = 500 self.df2 = DataFrame(np.random.randn(nrows, 10)) self.df2[0] = period_range('2000', periods=nrows) self.df2[1] = range(nrows) def time_to_html_mixed(self): self.df2.to_html()
def prepare_modeling_results(model,all_data,expnoList,path,exptitle): results_table = DataFrame(columns=['h','X','X model','S', 'S model', 'P', 'P model', 'expno', 'XnOBS', 'XnPRED', 'SnOBS', 'SnPRED', 'PnOBS', 'PnPRED']) for dataset1,expno1 in zip(all_data,expnoList): results_table1 = model.simulation(dataset1, expno=expno1) # calculate normalized values # XnOBS, XnPRED = feature_scaling(results_table1['X'].values,results_table1['X model'].values) # SnOBS, SnPRED = feature_scaling(results_table1['S'].values,results_table1['S model'].values) # PnOBS, PnPRED = feature_scaling(results_table1['P'].values,results_table1['P model'].values) # standardization XnOBS, XnPRED = zero_mean_variance(results_table1['X'].values,results_table1['X model'].values) SnOBS, SnPRED = zero_mean_variance(results_table1['S'].values,results_table1['S model'].values) PnOBS, PnPRED = zero_mean_variance(results_table1['P'].values,results_table1['P model'].values) # and add them to the table as new columns results_table1['XnOBS'] = XnOBS results_table1['XnPRED'] = XnPRED results_table1['SnOBS'] = SnOBS results_table1['SnPRED'] = SnPRED results_table1['PnOBS'] = PnOBS results_table1['PnPRED'] = PnPRED # now add the current experiment to the big table of all experiments results_table = results_table.append(results_table1) results_table.to_html("{0}results_table_{1}.html".format(path,exptitle)) return results_table
def test_to_html_invalid_classes_type(classes): # GH 25608 df = DataFrame() msg = "classes must be a string, list, or tuple" with pytest.raises(TypeError, match=msg): df.to_html(classes=classes)
def test_to_html_unicode(self): df = DataFrame({u('\u03c3'): np.arange(10.)}) expected = u'<table border="1" class="dataframe">\n <thead>\n <tr style="text-align: right;">\n <th></th>\n <th>\u03c3</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>0.0</td>\n </tr>\n <tr>\n <th>1</th>\n <td>1.0</td>\n </tr>\n <tr>\n <th>2</th>\n <td>2.0</td>\n </tr>\n <tr>\n <th>3</th>\n <td>3.0</td>\n </tr>\n <tr>\n <th>4</th>\n <td>4.0</td>\n </tr>\n <tr>\n <th>5</th>\n <td>5.0</td>\n </tr>\n <tr>\n <th>6</th>\n <td>6.0</td>\n </tr>\n <tr>\n <th>7</th>\n <td>7.0</td>\n </tr>\n <tr>\n <th>8</th>\n <td>8.0</td>\n </tr>\n <tr>\n <th>9</th>\n <td>9.0</td>\n </tr>\n </tbody>\n</table>' # noqa assert df.to_html() == expected df = DataFrame({'A': [u('\u03c3')]}) expected = u'<table border="1" class="dataframe">\n <thead>\n <tr style="text-align: right;">\n <th></th>\n <th>A</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>\u03c3</td>\n </tr>\n </tbody>\n</table>' # noqa assert df.to_html() == expected
def nice_charact(results_table1, **kwargs): """Returns nicely formatted DataFrame with process characteristics calculated from experimental data""" miu_high = estiamte_miu_high(results_table1['h'].values, results_table1['X'].values) Yxs = estiamte_Yxs(results_table1['X'].values, results_table1['S'].values) Yps = estiamte_Yxs(results_table1['P'].values, results_table1['S'].values) data_ch = DataFrame({'Parameters':['miu_high', 'Yxs', 'Yps']}) data_ch[kwargs['expno']] = [miu_high, Yxs, Yps] if 'path' in kwargs: # print("Saving requested...") if os.path.isfile(kwargs['path']+'.pickle'): # print("File exists...") all_data_ch = pd.read_pickle(kwargs['path']+'.pickle') all_data_ch[kwargs['expno']] = 0 all_data_ch.drop(kwargs['expno'], axis=1, inplace=True) all_data_ch = all_data_ch.merge(data_ch, on='Parameters') all_data_ch.to_pickle(kwargs['path']+'.pickle') all_data_ch.to_html(kwargs['path']+'.html') # print("File saved...") return all_data_ch else: print("File for characteristics does not exist...creating a new one.") data_ch.to_pickle(kwargs['path']+'.pickle') data_ch.to_html(kwargs['path']+'.html') # print("New file saved...") # print("Saving none...") return data_ch
def test_to_html_invalid_justify(self, justify): # see gh-17527 df = DataFrame() msg = "Invalid value for justify parameter" with tm.assert_raises_regex(ValueError, msg): df.to_html(justify=justify)
def test_to_html_invalid_justify(self, justify): # GH 17527 df = DataFrame() msg = "Invalid value for justify parameter" with pytest.raises(ValueError, match=msg): df.to_html(justify=justify)
def test_to_html_unicode(self, datapath): df = DataFrame({u('\u03c3'): np.arange(10.)}) expected = expected_html(datapath, 'unicode_1') assert df.to_html() == expected df = DataFrame({'A': [u('\u03c3')]}) expected = expected_html(datapath, 'unicode_2') assert df.to_html() == expected
def test_to_html_with_classes(self, datapath): df = DataFrame() result = df.to_html(classes="sortable draggable") expected = expected_html(datapath, 'with_classes') assert result == expected result = df.to_html(classes=["sortable", "draggable"]) assert result == expected
def test_to_html_round_column_headers(): # GH 17280 df = DataFrame([1], columns=[0.55555]) with pd.option_context('display.precision', 3): html = df.to_html(notebook=False) notebook = df.to_html(notebook=True) assert "0.55555" in html assert "0.556" in notebook
def test_to_html_float_format_no_fixed_width(self, datapath): # GH 21625 df = DataFrame({'x': [0.19999]}) expected = expected_html(datapath, 'gh21625_expected_output') assert df.to_html(float_format='%.3f') == expected # GH 22270 df = DataFrame({'x': [100.0]}) expected = expected_html(datapath, 'gh22270_expected_output') assert df.to_html(float_format='%.0f') == expected
def test_to_html_multiindex_sparsify(self, datapath): index = MultiIndex.from_arrays([[0, 0, 1, 1], [0, 1, 0, 1]], names=['foo', None]) df = DataFrame([[0, 1], [2, 3], [4, 5], [6, 7]], index=index) result = df.to_html() expected = expected_html(datapath, 'multiindex_sparsify_1') assert result == expected df = DataFrame([[0, 1], [2, 3], [4, 5], [6, 7]], columns=index[::2], index=index) result = df.to_html() expected = expected_html(datapath, 'multiindex_sparsify_2') assert result == expected
def test_to_html_multiindex_odd_even_truncate(self, datapath): # GH 14882 - Issue on truncation with odd length DataFrame mi = MultiIndex.from_product([[100, 200, 300], [10, 20, 30], [1, 2, 3, 4, 5, 6, 7]], names=['a', 'b', 'c']) df = DataFrame({'n': range(len(mi))}, index=mi) result = df.to_html(max_rows=60) expected = expected_html(datapath, 'gh14882_expected_output_1') assert result == expected # Test that ... appears in a middle level result = df.to_html(max_rows=56) expected = expected_html(datapath, 'gh14882_expected_output_2') assert result == expected
def test_parse_dates_list(self): df = DataFrame({'date': date_range('1/1/2001', periods=10)}) expected = df.to_html() res = self.read_html(expected, parse_dates=[1], index_col=0) tm.assert_frame_equal(df, res[0]) res = self.read_html(expected, parse_dates=['date'], index_col=0) tm.assert_frame_equal(df, res[0])
def test_to_html_truncate_multi_index_sparse_off(self, datapath): arrays = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'], ['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']] df = DataFrame(index=arrays, columns=arrays) result = df.to_html(max_rows=7, max_cols=7, sparsify=False) expected = expected_html(datapath, 'truncate_multi_index_sparse_off') assert result == expected
def test_to_html_datetime64_monthformatter(self): months = [datetime(2016, 1, 1), datetime(2016, 2, 2)] x = DataFrame({'months': months}) def format_func(x): return x.strftime('%Y-%m') result = x.to_html(formatters={'months': format_func}) expected = """\ <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>months</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>2016-01</td> </tr> <tr> <th>1</th> <td>2016-02</td> </tr> </tbody> </table>""" assert result == expected
def test_to_html_datetime64_hourformatter(self): x = DataFrame({'hod': pd.to_datetime(['10:10:10.100', '12:12:12.120'], format='%H:%M:%S.%f')}) def format_func(x): return x.strftime('%H:%M') result = x.to_html(formatters={'hod': format_func}) expected = """\ <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>hod</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>10:10</td> </tr> <tr> <th>1</th> <td>12:12</td> </tr> </tbody> </table>""" assert result == expected
def test_to_html_multiindex_max_cols(self): # GH 6131 index = MultiIndex(levels=[['ba', 'bb', 'bc'], ['ca', 'cb', 'cc']], labels=[[0, 1, 2], [0, 1, 2]], names=['b', 'c']) columns = MultiIndex(levels=[['d'], ['aa', 'ab', 'ac']], labels=[[0, 0, 0], [0, 1, 2]], names=[None, 'a']) data = np.array( [[1., np.nan, np.nan], [np.nan, 2., np.nan], [np.nan, np.nan, 3.]]) df = DataFrame(data, index, columns) result = df.to_html(max_cols=2) expected = dedent("""\ <table border="1" class="dataframe"> <thead> <tr> <th></th> <th></th> <th colspan="3" halign="left">d</th> </tr> <tr> <th></th> <th>a</th> <th>aa</th> <th>...</th> <th>ac</th> </tr> <tr> <th>b</th> <th>c</th> <th></th> <th></th> <th></th> </tr> </thead> <tbody> <tr> <th>ba</th> <th>ca</th> <td>1.0</td> <td>...</td> <td>NaN</td> </tr> <tr> <th>bb</th> <th>cb</th> <td>NaN</td> <td>...</td> <td>NaN</td> </tr> <tr> <th>bc</th> <th>cc</th> <td>NaN</td> <td>...</td> <td>3.0</td> </tr> </tbody> </table>""") assert result == expected
def test_to_html_float_format_no_fixed_width( value, float_format, expected, datapath): # GH 21625, GH 22270 df = DataFrame({'x': [value]}) expected = expected_html(datapath, expected) result = df.to_html(float_format=float_format) assert result == expected
def test_to_html_decimal(self): # GH 12031 df = DataFrame({'A': [6.0, 3.1, 2.2]}) result = df.to_html(decimal=',') expected = ('<table border="1" class="dataframe">\n' ' <thead>\n' ' <tr style="text-align: right;">\n' ' <th></th>\n' ' <th>A</th>\n' ' </tr>\n' ' </thead>\n' ' <tbody>\n' ' <tr>\n' ' <th>0</th>\n' ' <td>6,0</td>\n' ' </tr>\n' ' <tr>\n' ' <th>1</th>\n' ' <td>3,1</td>\n' ' </tr>\n' ' <tr>\n' ' <th>2</th>\n' ' <td>2,2</td>\n' ' </tr>\n' ' </tbody>\n' '</table>') assert result == expected
def pandas_open_in_browser(df): import tempfile htmlfile = tempfile.NamedTemporaryFile(delete=False, suffix=".html") htmlfile.write(df.to_html().encode("utf-8")) htmlfile.close() os.system("$BROWSER '{}'".format(htmlfile.name)) os.unlink(htmlfile.name)
def test_to_html_multiindex_index_false(self, datapath): # GH 8452 df = DataFrame({ 'a': range(2), 'b': range(3, 5), 'c': range(5, 7), 'd': range(3, 5) }) df.columns = MultiIndex.from_product([['a', 'b'], ['c', 'd']]) result = df.to_html(index=False) expected = expected_html(datapath, 'gh8452_expected_output') assert result == expected df.index = Index(df.index.values, name='idx') result = df.to_html(index=False) assert result == expected
def test_parse_dates_combine(self): raw_dates = Series(date_range('1/1/2001', periods=10)) df = DataFrame({'date': raw_dates.map(lambda x: str(x.date())), 'time': raw_dates.map(lambda x: str(x.time()))}) res = self.read_html(df.to_html(), parse_dates={'datetime': [1, 2]}, index_col=1) newdf = DataFrame({'datetime': raw_dates}) tm.assert_frame_equal(newdf, res[0])
def test_to_html_multiindex_sparsify_false_multi_sparse(self, datapath): with option_context('display.multi_sparse', False): index = MultiIndex.from_arrays([[0, 0, 1, 1], [0, 1, 0, 1]], names=['foo', None]) df = DataFrame([[0, 1], [2, 3], [4, 5], [6, 7]], index=index) result = df.to_html() expected = expected_html( datapath, 'multiindex_sparsify_false_multi_sparse_1') assert result == expected df = DataFrame([[0, 1], [2, 3], [4, 5], [6, 7]], columns=index[::2], index=index) result = df.to_html() expected = expected_html( datapath, 'multiindex_sparsify_false_multi_sparse_2') assert result == expected
def test_to_html_index_formatter(self, datapath): df = DataFrame([[0, 1], [2, 3], [4, 5], [6, 7]], columns=['foo', None], index=lrange(4)) f = lambda x: 'abcd' [x] result = df.to_html(formatters={'__index__': f}) expected = expected_html(datapath, 'index_formatter') assert result == expected
def test_to_html_truncation_index_false_max_cols(self, datapath, index): # GH 22783 data = [[1.764052, 0.400157, 0.978738, 2.240893, 1.867558], [-0.977278, 0.950088, -0.151357, -0.103219, 0.410599]] df = DataFrame(data) result = df.to_html(max_cols=4, index=index) expected = expected_html(datapath, 'gh22783_expected_output') assert result == expected
def test_to_html_multiindex(self, datapath): columns = MultiIndex.from_tuples(list(zip(np.arange(2).repeat(2), np.mod(lrange(4), 2))), names=['CL0', 'CL1']) df = DataFrame([list('abcd'), list('efgh')], columns=columns) result = df.to_html(justify='left') expected = expected_html(datapath, 'multiindex_1') assert result == expected columns = MultiIndex.from_tuples(list(zip( range(4), np.mod( lrange(4), 2)))) df = DataFrame([list('abcd'), list('efgh')], columns=columns) result = df.to_html(justify='right') expected = expected_html(datapath, 'multiindex_2') assert result == expected
def test_to_html_justify(self, justify, datapath): df = DataFrame({'A': [6, 30000, 2], 'B': [1, 2, 70000], 'C': [223442, 0, 1]}, columns=['A', 'B', 'C']) result = df.to_html(justify=justify) expected = expected_html(datapath, 'justify').format(justify=justify) assert result == expected
def test_to_html_multiindex_index_false(self): # issue 8452 df = DataFrame({ 'a': range(2), 'b': range(3, 5), 'c': range(5, 7), 'd': range(3, 5) }) df.columns = MultiIndex.from_product([['a', 'b'], ['c', 'd']]) result = df.to_html(index=False) expected = """\ <table border="1" class="dataframe"> <thead> <tr> <th colspan="2" halign="left">a</th> <th colspan="2" halign="left">b</th> </tr> <tr> <th>c</th> <th>d</th> <th>c</th> <th>d</th> </tr> </thead> <tbody> <tr> <td>0</td> <td>3</td> <td>5</td> <td>3</td> </tr> <tr> <td>1</td> <td>4</td> <td>6</td> <td>4</td> </tr> </tbody> </table>""" assert result == expected df.index = Index(df.index.values, name='idx') result = df.to_html(index=False) assert result == expected
def test_to_html_datetime64_monthformatter(self, datapath): months = [datetime(2016, 1, 1), datetime(2016, 2, 2)] x = DataFrame({'months': months}) def format_func(x): return x.strftime('%Y-%m') result = x.to_html(formatters={'months': format_func}) expected = expected_html(datapath, 'datetime64_monthformatter') assert result == expected
def test_to_html_with_classes(classes, datapath): df = DataFrame() expected = expected_html(datapath, 'with_classes') result = df.to_html(classes=classes) assert result == expected
def test_to_html_no_index_max_rows(datapath): # GH 14998 df = DataFrame({"A": [1, 2, 3, 4]}) result = df.to_html(index=False, max_rows=1) expected = expected_html(datapath, 'gh14998_expected_output') assert result == expected
def test_to_html_with_empty_string_label(self): # GH3547, to_html regards empty string labels as repeated labels data = {'c1': ['a', 'b'], 'c2': ['a', ''], 'data': [1, 2]} df = DataFrame(data).set_index(['c1', 'c2']) res = df.to_html() assert "rowspan" not in res
def df_to_html(df: pd.DataFrame, image_cols: List[str] = [], hyperlink_cols: List[str] = [], html_tags: Dict[str, Union[str, List[str]]] = dict(), transpose: bool = False, image_width: Optional[int] = None, max_num_rows: int = 200, **kwargs) -> str: """ Convert a Pandas DataFrame to HTML. Parameters ---------- df: DataFrame DataFrame to convert to HTML image_cols: str or list Column names that contain image urls or file paths. Columns specified as images will make all other transformations to those columns be ignored. Local files will display correctly in Jupyter if specified using relative paths but not if specified using absolute paths (see https://github.com/jupyter/notebook/issues/3810). hyperlink_cols: str or list Column names that contain hyperlinks to open in a new tab html_tags: dictionary A transformation to be inserted directly into the HTML tag. Ex: ``{'col_name_1': 'strong'}`` becomes ``<strong>col_name_1</strong>`` Ex: ``{'col_name_2': 'mark'}`` becomes ``<mark>col_name_2</mark>`` Ex: ``{'col_name_3': 'h2'}`` becomes ``<h2>col_name_3</h2>`` Ex: ``{'col_name_4': ['em', 'strong']}`` becomes ``<em><strong>col_name_4</strong></em>`` transpose: bool Transpose the DataFrame before converting to HTML image_width: int Set image width for each image generated max_num_rows: int Maximum number of rows to display **kwargs: keyword arguments Additional arguments sent to ``pandas.DataFrame.to_html``, as listed in: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_html.html Returns ------- df_html: HTML DataFrame converted to a HTML string, ready for displaying Examples -------- In a Jupyter notebook: .. code-block:: python from IPython.core.display import display, HTML import pandas as pd df = pd.DataFrame({ 'item': ['Beefy Fritos® Burrito'], 'price': ['1.00'], 'image_url': ['https://www.tacobell.com/images/22480_beefy_fritos_burrito_269x269.jpg'], }) display( HTML( df_to_html( df, image_cols='image_url', html_tags={'item': 'strong', 'price': 'em'}, image_width=200, ) ) ) Note ---- Converted table will have CSS class 'dataframe', unless otherwise specified. """ def _wrap_cols_if_needed(cols: [str, List[str]]) -> List[str]: """Necessary for columns named with integers.""" try: iter(cols) except TypeError: cols = [cols] if isinstance(cols, str): cols = [cols] return cols if max_num_rows is None or len(df) <= max_num_rows: df = df.copy() # copy the dataframe so we don't edit the original! else: # explicit copy eliminates a warning we don't need df = df.head(max_num_rows).copy() image_cols = _wrap_cols_if_needed(image_cols) for image_col in image_cols: if image_col not in df.columns: raise ValueError('{} not a column in df!'.format(image_col)) if not image_width: df[image_col] = df[image_col].map(lambda x: f'<img src="{x}">') else: df[image_col] = df[image_col].map( lambda x: f'<img src="{x}" width={image_width}>') hyperlink_cols = _wrap_cols_if_needed(hyperlink_cols) for hyperlink_col in hyperlink_cols: if hyperlink_col not in df.columns: raise ValueError('{} not a column in df!'.format(hyperlink_col)) if hyperlink_col in image_cols: continue df[hyperlink_col] = (df[hyperlink_col].map( lambda x: f'<a target="_blank" href="{x}">{x}</a>')) for col, transformations in html_tags.items(): if col not in df.columns: raise ValueError(f'{col} not a column in df!') if col in image_cols: continue if isinstance(transformations, str): transformations = [transformations] opening_tag = '' for extra in transformations: opening_tag += f'<{extra}>' closing_tag = '' for extra in transformations[::-1]: closing_tag += f'</{extra}>' df[col] = df[col].map(lambda x: f'{opening_tag}{x}{closing_tag}') max_colwidth = pd.get_option('display.max_colwidth') if pd.__version__ != '0': # this option is not backwards compatible with Pandas v1.0.0 pd.set_option('display.max_colwidth', None) else: pd.set_option('display.max_colwidth', -1) if transpose: df = df.T df_html = df.to_html(escape=False, **kwargs) pd.set_option('display.max_colwidth', max_colwidth) return df_html
def test_to_html_timestamp(self): rng = date_range("2000-01-01", periods=10) df = DataFrame(np.random.randn(10, 4), index=rng) result = df.to_html() assert "2000-01-01" in result
def test_to_html(self): # big mixed biggie = DataFrame( { 'A': np.random.randn(200), 'B': tm.makeStringIndex(200) }, index=lrange(200)) biggie.loc[:20, 'A'] = np.nan biggie.loc[:20, 'B'] = np.nan s = biggie.to_html() buf = StringIO() retval = biggie.to_html(buf=buf) assert retval is None assert buf.getvalue() == s assert isinstance(s, compat.string_types) biggie.to_html(columns=['B', 'A'], col_space=17) biggie.to_html(columns=['B', 'A'], formatters={'A': lambda x: '{x:.1f}'.format(x=x)}) biggie.to_html(columns=['B', 'A'], float_format=str) biggie.to_html(columns=['B', 'A'], col_space=12, float_format=str) frame = DataFrame(index=np.arange(200)) frame.to_html()
def test_to_html_columns_arg(): df = DataFrame(tm.getSeriesData()) result = df.to_html(columns=['A']) assert '<th>B</th>' not in result
def test_to_html_columns_arg(self): frame = DataFrame(tm.getSeriesData()) result = frame.to_html(columns=['A']) assert '<th>B</th>' not in result
def test_to_html_with_no_bold(self): x = DataFrame({'x': np.random.randn(5)}) ashtml = x.to_html(bold_rows=False) assert '<strong' not in ashtml[ashtml.find("</thead>")]
def test_to_html_multiindex(self): columns = MultiIndex.from_tuples(list( zip(np.arange(2).repeat(2), np.mod(lrange(4), 2))), names=['CL0', 'CL1']) df = DataFrame([list('abcd'), list('efgh')], columns=columns) result = df.to_html(justify='left') expected = ('<table border="1" class="dataframe">\n' ' <thead>\n' ' <tr>\n' ' <th>CL0</th>\n' ' <th colspan="2" halign="left">0</th>\n' ' <th colspan="2" halign="left">1</th>\n' ' </tr>\n' ' <tr>\n' ' <th>CL1</th>\n' ' <th>0</th>\n' ' <th>1</th>\n' ' <th>0</th>\n' ' <th>1</th>\n' ' </tr>\n' ' </thead>\n' ' <tbody>\n' ' <tr>\n' ' <th>0</th>\n' ' <td>a</td>\n' ' <td>b</td>\n' ' <td>c</td>\n' ' <td>d</td>\n' ' </tr>\n' ' <tr>\n' ' <th>1</th>\n' ' <td>e</td>\n' ' <td>f</td>\n' ' <td>g</td>\n' ' <td>h</td>\n' ' </tr>\n' ' </tbody>\n' '</table>') assert result == expected columns = MultiIndex.from_tuples( list(zip(range(4), np.mod(lrange(4), 2)))) df = DataFrame([list('abcd'), list('efgh')], columns=columns) result = df.to_html(justify='right') expected = ('<table border="1" class="dataframe">\n' ' <thead>\n' ' <tr>\n' ' <th></th>\n' ' <th>0</th>\n' ' <th>1</th>\n' ' <th>2</th>\n' ' <th>3</th>\n' ' </tr>\n' ' <tr>\n' ' <th></th>\n' ' <th>0</th>\n' ' <th>1</th>\n' ' <th>0</th>\n' ' <th>1</th>\n' ' </tr>\n' ' </thead>\n' ' <tbody>\n' ' <tr>\n' ' <th>0</th>\n' ' <td>a</td>\n' ' <td>b</td>\n' ' <td>c</td>\n' ' <td>d</td>\n' ' </tr>\n' ' <tr>\n' ' <th>1</th>\n' ' <td>e</td>\n' ' <td>f</td>\n' ' <td>g</td>\n' ' <td>h</td>\n' ' </tr>\n' ' </tbody>\n' '</table>') assert result == expected
def test_to_html_index(self): index = ['foo', 'bar', 'baz'] df = DataFrame( { 'A': [1, 2, 3], 'B': [1.2, 3.4, 5.6], 'C': ['one', 'two', np.nan] }, columns=['A', 'B', 'C'], index=index) expected_with_index = ('<table border="1" class="dataframe">\n' ' <thead>\n' ' <tr style="text-align: right;">\n' ' <th></th>\n' ' <th>A</th>\n' ' <th>B</th>\n' ' <th>C</th>\n' ' </tr>\n' ' </thead>\n' ' <tbody>\n' ' <tr>\n' ' <th>foo</th>\n' ' <td>1</td>\n' ' <td>1.2</td>\n' ' <td>one</td>\n' ' </tr>\n' ' <tr>\n' ' <th>bar</th>\n' ' <td>2</td>\n' ' <td>3.4</td>\n' ' <td>two</td>\n' ' </tr>\n' ' <tr>\n' ' <th>baz</th>\n' ' <td>3</td>\n' ' <td>5.6</td>\n' ' <td>NaN</td>\n' ' </tr>\n' ' </tbody>\n' '</table>') assert df.to_html() == expected_with_index expected_without_index = ('<table border="1" class="dataframe">\n' ' <thead>\n' ' <tr style="text-align: right;">\n' ' <th>A</th>\n' ' <th>B</th>\n' ' <th>C</th>\n' ' </tr>\n' ' </thead>\n' ' <tbody>\n' ' <tr>\n' ' <td>1</td>\n' ' <td>1.2</td>\n' ' <td>one</td>\n' ' </tr>\n' ' <tr>\n' ' <td>2</td>\n' ' <td>3.4</td>\n' ' <td>two</td>\n' ' </tr>\n' ' <tr>\n' ' <td>3</td>\n' ' <td>5.6</td>\n' ' <td>NaN</td>\n' ' </tr>\n' ' </tbody>\n' '</table>') result = df.to_html(index=False) for i in index: assert i not in result assert result == expected_without_index df.index = Index(['foo', 'bar', 'baz'], name='idx') expected_with_index = ('<table border="1" class="dataframe">\n' ' <thead>\n' ' <tr style="text-align: right;">\n' ' <th></th>\n' ' <th>A</th>\n' ' <th>B</th>\n' ' <th>C</th>\n' ' </tr>\n' ' <tr>\n' ' <th>idx</th>\n' ' <th></th>\n' ' <th></th>\n' ' <th></th>\n' ' </tr>\n' ' </thead>\n' ' <tbody>\n' ' <tr>\n' ' <th>foo</th>\n' ' <td>1</td>\n' ' <td>1.2</td>\n' ' <td>one</td>\n' ' </tr>\n' ' <tr>\n' ' <th>bar</th>\n' ' <td>2</td>\n' ' <td>3.4</td>\n' ' <td>two</td>\n' ' </tr>\n' ' <tr>\n' ' <th>baz</th>\n' ' <td>3</td>\n' ' <td>5.6</td>\n' ' <td>NaN</td>\n' ' </tr>\n' ' </tbody>\n' '</table>') assert df.to_html() == expected_with_index assert df.to_html(index=False) == expected_without_index tuples = [('foo', 'car'), ('foo', 'bike'), ('bar', 'car')] df.index = MultiIndex.from_tuples(tuples) expected_with_index = ('<table border="1" class="dataframe">\n' ' <thead>\n' ' <tr style="text-align: right;">\n' ' <th></th>\n' ' <th></th>\n' ' <th>A</th>\n' ' <th>B</th>\n' ' <th>C</th>\n' ' </tr>\n' ' </thead>\n' ' <tbody>\n' ' <tr>\n' ' <th rowspan="2" valign="top">foo</th>\n' ' <th>car</th>\n' ' <td>1</td>\n' ' <td>1.2</td>\n' ' <td>one</td>\n' ' </tr>\n' ' <tr>\n' ' <th>bike</th>\n' ' <td>2</td>\n' ' <td>3.4</td>\n' ' <td>two</td>\n' ' </tr>\n' ' <tr>\n' ' <th>bar</th>\n' ' <th>car</th>\n' ' <td>3</td>\n' ' <td>5.6</td>\n' ' <td>NaN</td>\n' ' </tr>\n' ' </tbody>\n' '</table>') assert df.to_html() == expected_with_index result = df.to_html(index=False) for i in ['foo', 'bar', 'car', 'bike']: assert i not in result # must be the same result as normal index assert result == expected_without_index df.index = MultiIndex.from_tuples(tuples, names=['idx1', 'idx2']) expected_with_index = ('<table border="1" class="dataframe">\n' ' <thead>\n' ' <tr style="text-align: right;">\n' ' <th></th>\n' ' <th></th>\n' ' <th>A</th>\n' ' <th>B</th>\n' ' <th>C</th>\n' ' </tr>\n' ' <tr>\n' ' <th>idx1</th>\n' ' <th>idx2</th>\n' ' <th></th>\n' ' <th></th>\n' ' <th></th>\n' ' </tr>\n' ' </thead>\n' ' <tbody>\n' ' <tr>\n' ' <th rowspan="2" valign="top">foo</th>\n' ' <th>car</th>\n' ' <td>1</td>\n' ' <td>1.2</td>\n' ' <td>one</td>\n' ' </tr>\n' ' <tr>\n' ' <th>bike</th>\n' ' <td>2</td>\n' ' <td>3.4</td>\n' ' <td>two</td>\n' ' </tr>\n' ' <tr>\n' ' <th>bar</th>\n' ' <th>car</th>\n' ' <td>3</td>\n' ' <td>5.6</td>\n' ' <td>NaN</td>\n' ' </tr>\n' ' </tbody>\n' '</table>') assert df.to_html() == expected_with_index assert df.to_html(index=False) == expected_without_index
def test_to_html_multiindex_sparsify(self): index = MultiIndex.from_arrays([[0, 0, 1, 1], [0, 1, 0, 1]], names=['foo', None]) df = DataFrame([[0, 1], [2, 3], [4, 5], [6, 7]], index=index) result = df.to_html() expected = """<table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th></th> <th>0</th> <th>1</th> </tr> <tr> <th>foo</th> <th></th> <th></th> <th></th> </tr> </thead> <tbody> <tr> <th rowspan="2" valign="top">0</th> <th>0</th> <td>0</td> <td>1</td> </tr> <tr> <th>1</th> <td>2</td> <td>3</td> </tr> <tr> <th rowspan="2" valign="top">1</th> <th>0</th> <td>4</td> <td>5</td> </tr> <tr> <th>1</th> <td>6</td> <td>7</td> </tr> </tbody> </table>""" assert result == expected df = DataFrame([[0, 1], [2, 3], [4, 5], [6, 7]], columns=index[::2], index=index) result = df.to_html() expected = """\ <table border="1" class="dataframe"> <thead> <tr> <th></th> <th>foo</th> <th>0</th> <th>1</th> </tr> <tr> <th></th> <th></th> <th>0</th> <th>0</th> </tr> <tr> <th>foo</th> <th></th> <th></th> <th></th> </tr> </thead> <tbody> <tr> <th rowspan="2" valign="top">0</th> <th>0</th> <td>0</td> <td>1</td> </tr> <tr> <th>1</th> <td>2</td> <td>3</td> </tr> <tr> <th rowspan="2" valign="top">1</th> <th>0</th> <td>4</td> <td>5</td> </tr> <tr> <th>1</th> <td>6</td> <td>7</td> </tr> </tbody> </table>""" assert result == expected
def test_to_html_border(self): df = DataFrame({'A': [1, 2]}) result = df.to_html() assert 'border="1"' in result
def test_to_html_decimal(datapath): # GH 12031 df = DataFrame({'A': [6.0, 3.1, 2.2]}) result = df.to_html(decimal=',') expected = expected_html(datapath, 'gh12031_expected_output') assert result == expected
def test_to_html_multiindex(columns, justify, expected, datapath): df = DataFrame([list('abcd'), list('efgh')], columns=columns) result = df.to_html(justify=justify) expected = expected_html(datapath, expected) assert result == expected
def test_to_html_border_option(self): df = DataFrame({'A': [1, 2]}) with option_context('display.html.border', 0): result = df.to_html() assert 'border="0"' in result assert 'border="0"' in df._repr_html_()
def test_to_html_truncate(self, datapath): index = pd.date_range(start='20010101', freq='D', periods=20) df = DataFrame(index=index, columns=range(20)) result = df.to_html(max_rows=8, max_cols=4) expected = expected_html(datapath, 'truncate') assert result == expected
def test_to_html_with_no_bold(self): x = DataFrame({'x': randn(5)}) ashtml = x.to_html(bold_rows=False) assert ('<strong>' not in ashtml)
def test_to_html_with_no_bold(): df = DataFrame({'x': np.random.randn(5)}) html = df.to_html(bold_rows=False) result = html[html.find("</thead>")] assert '<strong' not in result
def test_to_html_unicode(self): # it works! df = DataFrame({u'\u03c3': np.arange(10.)}) df.to_html() df = DataFrame({'A': [u'\u03c3']}) df.to_html()
def test_to_html_truncate_multi_index_sparse_off(self): arrays = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'], ['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']] df = DataFrame(index=arrays, columns=arrays) result = df.to_html(max_rows=7, max_cols=7, sparsify=False) expected = '''\ <table border="1" class="dataframe"> <thead> <tr> <th></th> <th></th> <th>bar</th> <th>bar</th> <th>baz</th> <th>...</th> <th>foo</th> <th>qux</th> <th>qux</th> </tr> <tr> <th></th> <th></th> <th>one</th> <th>two</th> <th>one</th> <th>...</th> <th>two</th> <th>one</th> <th>two</th> </tr> </thead> <tbody> <tr> <th>bar</th> <th>one</th> <td>NaN</td> <td>NaN</td> <td>NaN</td> <td>...</td> <td>NaN</td> <td>NaN</td> <td>NaN</td> </tr> <tr> <th>bar</th> <th>two</th> <td>NaN</td> <td>NaN</td> <td>NaN</td> <td>...</td> <td>NaN</td> <td>NaN</td> <td>NaN</td> </tr> <tr> <th>baz</th> <th>one</th> <td>NaN</td> <td>NaN</td> <td>NaN</td> <td>...</td> <td>NaN</td> <td>NaN</td> <td>NaN</td> </tr> <tr> <th>foo</th> <th>two</th> <td>NaN</td> <td>NaN</td> <td>NaN</td> <td>...</td> <td>NaN</td> <td>NaN</td> <td>NaN</td> </tr> <tr> <th>qux</th> <th>one</th> <td>NaN</td> <td>NaN</td> <td>NaN</td> <td>...</td> <td>NaN</td> <td>NaN</td> <td>NaN</td> </tr> <tr> <th>qux</th> <th>two</th> <td>NaN</td> <td>NaN</td> <td>NaN</td> <td>...</td> <td>NaN</td> <td>NaN</td> <td>NaN</td> </tr> </tbody> </table>''' assert result == expected
def test_to_html_border_zero(self): df = DataFrame({'A': [1, 2]}) result = df.to_html(border=0) assert 'border="0"' in result
def main(): print "Name of Script: ", sys.argv[0] print "ACONC FILE: ", sys.argv[1] print "GRIDCRO2D FILE: ", sys.argv[2] files = glob(sys.argv[1]) grid = sys.argv[2] va = verify.verify_airnow(concpath=files, gridcro=grid, datapath='AIRNOW.hdf', combine=True, neighbors=9, user='******', passw='p00pST!ck123') regions = arange(7).astype('|S15') regions[:] = 'Domain' stats = [ 'Obs Mean', 'Model Mean', 'Mean Bias', 'Fractional Bias', 'RMSE', 'd1', 'E1' ] c1 = regions c2 = array(stats) #species list spec = va.df.Species.unique() c3 = empty((array(stats).shape[0], spec.shape[0])) print c3.shape print c1.shape, c2.shape for i, j in enumerate(spec): c3[:, i] = get_stats(va.df.groupby('Species').get_group(j)) for k, region in enumerate(va.df['Region'].dropna().unique()): temp = arange(7).astype('|S15') temp[:] = region c1 = append(c1, temp) c2 = append(c2, array(stats)) dftemp = va.df.groupby('Region').get_group(region) c = empty((array(stats).shape[0], spec.shape[0])) c[:] = 0. for i, j in enumerate(spec): if j not in dftemp.Species.unique(): c[:, i] = NaN else: c[:, i] = get_stats(dftemp.groupby('Species').get_group(j)) c3 = concatenate([c3, c], axis=0) tuples = list(zip(*[c1.tolist(), c2.tolist()])) print tuples index = MultiIndex.from_tuples(tuples, names=['Area', 'Statistic']) print c1.shape, c2.shape print c3.shape print c3 df = DataFrame(c3.T, columns=index, index=spec).round(decimals=3).T df.to_html('test.html') cssstyle = '<style>\n.GenericTable\n{\nfont-size:12px;\ncolor:white;\nborder-width: 1px;\nborder-color: rgb(160,160,160);/* This is dark*/\nborder-collapse: collapse;\n}\n.GenericTable th\n{\nfont-size:16px;\ncolor:white;\nbackground-color:rgb(100,100,100);/* This is dark*/\nborder-width: 1px;\npadding: 4px;\nborder-style: solid;\nborder-color: rgb(192, 192, 192);/* This is light*/\ntext-align:left;\n}\n.GenericTable tr\n{\ncolor:black;\nbackground-color:rgb(224, 224, 224);/* This is light*/\n}\n.GenericTable td\n{\nfont-size:14px;\nborder-width: 1px;\nborder-style: solid;\nborder-color: rgb(255, 255, 255);/* This is dark*/\n}\n.hoverTable{\nwidth:100%; \nborder-collapse:collapse; \n}\n.hoverTable td{ \npadding:7px; border:#E0E0E0 1px solid;\n}\n/* Define the default color for all the table rows */\n.hoverTable tr{\nbackground: #C0C0C0;\n}\n/* Define the hover highlight color for the table row */\n .hoverTable tr:hover {\n background-color: #ffff99;\n }\n</style>' lines = cssstyle.split('\n') with open('test.html', 'r') as f: for line in f.readlines(): lines.append( line.replace('class="dataframe"', 'class="GenericTable hoverTable"')) f.close() with open('test.html', 'w') as f: for line in lines: f.write(line) f.close()
def test_to_html_truncate(self): index = pd.DatetimeIndex(start='20010101', freq='D', periods=20) df = DataFrame(index=index, columns=range(20)) result = df.to_html(max_rows=8, max_cols=4) expected = '''\ <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th>0</th> <th>1</th> <th>...</th> <th>18</th> <th>19</th> </tr> </thead> <tbody> <tr> <th>2001-01-01</th> <td>NaN</td> <td>NaN</td> <td>...</td> <td>NaN</td> <td>NaN</td> </tr> <tr> <th>2001-01-02</th> <td>NaN</td> <td>NaN</td> <td>...</td> <td>NaN</td> <td>NaN</td> </tr> <tr> <th>2001-01-03</th> <td>NaN</td> <td>NaN</td> <td>...</td> <td>NaN</td> <td>NaN</td> </tr> <tr> <th>2001-01-04</th> <td>NaN</td> <td>NaN</td> <td>...</td> <td>NaN</td> <td>NaN</td> </tr> <tr> <th>...</th> <td>...</td> <td>...</td> <td>...</td> <td>...</td> <td>...</td> </tr> <tr> <th>2001-01-17</th> <td>NaN</td> <td>NaN</td> <td>...</td> <td>NaN</td> <td>NaN</td> </tr> <tr> <th>2001-01-18</th> <td>NaN</td> <td>NaN</td> <td>...</td> <td>NaN</td> <td>NaN</td> </tr> <tr> <th>2001-01-19</th> <td>NaN</td> <td>NaN</td> <td>...</td> <td>NaN</td> <td>NaN</td> </tr> <tr> <th>2001-01-20</th> <td>NaN</td> <td>NaN</td> <td>...</td> <td>NaN</td> <td>NaN</td> </tr> </tbody> </table>''' assert result == expected
def postprocess(self, pimp, output_dir): result = OrderedDict() def parse_pairwise(p): """parse pimp's way of having pairwise parameters as key as str and return list of individuals""" res = [tmp.strip('\' ') for tmp in p.strip('[]').split(',')] return res parameter_imp = {k: v * 100 for k, v in pimp.evaluator.evaluated_parameter_importance.items()} param_imp_std = {} if hasattr(pimp.evaluator, 'evaluated_parameter_importance_uncertainty'): param_imp_std = {k: v * 100 for k, v in pimp.evaluator.evaluated_parameter_importance_uncertainty.items()} for k in parameter_imp.keys(): self.logger.debug("fanova-importance for %s: mean (over trees): %f, std: %s", k, parameter_imp[k], str(param_imp_std[k]) if param_imp_std else 'N/A') # Split single and pairwise (pairwise are string: "['p1','p2']") single_imp = {k: v for k, v in parameter_imp.items() if not k.startswith('[') and v > self.marginal_threshold} pairwise_imp = {k: v for k, v in parameter_imp.items() if k.startswith('[') and v > self.marginal_threshold} # Set internal parameter importance for further analysis (such as parallel coordinates) self.fanova_single_importance = single_imp self.fanova_pairwise_importance = single_imp # Dicts to lists of tuples, sorted descending after importance single_imp = OrderedDict(sorted(single_imp.items(), key=operator.itemgetter(1), reverse=True)) pairwise_imp = OrderedDict(sorted(pairwise_imp.items(), key=operator.itemgetter(1), reverse=True)) # Create table table = [] if len(single_imp) > 0: table.extend([(20*"-"+" Single importance: "+20*"-", 20*"-")]) for k, v in single_imp.items(): value = str(round(v, 4)) if param_imp_std: value += " +/- " + str(round(param_imp_std[k], 4)) table.append((k, value)) if len(pairwise_imp) > 0: table.extend([(20*"-"+" Pairwise importance: "+20*"-", 20*"-")]) for k, v in pairwise_imp.items(): name = ' & '.join(parse_pairwise(k)) value = str(round(v, 4)) if param_imp_std: value += " +/- " + str(round(param_imp_std[k], 4)) table.append((name, value)) keys, fanova_table = [k[0] for k in table], [k[1:] for k in table] df = DataFrame(data=fanova_table, index=keys) result['Importance'] = {'table': df.to_html(escape=False, header=False, index=True, justify='left')} # Get plot-paths result['Marginals'] = {p: {'figure': os.path.join(output_dir, "fanova", p + '.png')} for p in single_imp.keys()} # Right now no way to access paths of the plots -> file issue pairwise_plots = {" & ".join(parse_pairwise(p)): os.path.join(output_dir, 'fanova', '_'.join(parse_pairwise(p)) + '.png') for p in pairwise_imp.keys()} result['Pairwise Marginals'] = {p: {'figure': path} for p, path in pairwise_plots.items() if os.path.exists(path)} return result
def test_to_html_with_id(): # GH 8496 df = DataFrame({"A": [1, 2]}, index=Index(['a', 'b'], name='myindexname')) result = df.to_html(index_names=False, table_id="TEST_ID") assert ' id="TEST_ID"' in result
def test_to_html(self): # big mixed biggie = DataFrame({ 'A': randn(200), 'B': tm.makeStringIndex(200) }, index=range(200)) biggie['A'][:20] = nan biggie['B'][:20] = nan s = biggie.to_html() buf = StringIO() retval = biggie.to_html(buf=buf) self.assert_(retval is None) self.assertEqual(buf.getvalue(), s) self.assert_(isinstance(s, basestring)) biggie.to_html(columns=['B', 'A'], col_space=17) biggie.to_html(columns=['B', 'A'], formatters={'A': lambda x: '%.1f' % x}) biggie.to_html(columns=['B', 'A'], float_format=str) biggie.to_html(columns=['B', 'A'], col_space=12, float_format=str) frame = DataFrame(index=np.arange(200)) frame.to_html()
def test_to_html_with_index_names_false(): # GH 16493 df = DataFrame({"A": [1, 2]}, index=Index(['a', 'b'], name='myindexname')) result = df.to_html(index_names=False) assert 'myindexname' not in result
def data_frame_to_html_simple(df: pd.DataFrame) -> str: html = df.to_html(index=True) html = html.replace('\\n', '<br>') return html
def test_to_html_multiindex_odd_even_truncate(self): # GH 14882 - Issue on truncation with odd length DataFrame mi = MultiIndex.from_product( [[100, 200, 300], [10, 20, 30], [1, 2, 3, 4, 5, 6, 7]], names=['a', 'b', 'c']) df = DataFrame({'n': range(len(mi))}, index=mi) result = df.to_html(max_rows=60) expected = """\ <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th></th> <th></th> <th>n</th> </tr> <tr> <th>a</th> <th>b</th> <th>c</th> <th></th> </tr> </thead> <tbody> <tr> <th rowspan="21" valign="top">100</th> <th rowspan="7" valign="top">10</th> <th>1</th> <td>0</td> </tr> <tr> <th>2</th> <td>1</td> </tr> <tr> <th>3</th> <td>2</td> </tr> <tr> <th>4</th> <td>3</td> </tr> <tr> <th>5</th> <td>4</td> </tr> <tr> <th>6</th> <td>5</td> </tr> <tr> <th>7</th> <td>6</td> </tr> <tr> <th rowspan="7" valign="top">20</th> <th>1</th> <td>7</td> </tr> <tr> <th>2</th> <td>8</td> </tr> <tr> <th>3</th> <td>9</td> </tr> <tr> <th>4</th> <td>10</td> </tr> <tr> <th>5</th> <td>11</td> </tr> <tr> <th>6</th> <td>12</td> </tr> <tr> <th>7</th> <td>13</td> </tr> <tr> <th rowspan="7" valign="top">30</th> <th>1</th> <td>14</td> </tr> <tr> <th>2</th> <td>15</td> </tr> <tr> <th>3</th> <td>16</td> </tr> <tr> <th>4</th> <td>17</td> </tr> <tr> <th>5</th> <td>18</td> </tr> <tr> <th>6</th> <td>19</td> </tr> <tr> <th>7</th> <td>20</td> </tr> <tr> <th rowspan="19" valign="top">200</th> <th rowspan="7" valign="top">10</th> <th>1</th> <td>21</td> </tr> <tr> <th>2</th> <td>22</td> </tr> <tr> <th>3</th> <td>23</td> </tr> <tr> <th>4</th> <td>24</td> </tr> <tr> <th>5</th> <td>25</td> </tr> <tr> <th>6</th> <td>26</td> </tr> <tr> <th>7</th> <td>27</td> </tr> <tr> <th rowspan="5" valign="top">20</th> <th>1</th> <td>28</td> </tr> <tr> <th>2</th> <td>29</td> </tr> <tr> <th>...</th> <td>...</td> </tr> <tr> <th>6</th> <td>33</td> </tr> <tr> <th>7</th> <td>34</td> </tr> <tr> <th rowspan="7" valign="top">30</th> <th>1</th> <td>35</td> </tr> <tr> <th>2</th> <td>36</td> </tr> <tr> <th>3</th> <td>37</td> </tr> <tr> <th>4</th> <td>38</td> </tr> <tr> <th>5</th> <td>39</td> </tr> <tr> <th>6</th> <td>40</td> </tr> <tr> <th>7</th> <td>41</td> </tr> <tr> <th rowspan="21" valign="top">300</th> <th rowspan="7" valign="top">10</th> <th>1</th> <td>42</td> </tr> <tr> <th>2</th> <td>43</td> </tr> <tr> <th>3</th> <td>44</td> </tr> <tr> <th>4</th> <td>45</td> </tr> <tr> <th>5</th> <td>46</td> </tr> <tr> <th>6</th> <td>47</td> </tr> <tr> <th>7</th> <td>48</td> </tr> <tr> <th rowspan="7" valign="top">20</th> <th>1</th> <td>49</td> </tr> <tr> <th>2</th> <td>50</td> </tr> <tr> <th>3</th> <td>51</td> </tr> <tr> <th>4</th> <td>52</td> </tr> <tr> <th>5</th> <td>53</td> </tr> <tr> <th>6</th> <td>54</td> </tr> <tr> <th>7</th> <td>55</td> </tr> <tr> <th rowspan="7" valign="top">30</th> <th>1</th> <td>56</td> </tr> <tr> <th>2</th> <td>57</td> </tr> <tr> <th>3</th> <td>58</td> </tr> <tr> <th>4</th> <td>59</td> </tr> <tr> <th>5</th> <td>60</td> </tr> <tr> <th>6</th> <td>61</td> </tr> <tr> <th>7</th> <td>62</td> </tr> </tbody> </table>""" assert result == expected # Test that ... appears in a middle level result = df.to_html(max_rows=56) expected = """\ <table border="1" class="dataframe"> <thead> <tr style="text-align: right;"> <th></th> <th></th> <th></th> <th>n</th> </tr> <tr> <th>a</th> <th>b</th> <th>c</th> <th></th> </tr> </thead> <tbody> <tr> <th rowspan="21" valign="top">100</th> <th rowspan="7" valign="top">10</th> <th>1</th> <td>0</td> </tr> <tr> <th>2</th> <td>1</td> </tr> <tr> <th>3</th> <td>2</td> </tr> <tr> <th>4</th> <td>3</td> </tr> <tr> <th>5</th> <td>4</td> </tr> <tr> <th>6</th> <td>5</td> </tr> <tr> <th>7</th> <td>6</td> </tr> <tr> <th rowspan="7" valign="top">20</th> <th>1</th> <td>7</td> </tr> <tr> <th>2</th> <td>8</td> </tr> <tr> <th>3</th> <td>9</td> </tr> <tr> <th>4</th> <td>10</td> </tr> <tr> <th>5</th> <td>11</td> </tr> <tr> <th>6</th> <td>12</td> </tr> <tr> <th>7</th> <td>13</td> </tr> <tr> <th rowspan="7" valign="top">30</th> <th>1</th> <td>14</td> </tr> <tr> <th>2</th> <td>15</td> </tr> <tr> <th>3</th> <td>16</td> </tr> <tr> <th>4</th> <td>17</td> </tr> <tr> <th>5</th> <td>18</td> </tr> <tr> <th>6</th> <td>19</td> </tr> <tr> <th>7</th> <td>20</td> </tr> <tr> <th rowspan="15" valign="top">200</th> <th rowspan="7" valign="top">10</th> <th>1</th> <td>21</td> </tr> <tr> <th>2</th> <td>22</td> </tr> <tr> <th>3</th> <td>23</td> </tr> <tr> <th>4</th> <td>24</td> </tr> <tr> <th>5</th> <td>25</td> </tr> <tr> <th>6</th> <td>26</td> </tr> <tr> <th>7</th> <td>27</td> </tr> <tr> <th>...</th> <th>...</th> <td>...</td> </tr> <tr> <th rowspan="7" valign="top">30</th> <th>1</th> <td>35</td> </tr> <tr> <th>2</th> <td>36</td> </tr> <tr> <th>3</th> <td>37</td> </tr> <tr> <th>4</th> <td>38</td> </tr> <tr> <th>5</th> <td>39</td> </tr> <tr> <th>6</th> <td>40</td> </tr> <tr> <th>7</th> <td>41</td> </tr> <tr> <th rowspan="21" valign="top">300</th> <th rowspan="7" valign="top">10</th> <th>1</th> <td>42</td> </tr> <tr> <th>2</th> <td>43</td> </tr> <tr> <th>3</th> <td>44</td> </tr> <tr> <th>4</th> <td>45</td> </tr> <tr> <th>5</th> <td>46</td> </tr> <tr> <th>6</th> <td>47</td> </tr> <tr> <th>7</th> <td>48</td> </tr> <tr> <th rowspan="7" valign="top">20</th> <th>1</th> <td>49</td> </tr> <tr> <th>2</th> <td>50</td> </tr> <tr> <th>3</th> <td>51</td> </tr> <tr> <th>4</th> <td>52</td> </tr> <tr> <th>5</th> <td>53</td> </tr> <tr> <th>6</th> <td>54</td> </tr> <tr> <th>7</th> <td>55</td> </tr> <tr> <th rowspan="7" valign="top">30</th> <th>1</th> <td>56</td> </tr> <tr> <th>2</th> <td>57</td> </tr> <tr> <th>3</th> <td>58</td> </tr> <tr> <th>4</th> <td>59</td> </tr> <tr> <th>5</th> <td>60</td> </tr> <tr> <th>6</th> <td>61</td> </tr> <tr> <th>7</th> <td>62</td> </tr> </tbody> </table>""" assert result == expected