Saturday 4 August 2018

Pandas exercise 1

>>>>import pandas as pd
>>>df = pd.DataFrame({"val": list(zip(range(9), range(9)[::-1]))})
>>>df 

use query to subset the df using the first entry(>3) of the tuple. 
      val
0  (0, 8)
1  (1, 7)
2  (2, 6)
3  (3, 5)
4  (4, 4)
5  (5, 3)
6  (6, 2)
7  (7, 1)
8  (8, 0)
Solution:
#Access the first elements of the tuple
>>> df.val.str[0]
0    0
1    1
2    2
3    3
4    4
5    5
6    6
7    7
8    8
>>> df.val.str[0].to_frame().query('val > 3')
   val
4    4
5    5
6    6
7    7
8    8
.str will work with any object column (this includes columns of lists and tuples), not just strings (strings are considered objects, one of many possible types).

Wednesday 1 August 2018

list exercise in python

  1. Write a Python program to sum all the items in a list
  2. Write a Python program to multiplies all the items in a list
    >>>l
    [4, 5, 6, 7]
    >>> reduce(lambda x,y:x*y,l)
    840
  3. Write a Python program to get the largest number from a list
  4. Write a Python program to get the smallest number from a list
  5. Write a Python program to find the list of words that are longer than n from a given list of words.
  6. Write a Python program to count the number of strings where the string length is 2 or more and the first and last character are same from a given list of strings.
    >>> s=['12341','aaa','abc', 'xyz', 'aba', '1221','xx']
    >>> new_s=[x for x in s if len(x)>=2 and x[0]==x[-1]]
    >>> new_s
    ['12341', 'aaa', 'aba', '1221', 'xx']
     
    
    
      
  7. Write a Python program to get a list, sorted in increasing order by the last element in each tuple from a given list of non-empty tuples.
  8.  Sample List : [(2, 5), (1, 2), (4, 4), (2, 3), (2, 1)]
    Expected Result : [(2, 1), (1, 2), (2, 3), (4, 4), (2, 5)]
    >>> l= [(2, 5), (1, 2), (4, 4), (2, 3), (2, 1)]
    >>>l.sort(key=lambda x:x[1])
    >>>l
    [(2, 1), (1, 2), (2, 3), (4, 4), (2, 5)]
    [or]
    >>> import operator
    >>> l= [(2, 5), (1, 2), (4, 4), (2, 3), (2, 1)]
    >>> l.sort(key=operator.itemgetter(1))
    >>> l
    [(2, 1), (1, 2), (2, 3), (4, 4), (2, 5)]
  9. Write a Python program to remove duplicates from a list
  10. Write a Python program to check a list is empty or not
  11.  Write a Python program to remove all the elements from a list(empty the list).
  12. Write a proram to remove all occurences of a element(ex:5) from a list.
    >>> l=[2,3,4,2,3,4,5,6,7,5,4,2]
    >>> filter(lambda x:x!=5,l)
    [2, 3, 4, 2, 3, 4, 6, 7, 4, 2]
  13.  Write a Python program to clone or copy a list.
  14. Write a Python function that takes two lists and returns True if they have at least one common member.
  15. Write a Python program to print a specified list after removing the 0th, 4th and 5th elements.
    Sample List : ['Red', 'Green', 'White', 'Black', 'Pink', 'Yellow','Red']
    Expected Output : ['Green', 'White', 'Black','Red']
    >>> slist=['Red', 'Green', 'White', 'Black', 'Pink', 'Yellow','Red']
    >>> nlist=[e for i,e in enumerate(slist) if i not in [0,4,5]]
    >>> nlist
    ['Green', 'White', 'Black', 'Red']
  16. Write a Python program to generate a 3*4*6 3D array, whose each element is *
  17. Write a Python program to print the numbers of a specified list after removing even numbers from it
  18. Write a Python program to shuffle and print a specified list
    >>> l=[1,2,3,4,5,6,3,4,8,9,22,44,54,31,23]
    >>> random.shuffle(l)
    >>> l
    [23, 44, 4, 1, 5, 4, 54, 9, 22, 31, 6, 2, 3, 8, 3]
  19.  Write a Python program to generate and print a list of first and last 5 elements where the values are square of numbers between 1 and 30 (both included).
    >>> l=list(range(31))
    >>> l=[x*x for x in l[1:6]+l[-5:]]
    >>> l
    [1, 4, 9, 16, 25, 676, 729, 784, 841, 900]
  20. Write a Python program to generate and print a list except for the first 5 elements, where the values are square of numbers between 1 and 30 (both included).
    >>> l=list(range(31))
    >>> l=[x*x for x in l[5:-5]]
  21. Write a Python program to generate all permutations of a list in Python.
    >>> import itertools
    >>> l=[1,2,3,4]
    >>> list(itertools.permutations(l,2))
    [(1, 2), (1, 3), (1, 4), (2, 1), (2, 3), (2, 4), (3, 1), (3, 2), (3, 4), (4, 1), (4, 2), (4, 3)]
  22. Write a Python program to get the difference between the two lists
    >>> x=[1,2,3]
    >>> y=[4,5,6]
    >>> list(set(x)^set(y))
    [or]
    >>> list(set(x).symmetric_difference(y))
    [1, 2, 3, 4, 5, 6]
  23. Write a Python program access the index of a list. 
  24. Write a Python program to convert a list of characters into a string.
  25. Write a Python program to find the index of an item in a specified list.
  26. Write a Python program to flatten a shallow list.
    >>> l=[[1,2,3],[4,5],[7,8,9]]
    >>> from itertools import chain
    >>> list(chain(*l))
    [1, 2, 3, 4, 5, 7, 8, 9]
    [or]
    >>> l=[[1,2,3],[4,5],[7,8,9]]
    >>> sum(l,[])
    [1, 2, 3, 4, 5, 7, 8, 9]
  27. Write a Python program to append a list to the second list.
  28. Write a Python program to select an item randomly from a list.
    >>> l=list(range(31))
    >>> random.choice(l)
    13
  29. Write a python program to check whether two lists are circularly identical.
  30. Write a Python program to get unique values from a list.
    >>> l=[1,2,3,4,5,3,4,8]
    >>> list(set(l))
    [1, 2, 3, 4, 5, 8]
  31. Write a Python program to find the second smallest number in a list.
  32. Write a Python program to find the second largest number in a list.
  33. Write a Python program to get the frequency of the elements in a list
    >>> l=[1,2,3,4,5,3,4,8]
    >>> {x:l.count(x) for x in l}
    {1: 1, 2: 1, 3: 2, 4: 2, 5: 1, 8: 1}
  34. Write a Python program to count the number of elements in a list within a specified range.
    >>> x=[10,23,21,45,65,34,25,78,54,43,89]
    >>> len([i for i in x if i>=30 and i<=60])
    4
  35. Write a Python program to check whether a list contains a sublist.
  36. Write a Python program to generate all sublists of a list


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: >...