转换之前
question_id sum count mean
0 1 15 24 0.625000
1 2 1 1 1.000000
2 3 6 16 0.375000
3 4 3 5 0.600000
4 5 6 8 0.750000
5 7 11 16 0.687500
6 11 5 8 0.625000
7 12 1 3 0.333333
8 15 16 27 0.592593
9 16 2 5 0.400000
数据类型
question_id int64
sum int64
count int64
mean float64
dtype: object
methods
res['a']=res['mean'].round(decimals=2)
res['b']=res['mean'].map(lambda x:("%.2f")%x)
res['c']=res['mean'].map(lambda x:format(x,".2%"))
print(res)
print(res.dtypes)
结果
question_id sum count mean a b c
0 1 15 24 0.625000 0.62 0.62 62.50%
1 2 1 1 1.000000 1.00 1.00 100.00%
2 3 6 16 0.375000 0.38 0.38 37.50%
3 4 3 5 0.600000 0.60 0.60 60.00%
4 5 6 8 0.750000 0.75 0.75 75.00%
5 7 11 16 0.687500 0.69 0.69 68.75%
6 11 5 8 0.625000 0.62 0.62 62.50%
7 12 1 3 0.333333 0.33 0.33 33.33%
8 15 16 27 0.592593 0.59 0.59 59.26%
9 16 2 5 0.400000 0.40 0.40 40.00%
10 17 9 13 0.692308 0.69 0.69 69.23%
注意转换后的数据类型
question_id int64
sum int64
count int64
mean float64
a float64
b object
c object
dtype: object