0
這是針對this的後續問題。使用熊貓風格將數千個分隔符添加到具有空字符串的數據框中
使用熊貓風格,我設法將數據框中的所有值格式化爲包含千位分隔符逗號的值。但是,如果數據框中有空字符串,則格式化失敗。
誰能幫助我?
這是我到目前爲止的代碼:
import pandas as pd
from IPython.display import HTML
styles = [
hover(),
dict(selector = "th",
props = [("font-size", "110%"),
("text-align", "left"),
("background-color", "#cacaca")
]
)
]
column_01 = ["", 2000000000, "", 21000000, 3000]
df = pd.DataFrame(column_01)
int_frmt = lambda x: "{:,}".format(x) # Integer
float_frmt = lambda x: "{:,.0f}".format(x) if x > 1e3 else "{:,.2f}".format(x) # Float
str_frmt = lambda x: "{:}".format(x) # <----- Added for empty strings but fails
frmt_map = {np.dtype("int64"): int_frmt,
np.dtype("float64"): float_frmt,
np.dtype("S"): str_frmt # <----- Added for empty strings but fails
}
frmt = {col: frmt_map[df.dtypes[col]] for col in df.columns if df.dtypes[col] in frmt_map.keys()}
html = (df.style.set_table_styles(styles).format(frmt))
html
嗨馬丁。你的代碼是否也在'IPython.display Html'中工作? – Fxs7576