CoCalc Shared FilesLessons / statistics_project / formatting.ipynb
Author: Jeffrey Oishi
Views : 25
In [2]:
! head data/KNYC.csv

#date,actual_mean_temp,actual_min_temp,actual_max_temp,average_min_temp,average_max_temp,record_min_temp,record_max_temp,record_min_temp_year,record_max_temp_year,actual_precipitation,average_precipitation,record_precipitation 2014-7-1,81,72,89,68,83,52,100,1943,1901,0.00,0.12,2.17 2014-7-2,82,72,91,68,83,56,100,2001,1966,0.96,0.13,1.79 2014-7-3,78,69,87,68,83,54,103,1933,1966,1.78,0.12,2.80 2014-7-4,70,65,74,68,84,55,102,1986,1949,0.14,0.13,1.76 2014-7-5,72,63,81,68,84,53,101,1979,1999,0.00,0.12,3.07 2014-7-6,75,66,84,68,84,54,103,1979,2010,0.00,0.13,1.97 2014-7-7,81,72,90,68,84,56,100,1914,2010,0.04,0.13,3.13 2014-7-8,81,71,91,69,84,56,100,1894,1993,0.39,0.14,1.80 2014-7-9,80,71,88,69,84,54,106,1963,1936,0.09,0.14,1.09
In [3]:
import numpy as np

In [4]:
knyc = np.loadtxt("data/KNYC.csv",delimiter=',')

--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-4-7c75d4b79cd5> in <module>() ----> 1 knyc = np.loadtxt("data/KNYC.csv",delimiter=',') /projects/anaconda3/lib/python3.5/site-packages/numpy/lib/npyio.py in loadtxt(fname, dtype, comments, delimiter, converters, skiprows, usecols, unpack, ndmin) 928 929 # Convert each value according to its column and store --> 930 items = [conv(val) for (conv, val) in zip(converters, vals)] 931 # Then pack it according to the dtype's nesting 932 items = pack_items(items, packing) /projects/anaconda3/lib/python3.5/site-packages/numpy/lib/npyio.py in <listcomp>(.0) 928 929 # Convert each value according to its column and store --> 930 items = [conv(val) for (conv, val) in zip(converters, vals)] 931 # Then pack it according to the dtype's nesting 932 items = pack_items(items, packing) /projects/anaconda3/lib/python3.5/site-packages/numpy/lib/npyio.py in floatconv(x) 657 if b'0x' in x: 658 return float.fromhex(asstr(x)) --> 659 return float(x) 660 661 typ = dtype.type ValueError: could not convert string to float: b'2014-7-1' 
In [ ]:
np.loadtxt?

In [5]:
knyc = np.loadtxt("data/KNYC.csv",delimiter=',',usecols=(1,2,3,4,5,6,7,8,9,10,11,12))

In [6]:
print(knyc)

[[ 81. 72. 89. ..., 0. 0.12 2.17] [ 82. 72. 91. ..., 0.96 0.13 1.79] [ 78. 69. 87. ..., 1.78 0.12 2.8 ] ..., [ 68. 62. 73. ..., 0.29 0.13 1.69] [ 70. 63. 76. ..., 0. 0.12 2.57] [ 75. 68. 82. ..., 0. 0.13 3.07]]
In [7]:
print(knyc[0,6], knyc[0,8], knyc[0,-1])

100.0 1901.0 2.17
In [8]:
knyc.shape

(365, 12)
In [9]:
for i in range(knyc.shape[0]):
print(knyc[i,6], knyc[i,8], knyc[i,-1])

100.0 1901.0 2.17 100.0 1966.0 1.79 103.0 1966.0 2.8 102.0 1949.0 1.76 101.0 1999.0 3.07 103.0 2010.0 1.97 100.0 2010.0 3.13 100.0 1993.0 1.8 106.0 1936.0 1.09 102.0 1993.0 1.79 98.0 1988.0 1.94 99.0 1966.0 2.68 101.0 1966.0 3.16 100.0 1954.0 1.6 102.0 1995.0 2.33 99.0 1980.0 1.38 100.0 1953.0 3.13 101.0 1953.0 1.76 102.0 1977.0 1.82 101.0 1980.0 1.97 104.0 1977.0 2.26 104.0 2011.0 1.86 100.0 2011.0 2.41 97.0 1999.0 3.75 97.0 1999.0 1.64 98.0 1940.0 3.8 98.0 1963.0 2.65 97.0 1999.0 3.11 99.0 1949.0 3.47 98.0 1988.0 3.56 102.0 1933.0 2.29 100.0 1933.0 2.85 100.0 1955.0 2.49 97.0 2005.0 2.71 100.0 1944.0 3.25 101.0 1944.0 1.44 97.0 1955.0 3.31 104.0 1918.0 2.18 99.0 2001.0 2.6 103.0 2001.0 4.1 98.0 1949.0 4.64 102.0 1944.0 2.39 97.0 1944.0 3.62 99.0 2005.0 2.7 99.0 1988.0 5.81 97.0 1988.0 1.52 96.0 1944.0 4.8 95.0 1944.0 2.86 94.0 2002.0 3.95 94.0 2002.0 2.53 97.0 1955.0 3.63 96.0 1955.0 4.19 95.0 1916.0 1.85 92.0 1916.0 3.03 94.0 1972.0 3.61 95.0 1948.0 1.86 103.0 1948.0 3.24 101.0 1948.0 4.16 100.0 1948.0 3.99 99.0 1953.0 2.68 98.0 1973.0 2.3 100.0 1953.0 3.76 97.0 1953.0 3.84 102.0 1953.0 2.12 99.0 1929.0 3.32 97.0 1929.0 3.48 94.0 1985.0 2.45 97.0 1881.0 3.26 101.0 1881.0 2.07 93.0 1919.0 4.86 94.0 1915.0 0.86 97.0 1983.0 1.38 99.0 1983.0 2.9 94.0 1961.0 2.35 94.0 1952.0 3.94 93.0 1931.0 3.82 92.0 1927.0 4.16 93.0 1915.0 5.02 93.0 1991.0 3.37 91.0 1891.0 3.92 94.0 1983.0 4.3 93.0 1983.0 2.32 95.0 1895.0 5.54 95.0 1914.0 2.34 97.0 1895.0 8.28 89.0 1959.0 2.26 90.0 1970.0 2.36 91.0 1970.0 2.34 90.0 1933.0 3.13 88.0 1881.0 3.84 88.0 1945.0 2.18 89.0 1986.0 2.64 88.0 1927.0 4.98 90.0 1927.0 2.16 87.0 1919.0 1.55 88.0 1941.0 4.05 94.0 1941.0 1.99 90.0 1941.0 2.39 88.0 1944.0 4.09 87.0 2007.0 4.3 86.0 1916.0 7.33 91.0 1939.0 2.16 85.0 1949.0 3.06 86.0 1954.0 4.26 87.0 1954.0 2.75 84.0 1920.0 1.76 84.0 1956.0 1.7 87.0 1897.0 2.15 90.0 1938.0 2.28 82.0 1928.0 2.45 83.0 1963.0 4.35 80.0 1969.0 2.78 84.0 1920.0 2.17 88.0 1979.0 1.51 85.0 1947.0 2.97 79.0 2001.0 2.51 79.0 1963.0 3.3 78.0 1964.0 3.4 82.0 1963.0 1.88 83.0 1919.0 2.49 78.0 1971.0 3.67 82.0 1961.0 1.64 81.0 1946.0 2.41 84.0 1950.0 1.69 83.0 1950.0 1.7 79.0 2003.0 2.6 78.0 1975.0 1.44 78.0 1961.0 1.94 74.0 1948.0 1.47 78.0 1938.0 2.96 76.0 1975.0 7.4 75.0 1975.0 3.65 73.0 1985.0 1.7 74.0 1949.0 1.41 76.0 1879.0 2.39 73.0 1931.0 2.06 72.0 1993.0 2.23 80.0 1993.0 2.43 72.0 1928.0 2.39 71.0 1953.0 1.54 73.0 1928.0 1.24 72.0 1921.0 1.95 77.0 1985.0 3.37 74.0 1900.0 1.33 72.0 1931.0 2.03 72.0 1931.0 1.84 73.0 1979.0 1.95 73.0 1979.0 1.36 67.0 1946.0 1.91 72.0 1896.0 2.15 70.0 2011.0 2.14 69.0 1990.0 2.01 70.0 1991.0 1.11 70.0 2006.0 1.72 66.0 1970.0 2.16 69.0 1998.0 1.63 74.0 1998.0 1.84 70.0 2001.0 1.28 71.0 2001.0 1.6 75.0 1998.0 1.98 65.0 1927.0 1.54 66.0 1966.0 2.54 70.0 1946.0 1.62 64.0 1879.0 2.41 68.0 1931.0 1.6 64.0 1923.0 3.03 67.0 1881.0 2.22 67.0 2008.0 1.34 63.0 1971.0 2.25 62.0 2000.0 2.28 63.0 1984.0 1.3 58.0 1931.0 1.58 60.0 2002.0 1.82 65.0 2013.0 2.49 71.0 2013.0 2.18 66.0 1990.0 1.61 63.0 1996.0 1.42 64.0 1982.0 1.3 63.0 1982.0 1.66 63.0 1949.0 2.14 65.0 1982.0 1.35 70.0 1984.0 2.52 65.0 1984.0 1.69 63.0 1965.0 2.31 62.0 1966.0 2.05 68.0 1876.0 1.92 64.0 2000.0 2.42 66.0 1950.0 2.73 64.0 1993.0 1.5 72.0 2007.0 1.65 64.0 1907.0 2.67 65.0 1998.0 1.25 64.0 1937.0 1.42 60.0 1876.0 1.72 63.0 1975.0 1.46 64.0 1890.0 2.35 68.0 1932.0 1.44 70.0 1932.0 2.06 67.0 1932.0 1.27 58.0 1995.0 1.44 63.0 1990.0 1.36 66.0 1990.0 2.1 64.0 1951.0 2.39 61.0 2006.0 1.41 63.0 2006.0 3.45 61.0 1959.0 1.7 62.0 1906.0 2.55 68.0 1967.0 2.18 60.0 1967.0 1.8 72.0 1950.0 2.19 69.0 1916.0 1.94 66.0 1916.0 1.87 69.0 2002.0 1.03 64.0 2006.0 1.19 63.0 1947.0 1.51 67.0 1989.0 2.12 59.0 1988.0 2.98 64.0 1991.0 1.55 68.0 1991.0 2.1 70.0 1991.0 1.43 68.0 2008.0 2.74 54.0 1938.0 2.96 61.0 1965.0 1.15 63.0 1990.0 1.74 61.0 2001.0 2.63 65.0 1960.0 2.74 62.0 1999.0 1.66 64.0 1951.0 2.42 63.0 1946.0 1.59 73.0 1949.0 1.73 71.0 1954.0 1.4 67.0 1976.0 1.49 68.0 1981.0 1.5 66.0 1997.0 2.15 69.0 1939.0 3.07 68.0 1930.0 1.86 69.0 1997.0 2.39 70.0 1985.0 1.38 75.0 1985.0 1.69 75.0 1930.0 2.11 65.0 1890.0 1.87 72.0 1997.0 1.56 67.0 1976.0 1.21 73.0 1972.0 2.95 72.0 1972.0 2.41 65.0 1991.0 2.25 70.0 1974.0 1.65 72.0 1880.0 1.81 68.0 1935.0 2.63 74.0 1946.0 1.87 76.0 1987.0 1.78 69.0 2000.0 1.82 74.0 2006.0 1.62 73.0 1977.0 2.94 71.0 1890.0 2.33 85.0 1990.0 3.86 75.0 1946.0 1.02 77.0 1990.0 1.81 82.0 1990.0 2.03 75.0 1945.0 1.42 77.0 1989.0 3.1 76.0 1918.0 2.19 83.0 1945.0 1.93 84.0 1921.0 2.37 78.0 2012.0 3.44 76.0 1923.0 1.6 76.0 1988.0 2.05 79.0 1963.0 4.25 76.0 1922.0 1.42 83.0 1998.0 1.79 84.0 1945.0 2.98 86.0 1945.0 2.03 82.0 1998.0 2.45 86.0 1998.0 2.2 83.0 1917.0 1.89 81.0 1967.0 1.93 81.0 1981.0 1.9 80.0 1892.0 1.99 80.0 1928.0 2.76 79.0 1947.0 2.52 92.0 2010.0 1.35 90.0 1991.0 1.93 86.0 1991.0 3.42 86.0 1922.0 4.31 84.0 1955.0 1.1 90.0 1977.0 2.12 88.0 1977.0 1.26 85.0 1941.0 2.72 87.0 1941.0 7.57 92.0 2002.0 3.29 96.0 2002.0 1.29 96.0 1976.0 2.19 92.0 1976.0 1.96 90.0 1927.0 1.96 87.0 1923.0 2.28 86.0 2001.0 2.45 86.0 2007.0 2.34 87.0 2001.0 2.17 91.0 1915.0 1.68 92.0 2009.0 1.88 92.0 1915.0 2.04 90.0 1990.0 2.74 89.0 1974.0 0.91 91.0 1942.0 4.97 87.0 2001.0 2.48 90.0 2001.0 1.1 90.0 2001.0 1.66 92.0 2001.0 2.02 90.0 1980.0 1.55 92.0 1986.0 1.46 93.0 2000.0 3.82 91.0 2000.0 3.02 94.0 1979.0 1.42 94.0 1979.0 2.1 92.0 1993.0 1.67 93.0 1881.0 1.84 89.0 1956.0 1.66 88.0 1900.0 3.38 90.0 1900.0 1.16 90.0 1951.0 2.66 92.0 1974.0 1.05 90.0 1936.0 2.18 99.0 1962.0 2.02 96.0 1996.0 2.03 93.0 1996.0 1.94 96.0 1941.0 1.25 94.0 1964.0 2.7 93.0 1975.0 2.07 95.0 1880.0 0.86 95.0 1880.0 1.28 96.0 1880.0 2.62 94.0 1959.0 1.16 97.0 1969.0 3.99 97.0 1987.0 2.19 96.0 1939.0 3.13 96.0 1895.0 2.6 96.0 1895.0 2.79 95.0 1895.0 3.01 99.0 1925.0 2.75 99.0 1925.0 2.8 98.0 1925.0 2.62 96.0 1925.0 4.16 95.0 1933.0 1.47 97.0 1933.0 2.55 96.0 2008.0 2.07 95.0 1973.0 1.14 93.0 1973.0 2.18 96.0 1961.0 1.71 99.0 1956.0 2.54 96.0 1994.0 1.13 97.0 1891.0 1.31 96.0 1957.0 1.82 95.0 1929.0 2.3 98.0 1994.0 2.16 98.0 1923.0 1.39 97.0 1988.0 1.7 98.0 1988.0 1.96 96.0 1888.0 1.75 96.0 1888.0 1.46 99.0 1952.0 1.28 100.0 1952.0 4.29 101.0 1966.0 2.11 96.0 1991.0 1.69 101.0 1934.0 2.57 99.0 1964.0 3.07
In [10]:
number = 367.3
print("Here is a number",number)

Here is a number 367.3
In [11]:
print("Here is a number: {}".format(number))

Here is a number: 367.3
In [12]:
print("Here is a num{}ber".format(number))

Here is a num367.3ber
In [15]:
print("Today is the {}th day of May. It is the {}th hour of the day.".format(16,13))

Today is the 16th day of May. It is the 13th hour of the day.
In [25]:
print("Here is a number {:010.1f} also it's {}".format(number,number))

Here is a number 00000367.3 also it's 367.3
In [42]:
nd = 10
for i in range(20):
print("{:5.1f} {:5d} {:4.{ndigits}f}".format(knyc[i,6], int(knyc[i,8]), knyc[i,-1],ndigits=nd))

100.0 1901 2.1700000000 100.0 1966 1.7900000000 103.0 1966 2.8000000000 102.0 1949 1.7600000000 101.0 1999 3.0700000000 103.0 2010 1.9700000000 100.0 2010 3.1300000000 100.0 1993 1.8000000000 106.0 1936 1.0900000000 102.0 1993 1.7900000000 98.0 1988 1.9400000000 99.0 1966 2.6800000000 101.0 1966 3.1600000000 100.0 1954 1.6000000000 102.0 1995 2.3300000000 99.0 1980 1.3800000000 100.0 1953 3.1300000000 101.0 1953 1.7600000000 102.0 1977 1.8200000000 101.0 1980 1.9700000000
In [43]:
a = 2.3
b = 7.3

In [44]:
a/b

0.3150684931506849
In [ ]: