1

I wonder how could I pass python pandas groupby result to html formatted such as printed in console. Pic below. to_html does not work because It says that

Series object has no attribute to_html()

enter image description here

The one on the left is from console the one from the right is from my html view.

Exa
  • 3,941
  • 6
  • 45
  • 59
Mr Lukas
  • 103
  • 1
  • 9

2 Answers2

2

Using reset_index() on your GroupBy object will enable you to treat it as a normal DataFrame i.e. apply to_html to it.

iacob
  • 14,010
  • 5
  • 54
  • 92
0

You can make sure you output a DataFrame, even if the output is a single series.

I can think of two ways.

results_series = df[column_name]  # your results, returns a series
# method 1: select column from list, as a DataFrame
results_df = df[[column_name]] # returns a DataFrame
# method 2: after selection, generate a new DataFrame
results_df = pd.DataFrame(results_series)
# then, export to html
results_df.to_html('output.html')
YardenR
  • 171
  • 1
  • 11