formatting .dscribe() output in python
Question:
descirbe() output
the method I used is below
creditcardDF.describe().apply("{0:.5f}".format)
the error I got is:
unsupported format string passed to Series.__format__
can someone explain to me what is the problem?
I also have another question
could anyone explain to me
pd.set_option('display.float_format', lambda x: '%.5f' % x)
lambda x: ‘%.5f’ % x what’s this mean?
and
.apply("{0:.5f}".format)
"{0:.5f}".format what’s this mean?
Answers:
You can’t pass a Series
(list like) to str.format
method because Python doesn’t understand how it should process this list.
>>> "{0:.5f}".format([1.23456789, 9.87654321])
...
TypeError: unsupported format string passed to list.__format__
>>> "{0:.5f}".format(1.23456789)
'1.23457'
So you have to use applymap
instead of apply
to transform each individual values (which is not the best choice):
>>> df
0 1 2
0 10.05856 9.31342 10.92815
1 15.32752 1.88274 5.79967
2 15.73966 3.19805 3.81324
3 9.02629 2.80339 0.77310
4 14.95545 4.84119 8.68670
>>> df.applymap("{0:.2f}".format)
0 1 2
0 10.06 9.31 10.93
1 15.33 1.88 5.80
2 15.74 3.20 3.81
3 9.03 2.80 0.77
4 14.96 4.84 8.69
You can also override the default Pandas behavior using pd.set_option
or pd.context_option
(for temporary modification):
>>> print(df.describe())
0 1 2
count 5.00000 5.00000 5.00000
mean 13.02149 4.40776 6.00017
std 3.20886 2.94391 3.98897
min 9.02629 1.88274 0.77310
25% 10.05856 2.80339 3.81324
50% 14.95545 3.19805 5.79967
75% 15.32752 4.84119 8.68670
max 15.73966 9.31342 10.92815
>>> with pd.option_context('display.float_format', lambda x: '%.2f' % x):
print(df.describe())
0 1 2
count 5.00 5.00 5.00
mean 13.02 4.41 6.00
std 3.21 2.94 3.99
min 9.03 1.88 0.77
25% 10.06 2.80 3.81
50% 14.96 3.20 5.80
75% 15.33 4.84 8.69
max 15.74 9.31 10.93
descirbe() output
the method I used is below
creditcardDF.describe().apply("{0:.5f}".format)
the error I got is:
unsupported format string passed to Series.__format__
can someone explain to me what is the problem?
I also have another question
could anyone explain to me
pd.set_option('display.float_format', lambda x: '%.5f' % x)
lambda x: ‘%.5f’ % x what’s this mean?
and
.apply("{0:.5f}".format)
"{0:.5f}".format what’s this mean?
You can’t pass a Series
(list like) to str.format
method because Python doesn’t understand how it should process this list.
>>> "{0:.5f}".format([1.23456789, 9.87654321])
...
TypeError: unsupported format string passed to list.__format__
>>> "{0:.5f}".format(1.23456789)
'1.23457'
So you have to use applymap
instead of apply
to transform each individual values (which is not the best choice):
>>> df
0 1 2
0 10.05856 9.31342 10.92815
1 15.32752 1.88274 5.79967
2 15.73966 3.19805 3.81324
3 9.02629 2.80339 0.77310
4 14.95545 4.84119 8.68670
>>> df.applymap("{0:.2f}".format)
0 1 2
0 10.06 9.31 10.93
1 15.33 1.88 5.80
2 15.74 3.20 3.81
3 9.03 2.80 0.77
4 14.96 4.84 8.69
You can also override the default Pandas behavior using pd.set_option
or pd.context_option
(for temporary modification):
>>> print(df.describe())
0 1 2
count 5.00000 5.00000 5.00000
mean 13.02149 4.40776 6.00017
std 3.20886 2.94391 3.98897
min 9.02629 1.88274 0.77310
25% 10.05856 2.80339 3.81324
50% 14.95545 3.19805 5.79967
75% 15.32752 4.84119 8.68670
max 15.73966 9.31342 10.92815
>>> with pd.option_context('display.float_format', lambda x: '%.2f' % x):
print(df.describe())
0 1 2
count 5.00 5.00 5.00
mean 13.02 4.41 6.00
std 3.21 2.94 3.99
min 9.03 1.88 0.77
25% 10.06 2.80 3.81
50% 14.96 3.20 5.80
75% 15.33 4.84 8.69
max 15.74 9.31 10.93