gpt4 book ai didi

python - 如何更改 .describe() 输出的格式?

转载 作者:太空宇宙 更新时间:2023-11-04 00:02:04 31 4
gpt4 key购买 nike

我将 .describe() 放入 Dataframe,输出看起来不太好。我希望输出显示整数而不是用指数简化。

输入:

df["A"].describe()

输出的样子:

count    6.000000e+01
mean 7.123568e+04
std 2.144483e+05
min 1.000000e+02
25% 2.770080e+03
50% 1.557920e+04
75% 4.348470e+04
max 1.592640e+06
Name: A, dtype: float64

预期输出:

count    60.0
mean 7123.568
std 214448.3
min 100.0000
25% 2770.080
50% 15579.20
75% 43484.70
max 1592640.0
Name: A, dtype: float64

最佳答案

您可以在 pandas set_option 中更改 pandas 的 float_format

import pandas as pd
import numpy as np

pd.set_option('display.float_format', lambda x: '%.5f' % x)

data = pd.DataFrame()

data['X'] = (np.random.rand(1000, ) + 10000000) * 0.587

data['X'].describe()

# Output
count 1000.00000
mean 5870000.47894
std 0.28447
min 5870000.00037
25% 5870000.23637
50% 5870000.45799
75% 5870000.71652
max 5870000.99774
Name: X, dtype: float64

或者不使用 set_option 像这样在输出序列上使用 apply

import pandas as pd
import numpy as np

data = pd.DataFrame()

data['X'] = np.random.rand(1000, ) + 10000000 * 0.587

data['X'].describe().apply("{0:.5f}".format)

#output

count 1000.00000
mean 5870000.48955
std 0.29247
min 5870000.00350
25% 5870000.22416
50% 5870000.50163
75% 5870000.73457
max 5870000.99995

关于python - 如何更改 .describe() 输出的格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55394854/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com