Monday 30 July 2018

pandas-programming practice 1

1.How to create a Boolean column that compares the value of the next n rows with the actual value of a row

Input
A  B  C
14 3 32
28 3 78
15 4 68
42 3 42
24 4 87
13 3 65

Calculation for D: if any of the next n rows (in this case 3) have a value that is >= than the actual row (n)+30 then return 1, else 0

OUTPUT
A  B  C  D
14 3 32  1     # 32+30 = 62 so [78>=62, 68>=62]
28 3 78  0     # 78+30 = 108 
15 4 68  0     # 68+30 = 98
42 3 42  1     # 42+30 = 72 so [87>=72]  
24 4 87  0     # 87+30 = 117
13 3 65  0     # 65+30 = 95
 
Solution:
>>> import pandas as pd
>>> import numpy as np
>>> df=pd.read_csv("pan1.csv")
>>> df['D'] = np.where((df.C+30<=df.C.shift(-1)) | ((df.C+30<=df.C.shift(-2))),1,0)
 
[or]
>>> import pandas as pd
>>> df=pd.read_csv("pan1.csv")  
>>> df['D'] =df.C.iloc[::-1].rolling(3,min_periods=1).max().iloc[::-1].gt(df.C+30).astype(int)

Saturday 28 July 2018

List comprehension

  1. Iterating through a string Using List Comprehension

    l = [ x for x in 'abcde' ]
    print( l)
    When we run the program, the output will be:
    ['a', 'b', 'c', 'd', 'e']
  2. Using if with List Comprehension

    l=[x for x in range(25) if x%5==0]
    print(l)
  3. [0, 5, 10, 15, 20]
  4. Nested IF with List Comprehension

    l=[x for x in range(50) if x%5==0 if x%2==0]
     
    Here, list comprehension checks:
    
        Is x divisible by 5 or not
        Is x divisible by 2 or not
    
    If x satisfies both conditions, x is appended to list l.
    
  5. if...else With List Comprehension

    l=["Even" if x%2==0 else "odd" for x in range(10)]
    print(l)
    ['Even', 'odd', 'Even', 'odd', 'Even', 'odd', 'Even', 'odd', 'Even', 'odd']

  6. Nested Loops in List Comprehension

    prefix = ['A', 'B', 'C']
    suffix = ['a', 'b']
    result = [val+" "+val2 for val in prefix for val2 in suffix ]
    print(result)
    o/p
    ['A a', 'A b', 'B a', 'B b', 'C a', 'C b']
    
    
    
    
     

Python exercises

  1. a = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]. Write one line of Python that takes this list a and makes a new list that has only the even elements of this list in it.
  2. given a list,years_of_birth = [1990, 1991, 1990, 1990, 1992, 1991]
    find the list of age?
  3. Find all of the numbers from 1-1000 that have a 3 in them? 
  4. Count the number of spaces in a string
  5. Remove all of the vowels in a string
  6. Find all of the words in a string that are less than 4 letter.
  7. A list of all consonants in the sentence 'The quick brown fox jumped over the lazy dog' 
  8. A list of all the capital letters (and not white space) in 'The Quick Brown Fox Jumped Over The Lazy Dog'
  9. A list of all square numbers formed by squaring the numbers from 1 to 1000.
  10. Create the list:

    [-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,0] 
  11.     return a list of the full path to items in a directory (hint, use os.listdir() and os.path.join())
  12. now extend the exercise11 to exclude directories (hint, use os.path.isdir())
  13. now extend exercise11  further to filter to only files ending in .jpg and .png
  14. Create a sample list of random integers with list comprehension and the random module. (2 lines max, import included) 
  15. Create a 5 x 5 matrix using a list of lists 
    nest_li = [[0, 1, 2, 3, 4],
              [0, 1, 2, 3, 4],
              [0, 1, 2, 3, 4],
              [0, 1, 2, 3, 4],
              [0, 1, 2, 3, 4]]
    Hint:[[output expression] for iterator variable in iterable]
    
    Note that here, the output expression is itself a list comprehension. 

Challenge:

  1. Use a dictionary comprehension to count the length of each word in a sentence.
  2. Use a dictionary comprehension to build a map of every ascii character to its ordinal number chr : ord and update it with the reverse mapping ord : chr. Obvisouly the chr and ord functions and the string module will help. (3 lines max, import included)
  3. Use a nested list comprehension to find all of the numbers from 1-1000 that are divisible by any single digit besides 1 (2-9)

  4. For all the numbers 1-1000, use a nested list/dictionary comprehension to find the highest single digit any of the numbers is divisible by.
  5. Convert a list of dicts which you have to create with a list comprehension, all with an unique id to a dict of dicts with that id as key. (2 lines, one for the list comp, one for the dict comp)

list operations in python

How to make each term of an array repeat, with the same terms grouped together? i/p: [A,B,C] o/p: [A,A,A,B,B,B,C,C,C] Solution: >...