1四、表格顯示控制

 

 

In [ ]:
'''
表格顯示控制

df.style.format()
 只是更改了顯示樣式
 原數據不會更改
'''
In [1]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
In [2]:
df = pd.DataFrame(np.random.randn(10,4),columns=['a','b','c','d'])
sty = df.style
In [3]:
# 按照百分數顯示

df = pd.DataFrame(np.random.randn(10,4),columns=['a','b','c','d'])
print(df.head())
df.head().style.format("{:.2%}")
 
          a         b         c         d
0 -0.240393  0.238639 -0.207536  0.635190
1  1.077959 -1.526583 -0.216252  1.218049
2 -1.489623 -0.959523  0.868225  0.279078
3 -0.909010 -1.713432 -0.758621  0.080633
4 -0.466262 -0.014200  1.180753 -1.859700
Out[3]:
 
  a b c d
0 -24.04% 23.86% -20.75% 63.52%
1 107.80% -152.66% -21.63% 121.80%
2 -148.96% -95.95% 86.82% 27.91%
3 -90.90% -171.34% -75.86% 8.06%
4 -46.63% -1.42% 118.08% -185.97%
In [4]:
# 顯示小數點數

df.head().style.format("{:.4f}")
Out[4]:
 
  a b c d
0 -0.2404 0.2386 -0.2075 0.6352
1 1.0780 -1.5266 -0.2163 1.2180
2 -1.4896 -0.9595 0.8682 0.2791
3 -0.9090 -1.7134 -0.7586 0.0806
4 -0.4663 -0.0142 1.1808 -1.8597
In [5]:
# 顯示正負數

df.head().style.format("{:+.2f}")
Out[5]:
 
  a b c d
0 -0.24 +0.24 -0.21 +0.64
1 +1.08 -1.53 -0.22 +1.22
2 -1.49 -0.96 +0.87 +0.28
3 -0.91 -1.71 -0.76 +0.08
4 -0.47 -0.01 +1.18 -1.86
In [6]:
# 分列顯示

df.head().style.format({'b':"{:.2%}", 'c':"{:+.3f}", 'd':"{:.3f}"})
Out[6]:
 
  a b c d
0 -0.240393 23.86% -0.208 0.635
1 1.07796 -152.66% -0.216 1.218
2 -1.48962 -95.95% +0.868 0.279
3 -0.90901 -171.34% -0.759 0.081
4 -0.466262 -1.42% +1.181 -1.860
In [ ]:
相關文章
相關標籤/搜索