User Tools

Site Tools


print_hundredths

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
round_vs._format [2023/02/13 23:07] rajuprint_hundredths [2023/02/14 20:45] – removed - external edit (Unknown date) 127.0.0.1
Line 1: Line 1:
-===== round vs. format ===== 
-==== write simple data ==== 
-<code> 
-$ 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 '?' for help. 
- 
-In [1]: 
-a = 10.30467 
- 
-In [2]: 
-'{:.2f}'.format(a) 
-Out[2]: 
-'10.30' 
- 
-In [3]: 
-type('{:.2f}'.format(a)) 
-Out[3]: 
-str 
- 
-In [4]: 
-round(a,2) 
-Out[4]: 
-10.3 
- 
-In [5]: 
-type(round(a,2)) 
-Out[5]: 
-float 
-</code> 
- 
-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. 
- 
-==== Write data into csv files ==== 
-Create some sample data 
-<code> 
-$ 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 '?' for help. 
- 
-In [1]: 
-import pandas as pd 
-df = pd.DataFrame({'symbol': ['A', 'B', 'C', 'D'], 'price': [8.222, 7.007, 3.971, 9.801], 'change': [6.601, 7.241, -9.341, 48.001]}) 
-df 
-Out[1]: 
-  symbol  price  change 
-0      A  8.222   6.601 
-1      B  7.007   7.241 
-2      C  3.971  -9.341 
-3      D  9.801  48.001 
-</code> 
- 
-If you round and dump the data to a file, it does not align around the decimal point 
-<code> 
-In [2]: 
-df.round({'price':2, 'change': 2}).to_csv('x/foo1.csv', index=False, lineterminator='\n') 
-</code> 
- 
-<code> 
-$ cat ~/x/foo1.csv 
-symbol,price,change 
-A,8.22,6.6 
-B,7.01,7.24 
-C,3.97,-9.34 
-D,9.8,48.0 
- 
-$ cat ~/x/foo1.csv | column -t -s, -R 2,3 
-symbol  price  change 
-A        8.22     6.6 
-B        7.01    7.24 
-C        3.97   -9.34 
-D         9.8    48.0 
-</code> 
- 
-But if we format the data, it can be aligned easily 
-<code> 
-In [3]: 
-formats = {'price': '{:.2f}', 'change': '{:.2f}'} 
-df2 = df.copy() 
-for col, f in formats.items(): 
-    df2[col] = df2[col].apply(lambda x: f.format(x)) 
-df2.to_csv('x/foo2.csv', index=False, lineterminator='\n') 
-</code> 
- 
-<code> 
-$ cat ~/x/foo2.csv 
-symbol,price,change 
-A,8.22,6.60 
-B,7.01,7.24 
-C,3.97,-9.34 
-D,9.80,48.00 
- 
-$ cat ~/x/foo2.csv | column -t -s, -R 2,3 
-symbol  price  change 
-A        8.22    6.60 
-B        7.01    7.24 
-C        3.97   -9.34 
-D        9.80   48.00 
-</code> 
- 
-Ref:- https://stackoverflow.com/questions/20003290/output-different-precision-by-column-with-pandas-dataframe-to-csv - shows how to format different columns with different precision. 
- 
-tags | print two digits after decimal, float_format by column 
  
print_hundredths.txt · Last modified: 2023/02/14 23:26 by raju