df1=pd.DataFrame(np.random.randn(5,5),columns=list('abcde'))
df1.applymap(lambda x:'.2f'% x)
想要格式化生成的亂數,結果卻得到這個錯誤。怎么回事啊
uj5u.com熱心網友回復:
格式化字串,缺少了%更改后代碼如下:
import numpy as np
df1=pd.DataFrame(np.random.randn(5,5),columns=list('abcde'))
print(df1)
df1=df1.applymap(lambda x:'%.2f'% x)
print(df1)
效果如下:
a b c d e
0 0.841073 0.167431 0.273329 0.209956 1.045138
1 0.203088 -0.535447 0.541680 0.272025 -0.269669
2 -0.890863 0.765354 -2.394559 -0.662201 0.180201
3 -1.059926 1.729437 0.085897 -0.627603 -0.391459
4 0.205721 1.574832 -1.688674 -0.454605 0.615806
a b c d e
0 0.84 0.17 0.27 0.21 1.05
1 0.20 -0.54 0.54 0.27 -0.27
2 -0.89 0.77 -2.39 -0.66 0.18
3 -1.06 1.73 0.09 -0.63 -0.39
4 0.21 1.57 -1.69 -0.45 0.62
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/201714.html
