print_hundredths
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
round_vs._format [2023/02/08 16:26] – raju | round_vs._format [2023/02/14 20:45] – raju | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ==== round vs. format | + | ===== print hundredths ===== |
- | < | + | Let's define hundredths as numbers with two decimal digits. This can be money amounts in dollars and cents. |
- | $ ipython | + | |
- | Python 3.10.9 | packaged by conda-forge | (main, Jan 11 2023, 15:15:40) [MSC v.1916 64 bit (AMD64)] | + | |
- | IPython 8.8.0 -- An enhanced Interactive Python. Type '?' | + | |
+ | tags | pennies, dollar-cent amounts, print two digits after decimal | ||
+ | |||
+ | ==== write single numbers ==== | ||
+ | There are two possible ways - round, format expression. I prefer the format expression as it always gives the same number of digits after the decimal. | ||
+ | |||
+ | ^ ^ round ^ format ^ | ||
+ | | output type | float | string | | ||
+ | | ::: | < | ||
In [1]: | In [1]: | ||
a = 10.30467 | a = 10.30467 | ||
In [2]: | In [2]: | ||
- | ' | + | type(round(a,2)) |
Out[2]: | Out[2]: | ||
- | ' | + | float |
In [3]: | In [3]: | ||
Line 17: | Line 22: | ||
Out[3]: | Out[3]: | ||
str | str | ||
+ | </ | ||
+ | | number of digits after the decimal point | varies | always two | | ||
+ | | ::: | < | ||
In [4]: | In [4]: | ||
- | round(a,2) | + | a = 10.30467 |
- | Out[4]: | + | |
- | 10.3 | + | |
In [5]: | In [5]: | ||
- | type(round(a,2)) | + | round(a,2) |
Out[5]: | Out[5]: | ||
- | float | + | 10.3 |
+ | |||
+ | In [6]: | ||
+ | ' | ||
+ | Out[6]: | ||
+ | ' | ||
+ | </ | ||
+ | |||
+ | Tested with | Python 3.10.9, ipython 8.8.0 | ||
+ | |||
+ | tags | round vs. format | ||
+ | |||
+ | ==== write dataframe to csv files ==== | ||
+ | |||
+ | If you round and dump the data into a csv file, it does not align around the decimal point. The result is also difficult to align using command line tools. | ||
+ | |||
+ | On the other hand, if the data is formatted using the format expression, it will still not align but can be aligned using command line tools. | ||
+ | |||
+ | For example, consider | ||
+ | < | ||
+ | $ ipython | ||
+ | Python 3.10.9 | packaged by conda-forge | (main, Jan 11 2023, 15:15:40) [MSC v.1916 64 bit (AMD64)] | ||
+ | Type ' | ||
+ | IPython 8.8.0 -- An enhanced Interactive Python. Type '?' | ||
+ | |||
+ | In [1]: | ||
+ | import pandas as pd | ||
+ | df = pd.DataFrame({ | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | df | ||
+ | Out[1]: | ||
+ | symbol | ||
+ | 0 A 8.222 | ||
+ | 1 B 7.007 | ||
+ | 2 C 3.971 -9.341 | ||
+ | 3 D 9.801 48.001 | ||
+ | </ | ||
+ | |||
+ | If it is rounded and dumped into a csv file | ||
+ | < | ||
+ | In [2]: | ||
+ | df.round({' | ||
+ | </ | ||
+ | The result does not align | ||
+ | < | ||
+ | $ cat ~/ | ||
+ | symbol, | ||
+ | A, | ||
+ | B, | ||
+ | C, | ||
+ | D, | ||
+ | </ | ||
+ | and can't easily be aligned using other command line tools | ||
+ | < | ||
+ | $ cat ~/ | ||
+ | symbol | ||
+ | A 8.22 6.6 | ||
+ | B 7.01 7.24 | ||
+ | C 3.97 | ||
+ | D | ||
+ | </ | ||
+ | |||
+ | However, if format expression is used | ||
+ | < | ||
+ | In [3]: | ||
+ | formats = {' | ||
+ | df2 = df.copy() | ||
+ | for col, f in formats.items(): | ||
+ | df2[col] = df2[col].apply(lambda x: f.format(x)) | ||
+ | df2.to_csv(' | ||
+ | </ | ||
+ | the result still does not align | ||
+ | < | ||
+ | $ cat ~/ | ||
+ | symbol, | ||
+ | A, | ||
+ | B, | ||
+ | C, | ||
+ | D, | ||
+ | </ | ||
+ | but can be using command line tools | ||
+ | < | ||
+ | $ cat ~/ | ||
+ | symbol | ||
+ | A 8.22 6.60 | ||
+ | B 7.01 7.24 | ||
+ | C 3.97 | ||
+ | D 9.80 | ||
+ | </ | ||
+ | |||
+ | Ref:- https:// | ||
+ | |||
+ | tags | round vs. format, float_format by column | ||
+ | |||
+ | ==== align hundredths column with spaces ==== | ||
+ | Use | ||
+ | < | ||
+ | import pandas as pd | ||
+ | from tabulate import tabulate | ||
+ | |||
+ | def to_fwf(df, fname): | ||
+ | content = tabulate(df.values.tolist(), | ||
+ | with open(fname, " | ||
+ | FileObj.write(content) | ||
+ | |||
+ | pd.DataFrame.to_fwf = to_fwf | ||
+ | </ | ||
+ | |||
+ | For example, consider | ||
+ | < | ||
+ | $ ipython | ||
+ | Python 3.10.9 | packaged by conda-forge | (main, Jan 11 2023, 15:15:40) [MSC v.1916 64 bit (AMD64)] | ||
+ | Type ' | ||
+ | IPython 8.8.0 -- An enhanced Interactive Python. Type '?' | ||
+ | |||
+ | In [1]: | ||
+ | import pandas as pd | ||
+ | df = pd.DataFrame({ | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | df | ||
+ | Out[1]: | ||
+ | symbol | ||
+ | 0 A 8.222 | ||
+ | 1 B 7.007 | ||
+ | 2 C 3.971 -9.341 | ||
+ | 3 D 9.801 48.001 | ||
+ | |||
+ | In [2]: | ||
+ | import pandas as pd | ||
+ | from tabulate import tabulate | ||
+ | |||
+ | def to_fwf(df, fname): | ||
+ | content = tabulate(df.values.tolist(), | ||
+ | with open(fname, " | ||
+ | FileObj.write(content) | ||
+ | |||
+ | pd.DataFrame.to_fwf = to_fwf | ||
+ | </ | ||
+ | |||
+ | round the data and dump it | ||
+ | < | ||
+ | In [3]: | ||
+ | df.round({' | ||
+ | </ | ||
+ | |||
+ | the result is aligned and space separated | ||
+ | < | ||
+ | $ cat ~/ | ||
+ | symbol | ||
+ | A 8.22 6.6 | ||
+ | B 7.01 7.24 | ||
+ | C 3.97 | ||
+ | D 9.8 48 | ||
+ | </ | ||
+ | |||
+ | You can also do it using format expression | ||
+ | < | ||
+ | In [4]: | ||
+ | formats = {' | ||
+ | df2 = df.copy() | ||
+ | for col, f in formats.items(): | ||
+ | df2[col] = df2[col].apply(lambda x: f.format(x)) | ||
+ | df2.to_fwf(' | ||
+ | </ | ||
+ | which gives the same result | ||
+ | < | ||
+ | $ cat ~/ | ||
+ | symbol | ||
+ | A 8.22 6.6 | ||
+ | B 7.01 7.24 | ||
+ | C 3.97 | ||
+ | D 9.8 48 | ||
</ | </ | ||
- | Conclusions: | ||
- | * The output of round is a floating point number. The output of format is a string | ||
- | * To output dollars and pennies, format expression is better than rounding as it always gives the same number of digits after the decimal point. | ||
+ | Ref :- https:// |
print_hundredths.txt · Last modified: 2023/02/14 23:26 by raju