tuomasvaltanen

Untitled

Mar 2nd, 2021 (edited)
676
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.49 KB | None | 0 0
  1. # this usually comes from an internet resource
  2. product = {
  3.     "name": "MoccaMaster",
  4.     "price": 139,
  5.     "rating": 5,
  6.     "country": "Netherlands",
  7.     "manufacture_date": 2020
  8.     }
  9.  
  10. # we usually don't print the whole dictionary
  11. # because it looks a bit strange to users
  12. # print(product)
  13.  
  14. # printing individual info
  15. print(product['name'])
  16. print(f"{product['price']} €")
  17.  
  18. actors = ["Elijah Wood", "Orlando Bloom", "Viggo Mortensen"]
  19.  
  20. # another example, a dictionary with a list
  21. movie = {
  22.     "name": "Lord of the rings: Two Towers",
  23.     "year": 2002,
  24.     "genre": "Fantasy",
  25.     "length_minutes": 223,
  26.     "actors": actors
  27.     }
  28.  
  29. print(movie['name'])
  30.  
  31. # because movie is a dictionary, and actors is a list
  32. # we need two sets of brackets
  33. # example, 3rd actor in the list
  34. print(movie['actors'][2])
  35.  
  36. # NEW FILE
  37.  
  38. # let's make a list of three dictionaries
  39. p1 = {"name": "Coffee machine", "price": 119}
  40. p2 = {"name": "Vacuum Cleaner", "price": 79}
  41. p3 = {"name": "Tumbledryer", "price": 399}
  42.  
  43. all_products = [p1, p2, p3]
  44.  
  45. # get the price of the second product
  46. # index 1 = 2nd
  47. price_of_second = all_products[1]["price"]
  48. print(price_of_second)
  49. print()
  50.  
  51. # a basic loop that prints out the names and prices
  52. # one by one, starting from first product
  53. for p in all_products:
  54.     print(p['name'])
  55.     print(p['price'] * 1.24)
  56.     print()
  57.  
  58. # NEW FILE, FUNCTIONS AND LAMBDAS
  59.  
  60. # function definitions are usually put in the top of the file
  61. # or even better, in their own file
  62. def triple_value(number):
  63.     result = number * 3
  64.     return result
  65.  
  66.  
  67. def add_tax(price):
  68.     result = price * 1.24
  69.     result = round(result, 1)
  70.     return result
  71.  
  72.  
  73. # calling your own functions is like calling any other function in Python
  74. tripled = triple_value(250)
  75. print(tripled)
  76.  
  77. original_price = 356;
  78. price_with_tax = add_tax(original_price)
  79. print(price_with_tax)
  80.  
  81. # lambda is an anonymous function
  82. # a shorthand for typical function
  83. # useful for simple calculations
  84. doubler = lambda x: x * 2
  85. result = doubler(1500)
  86.  
  87. print(result)
  88.  
  89. even_test = lambda x : x % 2 == 0
  90. print(even_test(444))
  91.  
  92. # NEW FILE
  93.  
  94. import numpy as np
  95. import pandas as pd
  96.  
  97. # separate list for labels and values
  98. labels = ["First", "Second", "Third", "Fourth"]
  99. numbers = [10, 20, 40, 70]
  100. numbers_np = np.array(numbers)
  101.  
  102. # make a series
  103. ser1 = pd.Series(numbers_np, labels)
  104.  
  105. # you can also use a dictionary
  106. measurements = {
  107.     "Mon": 4,
  108.     "Tue": 10,
  109.     "Wed": -5,
  110.     "Thu": -3,
  111.     "Fri": 8,
  112.     "Sat": 15,
  113.     "Sun": -15
  114.     }
  115.  
  116. # make a series
  117. ser2 = pd.Series(measurements)
  118.  
  119. # some country codes
  120.  
  121. # if you have partially similar series, and you add them together
  122. # unique values be NaN = not a number
  123. country_names = ['Finland', 'Sweden', 'Norway']
  124. country_numbers = [1, 2, 3]
  125. #ser3 = pd.Series([1, 2, 3], ['Finland', 'Sweden', 'Norway'])
  126. ser3 = pd.Series(country_numbers, country_names)
  127.  
  128. country_names = ['Finland', 'Sweden', 'Denmark']
  129. country_numbers = [1, 2, 4]
  130. ser4 = pd.Series(country_numbers, country_names)
  131.  
  132. #print(ser3['Sweden'])
  133.  
  134. combined = ser3 + ser4
  135.  
  136. # NEW FILE
  137.  
  138. import numpy as np
  139. import pandas as pd
  140. from numpy.random import randn
  141.  
  142. np.random.seed(123)
  143.  
  144. labels_1 = ['A', 'B', 'C', 'D']
  145. labels_2 = ['X', 'Y', 'Z']
  146.  
  147. df = pd.DataFrame(randn(4, 3), labels_1, labels_2)
  148.  
  149. # this is the recommended way
  150. print(df['Y'])
  151.  
  152. # this can create strange bugs if your
  153. # column name overlaps with a function name
  154. # print(df.Y)
  155.  
  156. # if you want more than column at the same time
  157. # provide extra set of brackets
  158. print(df[['Y', 'Z']])
  159.  
  160. # one of the most useful features in pandas
  161. # you can create additional columns to help
  162. # make new calculations!
  163. # here we combine three columns and round to three decimals
  164. df['total'] = round(df['X'] + df['Y'] + df['Z'], 3)
  165.  
  166. # the basic way to drop a column
  167. #df = df.drop('Y', axis=1)
  168. #df = df.drop('C')
  169.  
  170. # df.drop('Y', axis=1, inplace=True)
  171. row = df.loc['B']
  172. last_row = df.iloc[3]
  173.  
  174. sub_values = df[df>0]
  175.  
  176. # this is very useful with real datasets
  177. filtered = df[df['Y'] > 0]
  178.  
  179. print()
  180. print(filtered['total'])
  181.  
  182. # one of the most important features in pandas filtering
  183. complex_filter = df[(df['Y'] > 0) | (df['Z'] > 0)]
  184.  
  185. # this is useful sometimes when you want to work
  186. # with numeric indeces instead of text indeces
  187. df = df.reset_index()
  188.  
  189. # you can do this as well, not very often used
  190. # with real datasets though
  191. newindeces = 'FI SE EN NO'.split()
  192. df['Languages'] = newindeces
  193. df= df.set_index('Languages')
  194.  
  195. # NEW FILE
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
Add Comment
Please, Sign In to add comment