import numpy as np
import pandas as pd
# 方法一
new_df = df.round({"A列": 2,
"B列": 2,
})
# 方法二
改之前先转成str,而且不可以用数据清洗astype的方式(我也有点奇怪)
data["A"].astype(str)
data["A"] = data["A"].map(lambda x:f"{x:,.2f}")
format函数的其他格式:
{:.2f} 保留小数点后两位
{:+.2f} 带符号保留小数点后两位
{:+.2f} 带符号保留小数点后两位
{:.0f} 不带小数
{:0>2d} 数字补零 (填充左边, 宽度为2)
{:x<4d} 数字补x (填充右边, 宽度为4)
{:x<4d} 数字补x (填充右边, 宽度为4)
{:,} 以逗号分隔的数字格式
{:.2} 百分比格式(2后加百分号)
{:.2e} 指数记法
{:>10d} 右对齐 (默认, 宽度为10)
{:<10d} 左对齐 (宽度为10)
{:^10d} 中间对齐 (宽度为10)