如何在Python中从descripe()函数打印整数?
我正在使用Python的pandas做一些统计工作,我有以下代码来打印数据描述(平均值、计数、中值等) 但是我的数据非常大(大约400万行),每行都有非常小的数据。因此不可避免地,计数会很大,平均值会很小,因此Python会这样打印它 为了便于使用和理解,我只想完全打印这些数字,例如,最好是如何在Python中从descripe()函数打印整数?,python,pandas,Python,Pandas,我正在使用Python的pandas做一些统计工作,我有以下代码来打印数据描述(平均值、计数、中值等) 但是我的数据非常大(大约400万行),每行都有非常小的数据。因此不可避免地,计数会很大,平均值会很小,因此Python会这样打印它 为了便于使用和理解,我只想完全打印这些数字,例如,最好是43934476,而不是4.393476e+06。我已经在谷歌上搜索过了,我能找到的最多的是和其他一些类似的帖子。但是,只有在变量中已经有了这些数字的情况下,这才有效。但对我来说不是。就我而言,我没有这些数
43934476
,而不是4.393476e+06
。我已经在谷歌上搜索过了,我能找到的最多的是和其他一些类似的帖子。但是,只有在变量中已经有了这些数字的情况下,这才有效。但对我来说不是。就我而言,我没有这些数字。这些数字是由descripe()函数创建的,所以我不知道会得到什么数字
抱歉,如果这似乎是一个非常基本的问题,我对Python还是新手。任何回应都会被告知。谢谢。假设您有以下
DataFrame
:
编辑
我检查了文档,您可能应该使用pandas.set_选项
API来执行此操作:
In [13]: df
Out[13]:
a b c
0 4.405544e+08 1.425305e+08 6.387200e+08
1 8.792502e+08 7.135909e+08 4.652605e+07
2 5.074937e+08 3.008761e+08 1.781351e+08
3 1.188494e+07 7.926714e+08 9.485948e+08
4 6.071372e+08 3.236949e+08 4.464244e+08
5 1.744240e+08 4.062852e+08 4.456160e+08
6 7.622656e+07 9.790510e+08 7.587101e+08
7 8.762620e+08 1.298574e+08 4.487193e+08
8 6.262644e+08 4.648143e+08 5.947500e+08
9 5.951188e+08 9.744804e+08 8.572475e+08
In [14]: pd.set_option('float_format', '{:f}'.format)
In [15]: df
Out[15]:
a b c
0 440554429.333866 142530512.999182 638719977.824965
1 879250168.522411 713590875.479215 46526045.819487
2 507493741.709532 300876106.387427 178135140.583541
3 11884941.851962 792671390.499431 948594814.816647
4 607137206.305609 323694879.619369 446424361.522071
5 174424035.448168 406285189.907148 445616045.754137
6 76226556.685384 979050957.963583 758710090.127867
7 876261954.607558 129857447.076183 448719292.453509
8 626264394.999419 464814260.796770 594750038.747595
9 595118819.308896 974480400.272515 857247528.610996
In [16]: df.describe()
Out[16]:
a b c
count 10.000000 10.000000 10.000000
mean 479461624.877280 522785202.100082 536344333.626082
std 306428177.277935 320806568.078629 284507176.411675
min 11884941.851962 129857447.076183 46526045.819487
25% 240956633.919592 306580799.695412 445818124.696121
50% 551306280.509214 435549725.351959 521734665.600552
75% 621482597.825966 772901261.744377 728712562.052142
max 879250168.522411 979050957.963583 948594814.816647
编辑结束
您需要修改pandas.options.display.float\u format属性。注意,在我的代码中,我使用了导入熊猫作为pd
。快速修复方法类似于:
In [29]: pd.options.display.float_format = "{:.2f}".format
In [10]: df
Out[10]:
a b c
0 440554429.33 142530513.00 638719977.82
1 879250168.52 713590875.48 46526045.82
2 507493741.71 300876106.39 178135140.58
3 11884941.85 792671390.50 948594814.82
4 607137206.31 323694879.62 446424361.52
5 174424035.45 406285189.91 445616045.75
6 76226556.69 979050957.96 758710090.13
7 876261954.61 129857447.08 448719292.45
8 626264395.00 464814260.80 594750038.75
9 595118819.31 974480400.27 857247528.61
In [11]: df.describe()
Out[11]:
a b c
count 10.00 10.00 10.00
mean 479461624.88 522785202.10 536344333.63
std 306428177.28 320806568.08 284507176.41
min 11884941.85 129857447.08 46526045.82
25% 240956633.92 306580799.70 445818124.70
50% 551306280.51 435549725.35 521734665.60
75% 621482597.83 772901261.74 728712562.05
max 879250168.52 979050957.96 948594814.82
屈服
A B C
count 4393476 4393476 4393476
mean 0.050039 0.050056 0.050057
std 0.028834 0.028836 0.028849
min 0.000100 0.000100 0.000100
25% 0.025076 0.025081 0.025065
50% 0.050047 0.050050 0.050037
75% 0.074987 0.075027 0.075055
max 0.100000 0.100000 0.100000
在引擎盖下,数据帧按列组织。列中的值只能有一种数据类型(列的
dtype
)。
df.descripe()
返回的数据帧具有浮点数据类型的列:
In [116]: df.describe().info()
<class 'pandas.core.frame.DataFrame'>
Index: 8 entries, count to max
Data columns (total 3 columns):
A 8 non-null float64
B 8 non-null float64
C 8 non-null float64
dtypes: float64(3)
memory usage: 256.0+ bytes
将count
行转换为整数(通过调用astype(int)
),然后将整数转换为字符串(通过调用astype(str)
)。然后
使用将浮点数格式设置为小数点后的6位数字,将其余浮点数转换为字符串
或者,您可以使用
import numpy as np
import pandas as pd
np.random.seed(2016)
N = 4393476
df = pd.DataFrame(np.random.uniform(1e-4, 0.1, size=(N,3)), columns=list('ABC'))
desc = df.describe().T
desc['count'] = desc['count'].astype(int)
print(desc)
产生
count mean std min 25% 50% 75% max
A 4393476 0.050039 0.028834 0.0001 0.025076 0.050047 0.074987 0.1
B 4393476 0.050056 0.028836 0.0001 0.025081 0.050050 0.075027 0.1
C 4393476 0.050057 0.028849 0.0001 0.025065 0.050037 0.075055 0.1
通过转置desc
数据帧,count
s现在位于它们自己的列中。
因此,现在可以通过将该列的数据类型转换为int
来解决这个问题
这样做的一个优点是desc
中的值保持为数字。
因此,基于数值的进一步计算仍然可以进行
如果转置格式是可以接受的,我认为这个解决方案更可取。如果您不想更改整个设置,而只想应用于特定的输出:
with pd.option\u context('float\u format','{:f}'。format):print(df.descripe())
@ayhan我该如何更改已恢复默认设置?%\
In [116]: df.describe().info()
<class 'pandas.core.frame.DataFrame'>
Index: 8 entries, count to max
Data columns (total 3 columns):
A 8 non-null float64
B 8 non-null float64
C 8 non-null float64
dtypes: float64(3)
memory usage: 256.0+ bytes
desc.loc['count'] = desc.loc['count'].astype(int).astype(str)
desc.iloc[1:] = desc.iloc[1:].applymap('{:.6f}'.format)
import numpy as np
import pandas as pd
np.random.seed(2016)
N = 4393476
df = pd.DataFrame(np.random.uniform(1e-4, 0.1, size=(N,3)), columns=list('ABC'))
desc = df.describe().T
desc['count'] = desc['count'].astype(int)
print(desc)
count mean std min 25% 50% 75% max
A 4393476 0.050039 0.028834 0.0001 0.025076 0.050047 0.074987 0.1
B 4393476 0.050056 0.028836 0.0001 0.025081 0.050050 0.075027 0.1
C 4393476 0.050057 0.028849 0.0001 0.025065 0.050037 0.075055 0.1