简体   繁体   中英

Passing pandas groupby result to html in a pretty way

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()

在此处输入图片说明

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

使用reset_index()您的GroupBy对象将使你能够把它作为一个正常的数据帧,即适用to_html它。

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM