Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # this usually comes from an internet resource
- product = {
- "name": "MoccaMaster",
- "price": 139,
- "rating": 5,
- "country": "Netherlands",
- "manufacture_date": 2020
- }
- # we usually don't print the whole dictionary
- # because it looks a bit strange to users
- # print(product)
- # printing individual info
- print(product['name'])
- print(f"{product['price']} €")
- actors = ["Elijah Wood", "Orlando Bloom", "Viggo Mortensen"]
- # another example, a dictionary with a list
- movie = {
- "name": "Lord of the rings: Two Towers",
- "year": 2002,
- "genre": "Fantasy",
- "length_minutes": 223,
- "actors": actors
- }
- print(movie['name'])
- # because movie is a dictionary, and actors is a list
- # we need two sets of brackets
- # example, 3rd actor in the list
- print(movie['actors'][2])
- # NEW FILE
- # let's make a list of three dictionaries
- p1 = {"name": "Coffee machine", "price": 119}
- p2 = {"name": "Vacuum Cleaner", "price": 79}
- p3 = {"name": "Tumbledryer", "price": 399}
- all_products = [p1, p2, p3]
- # get the price of the second product
- # index 1 = 2nd
- price_of_second = all_products[1]["price"]
- print(price_of_second)
- print()
- # a basic loop that prints out the names and prices
- # one by one, starting from first product
- for p in all_products:
- print(p['name'])
- print(p['price'] * 1.24)
- print()
- # NEW FILE, FUNCTIONS AND LAMBDAS
- # function definitions are usually put in the top of the file
- # or even better, in their own file
- def triple_value(number):
- result = number * 3
- return result
- def add_tax(price):
- result = price * 1.24
- result = round(result, 1)
- return result
- # calling your own functions is like calling any other function in Python
- tripled = triple_value(250)
- print(tripled)
- original_price = 356;
- price_with_tax = add_tax(original_price)
- print(price_with_tax)
- # lambda is an anonymous function
- # a shorthand for typical function
- # useful for simple calculations
- doubler = lambda x: x * 2
- result = doubler(1500)
- print(result)
- even_test = lambda x : x % 2 == 0
- print(even_test(444))
- # NEW FILE
- import numpy as np
- import pandas as pd
- # separate list for labels and values
- labels = ["First", "Second", "Third", "Fourth"]
- numbers = [10, 20, 40, 70]
- numbers_np = np.array(numbers)
- # make a series
- ser1 = pd.Series(numbers_np, labels)
- # you can also use a dictionary
- measurements = {
- "Mon": 4,
- "Tue": 10,
- "Wed": -5,
- "Thu": -3,
- "Fri": 8,
- "Sat": 15,
- "Sun": -15
- }
- # make a series
- ser2 = pd.Series(measurements)
- # some country codes
- # if you have partially similar series, and you add them together
- # unique values be NaN = not a number
- country_names = ['Finland', 'Sweden', 'Norway']
- country_numbers = [1, 2, 3]
- #ser3 = pd.Series([1, 2, 3], ['Finland', 'Sweden', 'Norway'])
- ser3 = pd.Series(country_numbers, country_names)
- country_names = ['Finland', 'Sweden', 'Denmark']
- country_numbers = [1, 2, 4]
- ser4 = pd.Series(country_numbers, country_names)
- #print(ser3['Sweden'])
- combined = ser3 + ser4
- # NEW FILE
- import numpy as np
- import pandas as pd
- from numpy.random import randn
- np.random.seed(123)
- labels_1 = ['A', 'B', 'C', 'D']
- labels_2 = ['X', 'Y', 'Z']
- df = pd.DataFrame(randn(4, 3), labels_1, labels_2)
- # this is the recommended way
- print(df['Y'])
- # this can create strange bugs if your
- # column name overlaps with a function name
- # print(df.Y)
- # if you want more than column at the same time
- # provide extra set of brackets
- print(df[['Y', 'Z']])
- # one of the most useful features in pandas
- # you can create additional columns to help
- # make new calculations!
- # here we combine three columns and round to three decimals
- df['total'] = round(df['X'] + df['Y'] + df['Z'], 3)
- # the basic way to drop a column
- #df = df.drop('Y', axis=1)
- #df = df.drop('C')
- # df.drop('Y', axis=1, inplace=True)
- row = df.loc['B']
- last_row = df.iloc[3]
- sub_values = df[df>0]
- # this is very useful with real datasets
- filtered = df[df['Y'] > 0]
- print()
- print(filtered['total'])
- # one of the most important features in pandas filtering
- complex_filter = df[(df['Y'] > 0) | (df['Z'] > 0)]
- # this is useful sometimes when you want to work
- # with numeric indeces instead of text indeces
- df = df.reset_index()
- # you can do this as well, not very often used
- # with real datasets though
- newindeces = 'FI SE EN NO'.split()
- df['Languages'] = newindeces
- df= df.set_index('Languages')
- # NEW FILE
Add Comment
Please, Sign In to add comment