You are on page 1of 18

Day 1

Python for Data Science


By
Akshita Sawhney
Overview of the Bootcamp

Do’s and Don'ts

Day’s Split up

Evaluation Criteria
Topics

● List, Strings and Tuple


● Operations Such as Intersection and Union
● List Comprehension
● Enumerate Function
● User defined functions
● Accepting String as an Input from the User
Topics- Continued

● Built in methods for String handling like isupper(),islower(),lower()


● Palindrome
● Dictionary
● Map,Reduce and Filter
● Accessing elements in a Matrix
● Operations on Array
● Np.random ,Reshape on an array
Topics - Continued

● Convert Numpy to Pandas


● Convert Numpy to Array
● Creating a DataFrame
● Drop a Column in a DataFrame
● Conditions in a DataFrame
● Null Values in a Dataframe
● Operations on a Column
Python Lists
Python Lists
myList = [1, 2, 3, 2]

list.remove(value): remove() removes the first matching value/object. It does not do anything with the indexing.
myList.remove(2)

o/p: [1, 3, 2]

del list[index]: del removes the item at a specific index.


del myList[1]

o/p: [1, 3, 2]

list.pop(index): And pop removes the item at a specific index and returns it.
a = myList.pop(2)

print(a)

o/p: 3, [1,2,2]
Python Lists
● reverse() method: It reverses a list
● any() method: It checks if any Element of an Iterable is True
● sorted() method: sorted() returns sorted list from a given iterable

SLICING: list[start at: end before : stepsize]


1

>>> a = [1, 2, 3, 4, 5, 6, 7, 8]
>>> a[1:4:2]
[2, 4]
>>> a[::2] (every second element from start to end)
[2,4,6,8]
>>> a[::-1]
[8, 7, 6, 5, 4, 3, 2, 1, 0]
Set Operations (union, intersection,
difference and symmetric difference)
● INPUT:
A= {0,2,4,6,8}
B= {1,2,3,4,5}
1. UNION: print(“Union : ”, A | B)
2. INTERSECTION: print(“Intersection : ”, A & B)
3. DIFFERENCE: print(“Difference : ”, A - B)
4. SET DIFFERENCE: print(“Set Difference : ”, A ^ B)
● OUTPUT:

('Union :', set([0, 1, 2, 3, 4, 5, 6, 8]))


('Intersection :', set([2, 4]))
('Difference :', set([8, 0, 6]))
('Symmetric difference :', set([0, 1, 3, 5, 6, 8]))
Important functions of Strings
● str.center(length, fillchar)
INPUT: sent = “algorithm”
sent.centre(15,”#)
OUTPUT: ‘###algorithm###’
● str.count(value, start, end)
INPUT: sentence = (“ She sells seashells by the seashore. The shells she sells are surely
seashells)
sentence.count(“seashells”)
OUTPUT: 2
sentence.count(“seashells”,9,25)
OUTPUT: 1
● str.find(value, start, end): returns the lowest index of a particular substring, if not found then -1 is returned
INPUT: sentence = (“ She sells seashells by the seashore. The shells she sells are surely
seashells)
sentence.find(“seashells”)
OUTPUT: 10
sentence.rfind(“seashells”), OUTPUT: 69
● string.swapcase(): returns a string with all its uppercase letters converted into lower case and vice versa.
INPUT: sentence = “Queue IS another FUNDAMENTAL data STRucture”
sentence.swapcase()
OUTPUT: qUEUE is ANOTHER fundamental DATA structure
● string.startswith(value, start, end)
● string.endsswith(value, start, end)
● string.split(sep, maxsplit) ,maxsplit: denotes the number of splits. Default is -1
INPUT: fruits= “apple, mango, banana, grapes”
fruits.split() [‘apples’,’mango
OUTPUT:’,’banana’,’grapes’]
fruits.rsplit(“,”, maxsplit=1)
OUTPUT: [“apple, mango, banana”, ‘grapes’]
● String.isupper()
● string.islower()
● String.isdigit()
● String.isalpha()
● String.isalnum()
● String.replace(“H”, “J”)
List Comprehension
WAP to get the square of all the odd numbers in the given list
odd_square = []
For loop to iterate over a list of
for x in range(1, 11): integers 1 to 10

if x % 2 == 1:
odd_square.append(x**2) Condition
print odd_square Expression

odd_square = [x ** 2 for x in range(1, 11) if x % 2 == 1]


print odd_square
Enumerate
Enumerate is a built-in function of Python. It allows us to loop over
something and have an automatic counter
li = [1,2,3,4,5,6,7,8,9]
odd_digits = [x for (i,x) in enumerate(li) if i % 2 == 0]
print(odd_digits)

O/P: [1, 3, 5, 7, 9]
LIST , SET and TUPLES
LIST TUPLES SET

Mutable Immutable(reassign or delete an Mutable


entire tuple, you cannot do the
same to a single item or a slice)

L = [], L= list() T = (1,) and not T=(1): int S = set()

Heterogeneous, Can hold Heterogeneous,Can hold Does not hold duplicate values
duplicate values. duplicate values. and is unordered: there is no
way we can use indexing to
access or delete its elements

List is slower than tuple: Set are faster than list in terms
because list are mutable , with of accessing or removing an
any changes done a new element(hash table)
object space is made
Python Dictionary
●get() method : It returns Value of The Key
dic = {"A":1, "B":2}
print(dic.get("A"))
o/p: 1

●update() method : Update method is used to modify the value of any existing
element
d = {1: "one", 2: "three"}
d1 = {2: "two"}
# updates the value of key 2
d.update(d1)
print(d)
O/P: {1: 'one', 2: 'two'}
Lambda Function
● In Python, anonymous function is a function that is defined without a name
● Normal functions are defined using the def keyword, in Python anonymous
functions are defined using the lambda keyword

# Program to show the use of lambda functions


double = lambda x: x * 2
# Output: 10
print(double(5))
Map, Filter and Reduce
MAP: applies a function to all the items in an input_list.
items = [1, 2, 3, 4, 5]
squared = list(map(lambda x: x**2, items))

FILTER: creates a list of elements for which a function returns true


number_list = range(-5, 5)
less_than_zero = list(filter(lambda x: x < 0, number_list))
print(less_than_zero)
Output: [-5, -4, -3, -2, -1]

Reduce: is a really useful function for performing some computation on a list and returning the result.
from functools import reduce
product = reduce((lambda x, y: x * y), [1, 2, 3, 4]) # Output: 24
np.linspace, np.arange, np.Random
np.arange(start, stop,step size) np.linspace(start, stop,number of sample)

You might also like