【发布时间】:2019-12-16 05:33:51
【问题描述】:
我在 win10 上使用 jupyter notebook 时有以下数据框。
d = {'col1': [1000000, 200000000000000], 'col2': [3, 4]}
df2 = pd.DataFrame(data=d)
df2.describe()
导致以下输出:
col1 col2
count 2.000000e+00 2.000000
mean 1.000000e+14 3.500000
std 1.414214e+14 0.707107
min 1.000000e+06 3.000000
25% 5.000000e+13 3.250000
50% 1.000000e+14 3.500000
75% 1.500000e+14 3.750000
max 2.000000e+14 4.000000
我知道这只是一个“光学”问题,但不知何故我不喜欢它,当第一行没有相同的数字格式时,比较它并不容易。有没有办法避免这种显示,而是有输出,不知何故python对列使用相同的格式而不是对行?:
col1 col2
count 2.000000 2.000000
mean 1.000000e+14 3.500000
std 1.414214e+14 0.707107
min 1.000000e+06 3.000000
25% 5.000000e+13 3.250000
50% 1.000000e+14 3.500000
75% 1.500000e+14 3.750000
max 2.000000e+14 4.000000
或者是否有可能每行而不是列具有相同的格式?
我可以只更改所有的浮动显示吗?这样:Customized float formatting in a pandas DataFrame,还是pandas能聪明点?
【问题讨论】:
-
我想你可能正在寻找这个? stackoverflow.com/questions/55394854/…
-
df2.describe().astype(str)怎么样?如果它只是为了显示,那么谁在乎它是一个字符串 -
方法还不错,我能不能把前面的数字四舍五入到小数点后两位?
-
df2.describe().astype(object)也许?