import pandas as pd
import numpy as np
from IPython.display import HTML
import functools
def borderTop(strRow, srRow):
#print(strRow, type(sRow), sRow.name)
if srRow.name == strRow:
return [ "border-top: 1pt solid black; font-weight: bold" for sCol in srRow ]
else:
return [""] * len(srRow)
np.random.seed(0) # to make the numbers reproducible
dfBody = pd.DataFrame(np.random.randint(0,10,(5,3)))
# make a column of row totals and append it on the right
srRowTots = dfBody.sum(axis=1).astype(int)
srRowTots.name="Total"
dfBody2 = pd.concat([dfBody, srRowTots], axis=1)
# make a row of column totals and append to the bottom
srColTots = dfBody2.sum().astype(int)
srColTots.name="Total"
dfBodyTots = pd.concat([dfBody2, srColTots.to_frame().T])
# Display it, making the total column and row bold and bordered
dfBodyTots.style\
.set_properties(subset=['Total'], **{'font-weight': 'bold', "border-left": "1pt solid black"})\
.apply(functools.partial(borderTop, "Total"), axis=1)
以下是我正在使用的:
相关问题 更多 >
编程相关推荐