Python Learning day by day

This post will keep getting updated either in the same blog post or will get published in different versions.

# Notes on Python’s pandas and everything else

Pandas have dataframes. With the dataframes, we can have several operations.

1. Read into csv file
2. Lookup the first five rows of the dataset by
first_rows = dataset.head()
We can add in an integer to display any number <= dataset size into head()

3. We can see how the dataset is setup/seeing the dimensions of dataset by using:
dimensions = dataset.shape
num_rows = dimensions[0] # to see rows
num_cols = dimensions[1] # to see columns
This returns the tuple

4. Pandas have series object that returns the row/column labels with the values corresponding to them instead of
returning a list of row/column values.

5. loc[] is to select any row from the dataset

6. Datatypes in pandas are a little different from the basic programming language datatypes we have been studying
object is equivalent to strings datatypes, then we have int, float, datetime, bool

7. Pandas can do a lot with extracting columns from the dataset like selecting multiple columns from the dataframe
and it returns the columns in the order they are passed even though they are not situated together in the dataset:
colList = dataset[[“col1”, “col2”]]
———————————————–
1. Generators and iterables have two differences among them: (for more details: https://pythontips.com/2013/09/29/the-python-yield-keyword-explained/)
Generators use ()
Iterables use []

Generators can be traversed once
Iterables can be traversed as many times you want

Strings, lists, tuples are all iterables and so are generators.

2. Yield works similar to return except the function returns a generator

3. Yield and generator combination works efficiently in tasks involving sequential concept like generating a fibonacci series:
(http://pythoncentral.io/python-generators-and-yield-keyword/)
Example:
def fibonacci(n):
curr = 1
prev = 0
counter = 0
while counter < n:
yield curr
prev, curr = curr, prev + curr
counter += 1
yield and xrange can be treated similarly since both of them generates the value as the loop progresses rather than creating the list beforehand.

———————————————–
Python data structure complexities: https://wiki.python.org/moin/TimeComplexity

1. Linked lists: To delete any element from a linked list and replace it with a new one, we need to make sure we add reference of the next element in this new element otherwise we may lose the reference for it.

References: https://developers.google.com/edu/python/lists#list-methods


 

The magic of getattr():

Came across this brilliant method which can take in the attributes for any object you create. The example is as follows:

a = ‘sort’ #assigning the sort method to a variable

myList = [2,4,5,1,99] #creating a list of 5 integers to sort in ascending order

myFunc = getattr (list, a) # Now myFunc gets the list we created and applies the sort function that we stored in the variable ‘a’.

myList #we now check the output

Output: [1, 2, 4, 5, 99]


# Slice it up with Python Slices in lists

#palindrome:
def palindromeCheck(number):
return number == number[::-1]

palindromeCheck(‘141’)
#output: True
palindromeCheck(‘154’)
# output: False

#Isn’t this brilliant how checking up a palindrome in Python is about 2 lines of code.

#power of slicing in the following image

slice

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s