PYTHON
PYTHON
objects. Everything in Python is an object, including numbers, strings, lists, and <class 'bool'>
even functions. Objects have attributes and methods associated with them. Set:
Numbers: A set is an unordered collection of unique elements. Sets are mutable, meaning
they can be modified after creation, but they cannot contain duplicate elements.
Python supports various types of numbers, including integers, floating-point
numbers, and complex numbers.
In [ ]: my_set = {1, 2, 3, 4, 5}
print(my_set)
• Integer: print(type(my_set))
Sequence:
• Float
Strings in Python are sequences of characters enclosed in single, double, or triple
In [ ]: num_int_error = 3.14 quotes. They support various operations such as concatenation, slicing, and
print(num_int_error)
print(type(num_int_error)) formatting.
3.14
In [ ]: my_str = "Hello, World!"
<class 'float'>
print(my_str)
print(type(my_str))
• Complex
Hello, World!
<class 'str'>
In [ ]: num_complex = 2 + 3j
print(num_complex) # List:
print(type(num_complex))
A list is a versatile data structure used to store collections of items. Lists are
(2+3j)
mutable, meaning they can be modified after creation. Each item in a list is
<class 'complex'>
indexed, and you can access elements by their index. Lists can contain items of
Bool: Booleans represent truth values, either True or False .
different data types, and they can also contain other lists (nested lists).
In [ ]: bool_true = True
In [ ]: my_list = [1, 2, 3, 4, 5]
print(bool_true)
print(my_list)
print(type(bool_true))
print(type(my_list))
True
[1, 2, 3, 4, 5]
<class 'bool'>
<class 'list'>
In [ ]: bool_false = False Tuple:
print(bool_false)
print(type(bool_false))
A tuple in Python is a collection of ordered and immutable elements. It is similar to
a list, but unlike lists, tuples are immutable, meaning their elements cannot be In [1]: a = 3
changed after creation. Tuples are defined using parentheses () and can contain
elements of different data types. In [2]: a
Out[2]: 3
In [ ]: my_tuple = (1, 'two', 3.0)
print(my_tuple)
print(type(my_tuple)) In [4]: b = "Ajay"
None is a special constant representing the absence of a value or a null value. It is In [6]: b[2]
often used to indicate that a variable or an object does not have a meaningful Out[6]: 'a'
value or has not been assigned yet.
In [7]: b[-2]
In [ ]: my_none = None
print(my_none) Out[7]: 'a'
print(type(my_none))
In [8]: #anaother type of data container
None
list_cont = [1, 2, 3, "Ajay", 3+5j, True, "a"]
<class 'NoneType'>
In [9]: type(list_cont)
Out[9]: list
In [10]: list_cont[3]
Out[10]: 'Ajay'
In [12]: list_cont
In [13]: #objects/container whose state or value can be changed after they are created are call
In [16]: b = "pwskills"
In [17]: b[2]
Out[17]: 's'
Out[1]: 7
In [3]: add
Out[3]: 7
In [4]: 5+2
Out[4]: 7
In [6]: 20 - 3
Out[6]: 17
In [7]: 3 * 4
Out[7]: 12
In [8]: 20 / 4
Out[8]: 5.0
Out[9]: 1
In [10]: 2 ** 3 #2*2*2
Out[10]: 8
In [11]: 3 ** 2
Out[11]: 9
In [13]: 4/ 3
Out[13]: 1.3333333333333333 In [24]: #Logical operator
#and
True and True
In [14]: 4 // 3 #floor operator
Out[24]: True
Out[14]: 1
Out[18]: False
Out[31]: False
In [19]: 10 >= 10
In [32]: not False
Out[19]: True
Out[32]: True
In [20]: 10 >= 1
In [33]: #Assignment operator
Out[20]: True
a =10
In [21]: 10<=10
In [34]: a
Out[21]: True
Out[34]: 10
In [22]: 10 <= 12
In [35]: a + 5
Out[22]: True
Out[35]: 15
In [23]: 10 >= 12
In [36]: a
Out[23]: False
Out[36]: 10
In [38]: a = a + 10 Out[52]: True
Out[39]: 20
In [56]: "data" in a
In [41]: a In [58]: #identity operator >> compare the memory location of two object
a = 2
Out[41]: 25
In [59]: b = 3
In [42]: a
In [60]: a is b
Out[42]: 25
Out[60]: False
In [44]: a *= 2
In [61]: b is a
In [45]: a
Out[61]: False
Out[45]: 50
Out[46]: 50
In [63]: b is a
In [48]: a In [64]: a is b
Out[49]: True
In [66]: a is not b
Out[50]: True
In [129… #bitwise operator>> operations at bit level
10 & 10
In [51]: "data" in "PWSKILLS"
Out[129… 10
Out[51]: False
In [130… 18 & 3
In [52]: "data" not in "PWSKILLS"
Out[130… 2 Out[144… '0b101'
Out[142… -7 Out[154… 35
In [143… #Bitwise xor >> return 1 when exactly one operand is 1 In [155… bin(280)
5 ^ 3 Out[155… '0b100011000'
10
In [159… bin(5)
In [ ]: type(int_num)
Out[159… '0b101'
Out[ ]: int
In [160… ## order of precedence
#execution will start from left
In [ ]: # Convert a string to an integer
2+3-1
str_num = "42"
int_str = int(str_num)
Out[160… 4
print(int_str)
(5+3) - 4 In [ ]: type(int_num)
In [162… 4 - (5+3)
2. Converting to Float (float())
Out[162… -4
In [ ]: # Convert an integer to a float
In [163… (10+5) + (8/2) int_num = 34
float_num = float(int_num)
Out[163… 19.0 print(float_num)
34.0
In [ ]:
In [ ]: type(float_num)
Out[ ]: float
2.12
In [ ]: type(float_num)
Out[ ]: float
In [1]: #Type conversion >> The process of changing the data type of a value or object in Pyth
3. Converting to String (str()) a = "5"
type(a)
In [ ]: # Convert an integer to a string
Out[1]: str
int_num = 32
str_num = str(int_num)
print(str_num) In [2]: a+2
32 ---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
In [ ]: type(str_num) Cell In[2], line 1
----> 1 a+2
Out[ ]: str
TypeError: can only concatenate str (not "int") to str
In [ ]: # Convert a float to a string
float_num = 2.89 In [3]: type(2)
str_num = str(float_num)
print(str_num) Out[3]: int
2.89
In [4]: type(a)
In [ ]: type(str_num)
Out[4]: str
Out[ ]: str
In [ ]: #TYPE CASTING >> implicit and explicit
In [12]: type(a)
Out[12]: str In [ ]:
In [13]: a+"shyam"
Out[13]: '5shyam'
In [14]: #explicit type conversion >> convert data type using inbuilt function
#string
#int
#float
#bool
In [15]: x = 22.5
y = int(22.5)
In [16]: type(y)
Out[16]: int
In [17]: y
Out[17]: 22
In [19]: s = "33"
a = int(s)
In [20]: type(a)
Out[20]: int
In [21]: b = "33.5"
c = float(b)
In [22]: c
Out[22]: 33.5
In [23]: type(c)
Out[23]: float
In [24]: bool(0)
Out[24]: False
In [25]: bool(1)
Out[25]: True
'if' Statement 'if-elif-else' Statement
In [ ]: course = "DataScience" In [ ]: course = "DataScience"
if course == "DataScience": if course == "DataScience":
print("You are on the right track!") print("You are in PWskills's course.")
elif course == "Physics":
You are on the right track! print("You are in Physics Wallah's course.")
else:
In [ ]: PWskills_grades = 8
print("You are not enrolled in any course.")
if PWskills_grades >= 7:
print("Impressive PWskills!") You are in PWskills's course.
In [ ]: marks = 90
if grades >= 7:
if marks >= 90:
print("Impressive skills!")
print("You got an 'A' grade!")
elif grades >= 5:
You got an 'A' grade! print("You're doing well at Physics Wallah!")
else:
In [ ]: number = 42 print("Keep working on your skills.")
if number % 2 == 0:
You're doing well at Physics Wallah!
print("The number is even.")
Zero
In [ ]: age = 30
if age < 18:
print("You are a minor.") Nested 'if-else' Statement
elif 18 <= age < 65:
print("You are an adult.")
In [ ]: course = "DataScience"
else:
print("You are a senior citizen.")
grades = 7
You are an adult. if course == "DataScience":
if grades >= 7:
print("Impressive skills in Data Science at PWskills!")
'if-else' Statement Impressive skills in Data Science at PWskills!
In [ ]: In [ ]: marks = 88
if marks >= 80:
In [ ]: course = "DataScience" if marks >= 90:
print("You got an 'A' grade!")
if course == "DataScience": else:
print("You are studying Data Science.") print("You got a 'B' grade.")
else: You got a 'B' grade.
print("You are not in the Data Science course.")
DataScience
In [ ]:
DataAnalytics
Python
JavaScript
I like apple
I like banana
I like cherry
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
Course 1: DataScience
Course 2: DataAnalytics
Course 3: Python
Course 4: JavaScript
In [ ]: # Multiplication table 2 * 1 = 2
2 * 2 = 4
for i in range(1, 6): 2 * 3 = 6
for j in range(1, 11): 2 * 4 = 8
product = i * j 2 * 5 = 10
print(f"{i} * {j} = {product}") 2 * 6 = 12
print() 2 * 7 = 14
2 * 8 = 16
2 * 9 = 18
2 * 10 = 20
3 * 1 = 3
3 * 2 = 6
3 * 3 = 9
3 * 4 = 12
3 * 5 = 15
3 * 6 = 18
3 * 7 = 21
3 * 8 = 24
3 * 9 = 27
3 * 10 = 30
4 * 1 = 4
4 * 2 = 8
4 * 3 = 12
4 * 4 = 16
4 * 5 = 20
4 * 6 = 24
4 * 7 = 28
4 * 8 = 32
4 * 9 = 36
4 * 10 = 40
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
'while' loop
In [ ]: # Printing a Pattern
for i in range(5):
In [ ]: #Printing course names until a condition is met
for j in range(i + 1):
print("*", end=" ")
courses = ["DataScience", "DataAnalytics", "Python", "JavaScript"]
print()
index = 0
*
* * while index < len(courses):
* * * print(courses[index])
* * * * index += 1
* * * * *
DataScience
DataAnalytics
In [ ]: # Creating a Matrix
Python
JavaScript
rows = 3
cols = 3
In [ ]: #Counting the number of characters in course names
matrix = []
Learn: Python
Learn: Python
Learn: Data Science
In [2]: #List >> it is an ordered collection of elements that can be of any data type, integer, float Out[15]: [1, 2, True, (3+7j), 'pwskills']
#List are mutable
In [16]: lis[2]
In [3]: num = [1, 2, 3, 4, 5]
Out[16]: True
In [4]: type(num)
In [17]: lis = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Out[4]: list
In [18]: type(lis)
In [5]: num[0]
Out[18]: list
Out[5]: 1
In [19]: lis
In [6]: num[1]
Out[19]: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Out[6]: 2
In [21]: lis[0][1]
In [7]: num[4]
Out[21]: 2
Out[7]: 5
In [23]: lis[2][2]
In [8]: num[5]
Out[23]: 9
---------------------------------------------------------------------------
IndexError Traceback (most recent call last) In [24]: shopping_list = ["Banana", "Apple", 3, 4.5, True]
Cell In[8], line 1
----> 1 num[5] In [25]: type(shopping_list)
In [35]: name = ("Ajay", "Sumit", "Bijay") In [51]: s = {"a", "a", "a", 1.2, 1.2}
--------------------------------------------------------------------------- ---------------------------------------------------------------------------
TypeError Traceback (most recent call last) TypeError Traceback (most recent call last)
Cell In[38], line 1 Cell In[54], line 1
----> 1 name[1] = "Bangalore" ----> 1 s[0]
TypeError: 'tuple' object does not support item assignment TypeError: 'set' object is not subscriptable
Out[43]: (('a', 'b'), ('c', 'd')) TypeError: 'set' object is not subscriptable
In [57]: s.add("pwskills")
In [44]: for bag in choclate_bag:
for choc in bag:
In [58]: s
print(choc)
In [61]: s
In [47]: type(s)
Out[61]: {1.2, 'a', 'pwskills'}
Out[47]: set
In [62]: s.remove("pwskills")
In [48]: s = {"red", "blue", "blue"}
In [63]: s
Out[63]: {1.2, 'a'} In [81]: phonebook.values()
In [66]: my_dict In [85]: from array import array # array is similar to list>> homogenous data
Out[70]: dict In [ ]:
In [71]: s = {}
type(s)
Out[71]: dict
In [73]: type(my_dict)
Out[73]: dict
In [75]: type(phonebook)
Out[75]: dict
In [76]: phonebook["Dad"]
Out[76]: 1234
In [78]: phonebook
In [80]: phonebook.keys()
In [72]: text.title()
str1 == str2
Out[72]: 'Hello, World'
Out[84]: False
In [73]: text1 = "I am Is A Student"
text1.swapcase()
In [85]: str1 = "Hello"
str2 = "Hello"
Out[73]: 'i AM iS a sTUDENT'
talk to reception
In [17]: name
In [4]: #STRING ORDERING
Out[17]: 'Ajay'
In [7]: playlist = ["KKHH", "Magic", "Hello", "Magic"] In [29]: #Escape sequence >> special combination of characters used within a strings
sorted(playlist)
address = """123, sanmukha villa,
Out[7]: ['Hello', 'KKHH', 'Magic', 'Magic'] marathali,
bangalore """
Out[8]: 'PWSKILLS' In [31]: address = """123, sanmukha villa, \nmarathali, \nbangalore """
In [9]: input_str = " PW SKILLS " In [33]: print(address) #"\n">> to insert a new line character
input_str.strip()
123, sanmukha villa,
Out[9]: 'PW SKILLS' marathali,
bangalore
In [10]: csv_row = "Apple "
In [36]: #\t escape charcter
csv_row.strip()
table = "Name\tAge\tGrade\nAlice\t20\tA\nRam\t30\tA+"
print(table)
Out[10]: 'Apple'
Name Age Grade
In [11]: #string splitting Alice 20 A
data = "Ajay, data science, teacher" Ram 30 A+
data
In [42]: file_path = "C:\\USERS\\DOWNLOAD\\FILES.txt"
Out[11]: 'Ajay, data science, teacher' print(file_path)
C:\USERS\DOWNLOAD\FILES.txt
In [13]: teacher_info = data.split(',')
In [45]: #\' single quote escape
In [14]: teacher_info str1 = 'I can\'t believe its a friday'
str1
Out[14]: ['Ajay', ' data science', ' teacher']
Out[45]: "I can't believe its a friday"
In [15]: name = teacher_info[0]
sub = teacher_info[1] In [47]: #Carriage return \r >> moves the cursor to the beginning of line
des = teacher_info[2]
print("Hello, I am \rAjay. I am going to Delhi") In [73]: str1 = "pwskills"
str1.endswith("s")
Ajay. I am going to Delhi
Out[73]: True
In [56]: #string formatting >> creating string with placeholder for variables
username = "Shyam"
greeting = f"hello {username}, how are you?" In [74]: str1 = "pwskills"
print(greeting) str1.endswith("A")
Out[70]: True
Out[71]: False
0 In [1]: #List are ordered collection of items
p #[]
1 #list is like a shopping bag which can store everything
w
2 In [2]: type([])
s
3 Out[2]: list
k
4
i In [3]: grocery_list = ["Milk", "Orange", 1, 2, 3, True, 2+4j, 2.3]
5
l In [4]: type(grocery_list)
6
l Out[4]: list
7
s In [5]: grocery_list
In [7]: grocery_list[0]
Out[7]: 'Milk'
In [8]: grocery_list[-1]
Out[8]: 2.3
In [9]: grocery_list[1:]
In [10]: grocery_list[1:4]
Out[10]: ['Orange', 1, 2]
In [12]: movies[0:3]
In [14]: pages[-1]
Out[14]: 'index'
In [15]: pages[-2]
Out[15]: 'conclusion' In [31]: lis1
Out[22]: ['Sare jhan se acha', 'Ae mere watan ke logo'] In [40]: my_list
Out[26]: ['Apple', 'Banana', 'orange'] Out[42]: ['Apple', 'Banana', 'orange', 'brinjal', 'potato']
"*" * 5
In [30]: lis1 #list are mutable
Out[43]: '*****'
Out[30]: ['Apple', 'brinjal', 'orange']
In [44]: "-" * 50 In [57]: #membership in , not in
Out[44]: '--------------------------------------------------'
grocery_list
In [46]: [0] * 10
Out[57]: ['Milk', 'Orange', 1, 2, 3, True, (2+4j), 2.3]
Out[46]: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
In [60]: "Milk" in grocery_list
In [47]: [1, 2, 3] * 10
Out[60]: True
Out[47]: [1,
2, In [61]: "Banana" in grocery_list
3,
1, Out[61]: False
2,
3, In [62]: "Banana" not in grocery_list
1,
2, Out[62]: True
3,
1,
In [63]: grocery_list
2,
3,
Out[63]: ['Milk', 'Orange', 1, 2, 3, True, (2+4j), 2.3]
1,
2,
3, In [75]: #shallow copy>> value will change with change in other list|
1, a = grocery_list
2,
3, In [65]: a
1,
2, Out[65]: ['Milk', 'Orange', 1, 2, 3, True, (2+4j), 2.3]
3,
1,
In [66]: grocery_list[0] = "Banana"
2,
3,
1, In [67]: grocery_list
2,
3, Out[67]: ['Banana', 'Orange', 1, 2, 3, True, (2+4j), 2.3]
1,
2, In [68]: a
3]
Out[68]: ['Banana', 'Orange', 1, 2, 3, True, (2+4j), 2.3]
In [50]: list(range(1, 6)) * 3
In [69]: #deep copy will not change with change in other list
Out[50]: [1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
b = grocery_list.copy()
In [99]: book_list.pop()
In [79]: book_list
In [100… book_list
In [80]: book_list.index("Algorithm")
In [103… book_list
In [85]: book_list
In [88]: book_list
In [108… doubled_price
Out[88]: ['Data Structure', 'Algorithm', 'Web dev']
Out[108… [20, 40, 60, 80, 100]
In [89]: book_list.sort()
In [110… doubled_price = [price * 2 for price in prices]
In [91]: book_list
In [111… doubled_price
Out[91]: ['Algorithm', 'Data Structure', 'Web dev']
Out[111… [20, 40, 60, 80, 100] In [127… #list as stack and queue
Out[119… ['ppt', 'pdf', 'jpg', 'py'] Out[129… ['plate1', 'plate2', 'plate3', 'plate4']
Out[130… 'plate4'
email_address = ["[email protected]", "[email protected]", "[email protected]", "[email protected]"
In [131… stack_of_plates
[email for email in email_address if email.endswith("@yahoo.com")]
Out[131… ['plate1', 'plate2', 'plate3']
Out[120… ['[email protected]', '[email protected]']
In [132… browsing_history = []
In [123… #Nested list comprehension browsing_history.append("home_page")
browsing_history.append("about us")
browsing_history.append("contact")
pairs = []
In [133… browsing_history
for x in [1, 2, 3]:
for y in [4, 5, 6]:
Out[133… ['home_page', 'about us', 'contact']
pairs.append([x, y])
In [134… browsing_history.pop()
In [124… pairs
Out[134… 'contact'
Out[124… [[1, 4], [1, 5], [1, 6], [2, 4], [2, 5], [2, 6], [3, 4], [3, 5], [3, 6]]
In [135… browsing_history
In [125… [[x, y] for x in [1, 2, 3] for y in [4, 5, 6]]
checkout = deque()
checkout.append("cus1")
checkout.append("cus2")
checkout.append("cus3")
checkout.append("cus4")
In [138… checkout
serving cus1
serving cus2
serving cus3
serving cus4
In [20]: tuple1[0:4]
In [6]: tuple1[0]
Out[20]: ('pwskills', (7+3j), True, 1)
Out[6]: 'pwskills'
In [23]: tuple1[::-1]
In [7]: #Tuples are immutable
tuple1[0] = "data" Out[23]: (2.2, 2, 1, 1, 2, 1, True, (7+3j), 'pwskills')
---------------------------------------------------------------------------
TypeError Traceback (most recent call last) In [25]: tuple1[-1]
Cell In[7], line 2
1 #Tuples are immutable Out[25]: 2.2
----> 2 tuple1[0] = "data"
In [28]: for i in tuple1:
TypeError: 'tuple' object does not support item assignment print(i, type(i))
TypeError: 'tuple' object does not support item assignment In [29]: t1 = (1, 2, 100, 4, 5)
In [11]: tuple1
In [30]: max(t1)
Out[11]: ('pwskills', (7+3j), True, 1, 2, 1, 1, 2, 2.2)
Out[30]: 100
In [12]: tuple1.count(1)
In [31]: min(t1)
Out[12]: 4
Out[31]: 1
In [13]: tuple1.count(2)
In [32]: t2 = (2, 3, 500)
In [34]: t3 = (t1, t2) Out[47]: (1, 2, 100, 4, 5, 2, 3, 500)
In [35]: t3 In [48]: #Set>> sets are collection of unique and unordered elements
#dont allow duplicate elements>> not mauintain any order and it will not be indexed
Out[35]: ((1, 2, 100, 4, 5), (2, 3, 500))
In [49]: s = {}
In [36]: del t3
In [50]: s
In [37]: t3
Out[50]: {}
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[37], line 1 In [51]: type(s)
----> 1 t3
Out[51]: dict
NameError: name 't3' is not defined
In [52]: s = {2, 3, 4}
In [39]: len(t1)
In [53]: type(s)
Out[39]: 5
Out[53]: set
In [40]: t1
In [54]: s = {1, 1, 1, 2, 1, 2, 2, 1, "ajay", "ajay", "Ajay"}
Out[40]: (1, 2, 100, 4, 5)
In [55]: s
In [41]: 100 in t1
Out[55]: {1, 2, 'Ajay', 'ajay'}
Out[41]: True
Out[43]: True
In [61]: s[0]
1
Out[91]: set()
2
3
(1, 2, 3) In [92]: s = {(1, 2, 3), 100, '2', 3}
In [73]: s In [93]: s
Out[74]: 4
In [95]: s
---------------------------------------------------------------------------
KeyError Traceback (most recent call last) In [109… #symmetric difference
Cell In[98], line 1 s1 ^ s2
----> 1 s.remove(5) #remove will throw an error if the given element is not pre
sent in set Out[109… {'hiking', 'photography', 'reading', 'travelling'}
In [99]: s In [111… s
In [100… s.discard(5) #error will not be thrown if element is not present In [112… s.add(123)
In [101… s In [113… s
In [102… #Set operations In [115… #frozen set >> immutable version of set., can not be changed added or removed
#Union: combines elements from two sets excluding duplicates
#Intersection: only common elements btw sets my_frozenset = frozenset([1, 2, 3, 4, 4, 5, 5])
#difference: return the elements that is present in first set and not in second
#symmetric difference: returns elements that are present in either of sets but not in both
In [116… type(my_frozenset)
In [117… my_frozenset.add(5, 6, 7)
In [104… s1
---------------------------------------------------------------------------
Out[104… {'coding', 'hiking', 'reading'} AttributeError Traceback (most recent call last)
Cell In[117], line 1
In [105… s2 ----> 1 my_frozenset.add(5, 6, 7)
Out[105… {'coding', 'photography', 'travelling'} AttributeError: 'frozenset' object has no attribute 'add'
In [118… my_frozenset.pop()
In [106… #intersection >> &
---------------------------------------------------------------------------
s1 & s2
AttributeError Traceback (most recent call last)
Cell In[118], line 1
Out[106… {'coding'}
----> 1 my_frozenset.pop()
In [108… #difference
s1 - s2
In [2]: #dictionary is a data structure that stores data as key value pair Out[22]: {101: 'abc'}
#Unordered, Keys are unique and immutable
In [23]: d = {1.2:"abc"}
In [3]: d = {}
type(d)
In [24]: d
Out[3]: dict
Out[24]: {1.2: 'abc'}
In [6]: type(d)
In [26]: d
Out[6]: dict
Out[26]: {True: 'abc'}
In [7]: d
In [27]: d = {#:"abc"}
Out[7]: {'name': 'Ajay', 'email': '[email protected]', 'contact': 1234}
Cell In[27], line 1
d = {#:"abc"}
In [10]: l = [100, 200, 300] ^
SyntaxError: incomplete input
In [13]: d['name']
In [28]: d = {@:"abc"}
Out[13]: 'Ajay'
Cell In[28], line 1
d = {@:"abc"}
In [14]: d['email']
^
SyntaxError: invalid syntax
Out[14]: '[email protected]'
In [29]: d = {[1, 2,3]:"abc"}
In [15]: d['name'] = "bijay"
---------------------------------------------------------------------------
In [16]: d TypeError Traceback (most recent call last)
Cell In[29], line 1
Out[16]: {'name': 'bijay', 'email': '[email protected]', 'contact': 1234} ----> 1 d = {[1, 2,3]:"abc"}
--------------------------------------------------------------------------- In [51]: d
TypeError Traceback (most recent call last)
Cell In[33], line 1 ---------------------------------------------------------------------------
----> 1 d = {{"key":123}:"abc"} NameError Traceback (most recent call last)
Cell In[51], line 1
TypeError: unhashable type: 'dict' ----> 1 d
In [34]: d = {"name": "Ajay", "email": "[email protected]", "contact" : 1234, "name": "ajay"} NameError: name 'd' is not defined
In [37]: d Out[54]: {}
Out[37]: {'name': 'data science', 'email': '[email protected]', 'contact': 1234} In [55]: d = {"name": ["Ajay", "Bijay", "Sanjay"], "course": ("ds", "ml")}
len(d)
In [38]: d = {"name": ["Ajay", "Bijay", "Sanjay"], "course": ("ds", "ml")}
Out[55]: 2
In [39]: d
In [56]: d
Out[39]: {'name': ['Ajay', 'Bijay', 'Sanjay'], 'course': ('ds', 'ml')}
Out[56]: {'name': ['Ajay', 'Bijay', 'Sanjay'], 'course': ('ds', 'ml')}
In [42]: d['name'][1]
In [57]: d.keys()
Out[42]: 'Bijay'
Out[57]: dict_keys(['name', 'course'])
In [44]: d["date_of_start"] = "15th March"
In [58]: d.values()
In [45]: d
Out[58]: dict_values([['Ajay', 'Bijay', 'Sanjay'], ('ds', 'ml')])
Out[45]: {'name': ['Ajay', 'Bijay', 'Sanjay'],
'course': ('ds', 'ml'), In [59]: d.items()
'date_of_start': '15th March'}
Out[59]: dict_items([('name', ['Ajay', 'Bijay', 'Sanjay']), ('course', ('ds', 'ml'))])
In [46]: d['name'] = "Rakesh"
In [60]: d1 = d.copy()
In [47]: d
In [61]: d1
Out[47]: {'name': 'Rakesh', 'course': ('ds', 'ml'), 'date_of_start': '15th March'}
Out[61]: {'name': ['Ajay', 'Bijay', 'Sanjay'], 'course': ('ds', 'ml')}
In [48]: del d['date_of_start']
In [62]: del d1
In [49]: d
In [64]: d
Out[49]: {'name': 'Rakesh', 'course': ('ds', 'ml')}
Out[64]: {'name': ['Ajay', 'Bijay', 'Sanjay'], 'course': ('ds', 'ml')} Out[77]: {'Arun': 80, 'Ajay': 95, 'Bob': 90}
Out[67]: {'name': ['Ajay', 'Bijay', 'Sanjay']} In [85]: {student:marks for student, marks in zip(students, marks)}
Out[72]: {'name': ['Ajay', 'Bijay', 'Sanjay'], 'course': 'DS', 'start': '20th mar'} In [89]: employee_records['E001']
In [91]: restaurant_menu['Dish1']
In [75]: d.fromkeys((1, 2, 3), ('a', 'b', 'c'))
Out[91]: {'name': 'Pasta Carbonara',
Out[75]: {1: ('a', 'b', 'c'), 2: ('a', 'b', 'c'), 3: ('a', 'b', 'c')}
'price': 15.99,
'description': 'Creamy pasta with bacon and parmesan'}
In [79]: #dictionary comprehension
In [92]: restaurant_menu
students = ['Arun', 'Ajay', 'Bob']
marks = [80, 95, 90] Out[92]: {'Dish1': {'name': 'Pasta Carbonara',
'price': 15.99,
student_marks = {} 'description': 'Creamy pasta with bacon and parmesan'},
'Dish2': {'name': 'Chicken Caesar Salad',
for student, mark in zip(students, marks): 'price': 12.5,
student_marks[student] = mark 'description': 'Grilled chicken with romaine lettuce and Caesar dressing'},
'Dish3': {'name': 'Margherita Pizza',
In [77]: student_marks 'price': 14.0,
'description': 'Pizza topped with tomato, mozzarella, and basil'}}
In [93]: #Dictionary view objects In [1]: print("hello world")
restaurant_menu.keys()
hello world
Out[93]: dict_keys(['Dish1', 'Dish2', 'Dish3'])
In [2]: len("hello")
In [7]: float(2)
Out[7]: 2.0
Out[9]: 7
In [10]: m.floor(3.5)
Out[10]: 3
Out[38]: 2
In [21]: "this is my first function" + " in Python"
Out[39]: 4
In [22]: func() + " in Python"
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str' Out[41]: True
In [25]: func()
In [43]: a, b, c, d, e = func()
this is my first function
In [44]: func()[2]
In [26]: type(func())
Out[44]: 4
this is my first function
Out[26]: NoneType
In [45]: def func():
calc = 2+3+6*5
In [27]: def func(): return calc
return "this is my first function"
In [47]: type(func())
In [28]: func()+" in Python"
Out[47]: int
Out[28]: 'this is my first function in Python'
In [48]: def square_no(a): #a is called as arguement
In [29]: def func(): return a*a
return "this is my first function", 2, 4, 3+7j, True
In [52]: square_no(60000)
In [30]: func()
Out[52]: 3600000000
Out[30]: ('this is my first function', 2, 4, (3+7j), True)
In [53]: def sum_(a, b):
In [31]: a = func() return a+b
Out[73]: 5 ---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
In [74]: sum_2(3, 5) Cell In[89], line 1
----> 1 sum_(2, 3, 4)
Out[74]: 8
TypeError: sum_() takes 2 positional arguments but 3 were given
In [98]: test1(1, 2.3, 4, 4.5, "Ajay", [1,2,3], (12,3), True, a = "ram") In [118… #Python namespace
def greet():
Out[98]: ((1, 2.3, 4, 4.5, 'Ajay', [1, 2, 3], (12, 3), True), 'ram') message = "hello, welcome" #message is a local variable
print(message)
In [99]: def return_list(*args):
l = [] In [119… greet()
for i in args:
if type(i) == list: hello, welcome
l.append(i)
In [120… print(message)
return l
---------------------------------------------------------------------------
In [102… return_list(1, 2.3, 4, 4.5, "Ajay", [1,2,3],[4, 5, 6, 7], (12,3), True) NameError Traceback (most recent call last)
Cell In[120], line 1
Out[102… [[1, 2, 3], [4, 5, 6, 7]] ----> 1 print(message)
In [122… greet()
In [105… #it should accept any no of arguement
#treated as key value pair hello, welcome to the course.
Out[111… {'a': 2, 'b': 3, 'c': 6, 'd': [1, 23], 'e': (12, 3, 4), 'f': [2, 3, 4]} ---------------------------------------------------------------------------
NameError Traceback (most recent call last)
In [112… #write a function which takes variable key word argument and return only the values which is a list Cell In[124], line 1
----> 1 print(message)
In [113… def test(**kwargs):
NameError: name 'message' is not defined
for i in kwargs.keys():
if type(kwargs[i]) ==list:
In [131… #function inside function
return i, kwargs[i]
In [ ]:
In [133… #calling from outside
def total_marks(marks_list):
return sum(marks_list)
def marks_in_subjects(**kwargs):
marks_list = []
for subject, marks in kwargs.items():
marks_list.append(marks)
return total_marks(marks_list)
Out[134… 106
In [138… find_power(2, 3)
Out[138… 8
In [139… help(find_power)
find_power(num, power)
This function returns the power of no
Args:
num(int): insert any integer value
power(int): insert any integer value
Returns:
integer: power of number
In [141… print(find_power.__doc__)
In [1]: #lambda >> anonymous function In [18]: fact_num(5)
#syntax: lambda arguements: expression
Out[18]: 120
In [2]: def square(x):
return x**2 In [19]: #map>> executes a specified function for each of item of an iterable
#syntax > map(func, *iterables)
In [3]: square(2)
In [20]: help(map)
Out[3]: 4
Help on class map in module builtins:
In [21]: l = [1, 2, 3, 4, 5]
In [14]: #factorial of 5 = 5*4*3*2*1 #factorial of 0 = 1 #!5 means factorial 5
def square(l):
In [15]: def fact_num(n): sq =[]
if n==0: for i in l:
return 1 sq.append(i**2)
else: return sq
return n*fact_num(n-1)
In [22]: square(l)
In [16]: fact_num(5)
Out[22]: [1, 4, 9, 16, 25]
Out[16]: 120
In [23]: def sq(x):
In [17]: fact_num = lambda n: 1 if n==0 else n*fact_num(n-1) return x**2
In [24]: l In [35]: words = ["python", "Ajay"]
list(map(str.capitalize, words))
Out[24]: [1, 2, 3, 4, 5]
Out[35]: ['Python', 'Ajay']
In [26]: list(map(sq, l))
In [38]: grades = ["A", "B", "C", "D"]
Out[26]: [1, 4, 9, 16, 25] list(map(lambda grade: 4 if grade == "A" else (3 if grade == "B" else 2), grades
Out[31]: [1, 4, 9, 16, 25] TypeError: <lambda>() missing 1 required positional argument: 'z'
Out[34]: ['P', 'W', 'S', 'K', 'I', 'L', 'L', 'S'] Out[45]: 1
In [46]: l list(filter(lambda x: len(x) >3, s))
Out[50]: 10000
In [52]: factorial(5)
Out[52]: 120
In [53]: #filter function >> is used to filter elements from an iterable based on some certain condition
#syntax >> filter(function, iterable)
In [54]: l
Out[54]: [2, 1, 3, 4, 5, 6]
Out[56]: [2, 4, 6]
Out[57]: [1, 3, 5]
Out[60]: [4, 5, 6]
Out[17]: 's'
In [5]: s = "pwskills"
Out[20]: 'l'
In [21]: next(a)
Out[21]: 'l' In [35]: next(a)
Out[35]: 'j'
In [22]: next(a)
Out[36]: 'a'
In [24]: for i in s:
print(i)
In [37]: next(a)
p
w Out[37]: 'y'
s
k In [38]: next(a)
i
l ---------------------------------------------------------------------------
l StopIteration Traceback (most recent call last)
s Cell In[38], line 1
----> 1 next(a)
In [26]: while True:
try: StopIteration:
s = iter(s)
item = next(s) In [39]: a = iter([1, 2, 3, 4])
print(item) next(a)
except StopIteration:
break Out[39]: 1
p
w In [40]: next(a)
s
k Out[40]: 2
i
l In [41]: next(a)
l
s Out[41]: 3
<class 'int'>
In [91]: next(f)
In [4]: s = "pwskills"
Out[91]: 0 print(type(s))
In [5]: a =6
Out[92]: 1
a =8
a =10
In [93]: next(f)
In [7]: print("Ajay")
Out[93]: 1
Ajay
In [94]: next(f)
In [8]: print("Aj")
Out[94]: 2
Aj
car is accelerting
Out[99]: 21
In [28]: #self > a variable that represents instance of the class using which you can access st
In [ ]:
In [30]: # def f1():
# pass
# def b2():
# pass
# def d1():
# pass
In [45]: ajay.deposit(1000)
In [56]: b
I am trying to deposit some money
Out[56]: 3
In [46]: ajay.amount
In [76]: class Listops:
Out[46]: 1000
def __init__(self, l):
In [47]: a =3 self.l = l
a= 3
a =8 def extract_even(self):
a=10 l = self.l
l1 = []
for i in l: In [90]: obj1.l
if i % 2 ==0:
l1.append(i) Out[90]: [1, 2, 3, 4, 5]
return l1
In [91]: class Book:
def extract_odd(self):
l=self.l def __init__(self, name, author, title):
l1 = [] self.name_of_book = name
for i in l: self.book_author = author
if i % 2 !=0: self.title_name = title
l1.append(i)
return l1 def extract_details_name_title(self):
print(self.name_of_book, self.title_name)
In [77]: obj1 = Listops([1, 2, 3, 4, 5])
def extract_details_name_author(self):
In [79]: obj1.l print(self.name_of_book, self.book_author)
Out[79]: [1, 2, 3, 4, 5]
In [92]: student1 = Book("ML", "MURPHY", "Linear Regression")
In [74]: obj1.l
In [93]: student1.extract_details_name_title()
Out[74]: [1, 2, 3, 4, 5]
ML Linear Regression
In [95]: student2.extract_details_name_author()
Out[75]: [8, 4, 10]
DL abc
In [88]: class Listops:
In [97]: student2.name_of_book
def __init__(self, l):
self.l = l Out[97]: 'DL'
def extract_details_name_title(self):
print(self.name_of_book, self.title_name)
def extract_details_name_author(self):
print(self.name_of_book, self.book_author)
ML MURPHY
In [ ]:
In [7]: child_obj.job()
In [8]: child_obj.father_property()
In [11]: father_obj.father_property()
In [12]: #parent class will not have access to the property of child class but child class will
In [15]: obj.apple_info()
In [16]: obj.fruit_info()
I am your father and having property of mine and your grandfather as well
In [28]: son.prop_grand_father()
I am your grandfather
In [51]: d.method()
method of class c
In [72]: ds = DataScience()
In [73]: ds.student_details()
In [74]: wb = WebDev()
In [75]: wb.student_marks()
class Rectange(Shape):
def calculate_area(self):
return "Area of rectangle : length * breadth"
class Circle(Shape):
def calculate_area(self):
return "Area of circle : pi r**2"
In [10]: #Polymorphism >> poly means many, morphism means forms/states In [20]: class Student:
#refers to an object taking several forms depending on data def student(self):
print("Welcome to PWskills class")
In [2]: len("Ajay") def student(self, name = ""):
print("Welcome to PWskills class", name)
Out[2]: 4 def student(self, name = "", course = ""):
print("Welcome to PWskills class", name, course)
Out[8]: 'pwskills' In [25]: #method overloading>> student is taking different forms, the last method overwrites th
class Student:
In [9]: func([1, 2, 3], [4, 5, 6])
def student(self, name = "", course = ""):
print("Welcome to PWskills class", name, course)
Out[9]: [1, 2, 3, 4, 5, 6]
stud = Student()
In [11]: class teacher_lecture:
def lec_info(self):
In [26]: stud.student()
print("This is lec info with teacher perspective")
Welcome to PWskills class
In [13]: class student_lecture:
def lec_info(self): In [27]: stud.student("Ajay")
print("This is lec info with student perspective")
Welcome to PWskills class Ajay
In [29]: #Method Overriding >> method in parent class and child class with same signature, the
In [15]: obj1 = teacher_lecture()
obj2 = student_lecture()
In [30]: class Animal:
def sound(self):
In [16]: class_obj = [obj1, obj2]
print("Animal sound")
class Cat(Animal):
In [17]: parcer(class_obj) def sound(self):
print("Cat meows")
This is lec info with teacher perspective
This is lec info with student perspective
In [31]: cat = Cat()
In [18]: #polymorphism >> Method overloading, Method Overriding
In [32]: cat.sound()
Cat meows ---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
In [33]: #Encapsulation>> means hiding something Cell In[70], line 1
#Bundling of data and methods ----> 1 stud.__degree
#accsess modifier>> public, protected, private
AttributeError: 'Student' object has no attribute '__degree'
In [34]: #Public >> accessible from anywhere from outside/inside the class
In [ ]: stud._Student__degree
class Student:
def __init__(self, name, degree): In [71]: stud.show()
self.name = name
self.degree = degree name Ramesh degree Masters
In [67]: #private >> within the class only its accessible, use two __ to make private Out[78]: '80'
Out[83]: 0
In [68]: stud = Student("Ramesh", "Masters")
In [84]: car.set_speed(100)
In [69]: stud.name
In [85]: car.get_speed()
Out[69]: 'Ramesh'
Out[85]: 100
In [70]: stud.__degree
In [100… stud.show()
def withdraw(self, amount):
if self.__balance >= amount: name Ajay college PWskills
self.__balance = self.__balance - amount
return True In [101… coll = College()
else:
return False
In [102… coll._college_name
def get_balance(self):
Out[102… 'PWskills'
return self.__balance
def show(self):
In [91]: acc.get_balance()
print("name", self.name, "college", self._college_name)
Out[91]: 11000
In [106… stud = Student("Ajay")
stud.show()
In [92]: acc.withdraw(10500)
name Ajay college PWskills
Out[92]: True
In [ ]:
In [93]: acc.get_balance()
Out[93]: 500
In [94]: acc.withdraw(1000)
Out[94]: False
In [98]: #protected>> within the class and its sub class protected member can be accessed, (_)
class College:
def __init__(self):
self._college_name = "PWskills"
class Student(College):
def __init__(self, name):
self.name = name
College.__init__(self)
def show(self):
In [6]: class Student: In [26]: stud.price_set = 1500
def __init__(self, name, price):
self.__name = name In [27]: stud.access_price
self.__price = price
Out[27]: 1500
In [7]: stud = Student("Ajay", 3000)
In [28]: class Student:
In [8]: stud.name #because its a private variable def __init__(self, name, price):
self.__name = name
--------------------------------------------------------------------------- self.__price = price
AttributeError Traceback (most recent call last)
Cell In[8], line 1 @property #it will make accesS_price as attribute of the class
----> 1 stud.name def access_price(self):
return self.__price
AttributeError: 'Student' object has no attribute 'name'
@access_price.setter
In [18]: class Student: def price_set(self, price_new):
def __init__(self, name, price): self.__price = price_new
self.__name = name
self.__price = price @access_price.deleter
def del_price(self):
@property #it will make accesS_price as attribute of the class del self.__price
def access_price(self):
return self.__price
In [29]: stud = Student("Ajay", 1000)
Out[20]: 2000
In [32]: stud.price_set = 2000
@property #it will make accesS_price as attribute of the class In [34]: del stud.del_price
def access_price(self):
return self.__price In [36]: stud.access_price
@access_price.setter ---------------------------------------------------------------------------
def price_set(self, price_new): AttributeError Traceback (most recent call last)
self.__price = price_new Cell In[36], line 1
----> 1 stud.access_price
In [41]: c.radius
Out[41]: 5
In [44]: c = Circle(5)
In [45]: c.radius
Out[45]: 5
In [46]: c.radius = 10
In [47]: c.radius
Out[47]: 10
In [49]: c.radius = 10
In [50]: c.area()
Out[50]: 314.0
In [ ]: class Circle:
def __init__(self, radius):
self.__radius = radius
@property
def radius(self):
return self.__radius
In [1]: #dunder/special/magic Out[4]: ['__add__',
'__class__',
In [2]: a = "PW" '__contains__',
b = "Skills" '__delattr__',
a + b '__dir__',
'__doc__',
Out[2]: 'PWSkills' '__eq__',
'__format__',
In [3]: a.__add__(b) '__ge__',
'__getattribute__',
Out[3]: 'PWSkills' '__getitem__',
'__getnewargs__',
'__gt__',
In [4]: dir(str)
'__hash__',
'__init__',
'__init_subclass__',
'__iter__',
'__le__',
'__len__',
'__lt__',
'__mod__',
'__mul__',
'__ne__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__rmod__',
'__rmul__',
'__setattr__',
'__sizeof__',
'__str__',
'__subclasshook__',
'capitalize',
'casefold',
'center',
'count',
'encode',
'endswith',
'expandtabs',
'find',
'format',
'format_map',
'index',
'isalnum',
'isalpha',
'isascii',
'isdecimal',
'isdigit',
'isidentifier',
'islower',
'isnumeric',
'isprintable',
'isspace',
'istitle', Out[8]: ['__abs__',
'isupper', '__add__',
'join', '__and__',
'ljust', '__bool__',
'lower', '__ceil__',
'lstrip', '__class__',
'maketrans', '__delattr__',
'partition', '__dir__',
'removeprefix', '__divmod__',
'removesuffix', '__doc__',
'replace', '__eq__',
'rfind', '__float__',
'rindex', '__floor__',
'rjust', '__floordiv__',
'rpartition', '__format__',
'rsplit', '__ge__',
'rstrip', '__getattribute__',
'split', '__getnewargs__',
'splitlines', '__gt__',
'startswith', '__hash__',
'strip', '__index__',
'swapcase', '__init__',
'title', '__init_subclass__',
'translate', '__int__',
'upper', '__invert__',
'zfill'] '__le__',
'__lshift__',
In [5]: 3+5 '__lt__',
'__mod__',
Out[5]: 8 '__mul__',
'__ne__',
In [6]: a = 3 '__neg__',
b = 5 '__new__',
a+b '__or__',
'__pos__',
Out[6]: 8 '__pow__',
'__radd__',
'__rand__',
In [7]: a.__add__(b)
'__rdivmod__',
'__reduce__',
Out[7]: 8
'__reduce_ex__',
'__repr__',
In [8]: dir(int) '__rfloordiv__',
'__rlshift__',
'__rmod__',
'__rmul__',
'__ror__',
'__round__',
'__rpow__',
'__rrshift__',
'__rshift__',
'__rsub__',
'__rtruediv__',
'__rxor__',
'__setattr__', In [19]: print(Student())
'__sizeof__', This method overrides the print statement of object method
'__str__',
'__sub__', In [21]: #repr
'__subclasshook__',
'__truediv__', class MyClass:
'__trunc__', def __init__(self, x):
'__xor__', self.x = x
'as_integer_ratio', def __repr__(self):
'bit_count', return f"MyClass({self.x})"
'bit_length',
'conjugate',
In [22]: obj = MyClass(2)
'denominator',
'from_bytes',
'imag', In [23]: print(repr(obj)) #retrun the string representation of object
'numerator',
MyClass(2)
'real',
'to_bytes']
In [24]: True == True
In [9]: class Student:
Out[24]: True
def __init__(self):
print("this is the first thing it will be executed once you make instance/object of the class"
In [26]: 3 == 3
In [11]: obj = Student()
Out[26]: True
this is the first thing it will be executed once you make instance/object of th
e class In [27]: a = 3
b = 3
In [12]: class Student: a.__eq__(b)
def __new__(cls):
print("This will be executed even before init") Out[27]: True
def __init__(self):
print("this is the first thing it will be executed once you make instance/object of the class"
In [28]: class Point:
def __init__(self, x, y):
In [13]: obj = Student() self.x = x
self.y = y
This will be executed even before init
In [8]: say_hello()
In [27]: a.add(2, 3) #This was regular class
The lines being printed before the func.
hello Out[27]: 5
The lines being printed after the func.
In [28]: class Math:
In [12]: import time @staticmethod
def timer_decorator(func): def add(x, y):
def timer(): return x+y
start = time.time()
func()
In [29]: Math.add(2,3)
end = time.time()
print(end-start)
Out[29]: 5
return timer
In [35]: c = Circle(5)
In [4]: obj.name
In [9]: obj1.name
Out[9]: 'Ajay'
In [12]: stud.name
Out[12]: 'Sanjay'
total_students = 0
def __init__(self, name): #instance
self.name = name
Student.total_students = Student.total_students + 1
Out[17]: 0 ---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
In [18]: std1 = Student("Ajay") Cell In[32], line 1
----> 1 Student.get_total_students()
In [19]: std2 = Student("Bijay") AttributeError: type object 'Student' has no attribute 'get_total_students'
In [20]: Student.get_total_students() In [33]: #static method >> don't have access or modify class, work with the
In [23]: #To make external class method to be part of the class @staticmethod
class Student: def add(x, y):
return x+y
total_students = 0
def __init__(self, name): #instance @staticmethod
self.name = name def subtract(x, y):
Student.total_students = Student.total_students + 1 return x-y
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[28], line 1
----> 1 Student.course_details("Data Science")
In [30]: Student.get_total_students()
Out[30]: 0
In [57]: os.listdir() In [ ]:
Out[57]: ['test',
'file.txt',
'.ipynb_checkpoints',
'1',
'sample-code.ipynb',
'.git',
'File_handling_reading_and_writing.ipynb',
'README.md']
In [58]: os.mkdir("feat")
In [1]: for i in range(10): In [1]: import logging
print("The current no is", i) logging.basicConfig(filename = "test_new1.log" , level = logging.DEBUG ,format
The current no is 0
The current no is 1 In [2]: logging.debug("debugging message")
The current no is 2
The current no is 3 In [3]: logging.info("info message")
The current no is 4
The current no is 5 In [4]: logging.shutdown()
The current no is 6
The current no is 7
In [1]: import logging
The current no is 8
logging.basicConfig(filename = "program.log" , level = logging.DEBUG ,format =
The current no is 9
In [3]: #logging>> keep track of In [2]: l = [1, "hello", [2, "world"], 3, ["python", 4]]
#logging module keep track of events in a software run
#write a prog to separate the integer and string in two list separfately
import logging
In [4]: l1_int = []
#logging supports different levels of logging which helps to catrgorize messages based on their severity l2_str = []
In [5]: logging.shutdown()
In [1]: text_content = """ data = {"name": "Ajay",
example 1 "Course": "DA",
example 2 "fee": 2900}
example 3
""" In [12]: type(data)
Out[5]: [['Name', 'course', 'Fee'], ['Ajay', 'DS', '1000'], ['Bijay', 'ML', '2000']] In [19]: #Buffered reading>> it allows the program to read the data in chunks
#BUFFERED writing
In [6]: for i in data:
print(i) In [20]: import io
with open("test_buf.txt", "wb") as f:
['Name', 'course', 'Fee'] file = io.BufferedWriter(f)
['Ajay', 'DS', '1000'] file.write(b"A computer is a machine that can be programmed to automatically carry
['Bijay', 'ML', '2000'] file.write(b"hello")
file.write(b"I am good")
In [8]: import csv file.flush()
In [9]: with open("example_csv.csv", "r") as f: b'A computer is a machine that can be programmed to automatically carry out seq
r = csv.reader(f) uences of arithmetic or'
for i in r:
print(i) In [ ]:
In [12]: try:
In [2]: 10/0 import pwskills
except ModuleNotFoundError as e:
--------------------------------------------------------------------------- print("the module not found", e)
ZeroDivisionError Traceback (most recent call last)
Cell In[2], line 1 the module not found No module named 'pwskills'
----> 1 10/0
In [13]: test()
ZeroDivisionError: division by zero
---------------------------------------------------------------------------
In [3]: try: NameError Traceback (most recent call last)
10/0 Cell In[13], line 1
except Exception as e: ----> 1 test()
print(e)
NameError: name 'test' is not defined
division by zero
In [14]: try:
In [5]: try: test()
10/0 except NameError as e:
except ZeroDivisionError as e: print(e)
print("Here I am handling the zero diviosion error", e)
name 'test' is not defined
Here I am handling the zero diviosion error division by zero
In [15]: d = {"name": "Ajay", "class":"ds"}
In [6]: # this is not best practice, always go for specific exception
# try:
In [16]: d["name"]
# 10/0
# except:
Out[16]: 'Ajay'
# print("This is some error")
ValueError: invalid literal for int() with base 10: 'pwskills' In [19]: try:
d = {"name": "Ajay", "class":"ds"}
In [8]: try: d["fee"]
int("pwskills") except KeyError as e:
except ValueError as e: print("The key is not found", e)
print("The string can not be converted to integer", e)
The key is not found 'fee'
The string can not be converted to integer invalid literal for int() with ba
se 10: 'pwskills' In [22]: try:
l = [1, 2, 3]
In [9]: try: l[4]
int("pwskills") except IndexError as e:
except (ValueError, TypeError) as e: print(e)
print("The string can not be converted to integer", e)
list index out of range except FileNotFoundError as e:
print("My file not found", e)
In [25]: try:
f = open("example1.txt", "r") My file not found [Errno 2] No such file or directory: 'example1.txt'
f.read()
except FileNotFoundError as e: In [7]: #give proper documentation, proiper comments
print("My file not found,", e)
"""
My file not found, [Errno 2] No such file or directory: 'example1.txt' This is my documentation
"""
In [26]: try:
f = open("example1.txt", "r") Out[7]: '\nThis is my documentation\n'
f.read()
except Exception as e: In [ ]: #always cleanup all the resources
print("first msg", e)
except FileNotFoundError as e: This notebook was converted with convert.ploomber.io
print("My file not found", e)
In [4]: test("example1.txt")
try:
f = open("example1.txt", "r")
f.read()
except ZeroDivisionError as e:
print("first msg", e)
do something
sleep for 1 sec
In [1]: ## A process is an instance of a computer program done with sleeping
#executable program, data, execution context do something
#processor parallely>> multiprocessing sleep for 1 sec
#muultiple threads in processor/core >> multi threading done with sleeping
do something
In [4]: import time sleep for 1 sec
start = time.perf_counter() done with sleeping
do something
def test_func(): sleep for 1 sec
print("do something") done with sleeping
print("sleep for 1 sec") The program finished in 4.01 seconds.
time.sleep(1)
print("done with sleeping") In [11]: import time
import threading
test_func() start = time.perf_counter()
end = time.perf_counter()
def test_func():
print("do something")
print(f"The program finished in {round(end-start, 2)} seconds.") print("sleep for 1 sec")
time.sleep(1)
do something print("done with sleeping")
sleep for 1 sec
done with sleeping t1 = threading.Thread(target = test_func)
The program finished in 1.0 seconds. t2 = threading.Thread(target = test_func)
def test_func(): t1.join() #in order to first execute the threads and then the execition of m
print("do something") t2.join()
print("sleep for 1 sec")
time.sleep(1)
print("done with sleeping") end = time.perf_counter()
test_func()
test_func() print(f"The program finished in {round(end-start, 2)} seconds.")
test_func()
test_func() do something
end = time.perf_counter() sleep for 1 sec
do something
sleep for 1 sec
print(f"The program finished in {round(end-start, 2)} seconds.") done with sleepingdone with sleeping
def test_func():
print("do something")
print("sleep for 1 sec")
time.sleep(1)
print("done with sleeping")
time.sleep(args)
threads = [] print("done with sleeping")
for i in range(10):
t = threading.Thread(target = test_func) threads = []
t.start() for i in range(10):
threads.append(t) t = threading.Thread(target = test_func, args = [2])
t.start()
for thread in threads: threads.append(t)
thread.join()
for thread in threads:
thread.join()
end = time.perf_counter()
end = time.perf_counter()
print(f"The program finished in {round(end-start, 2)} seconds.")
url_list = [
'https://raw.githubusercontent.com/dscape/spell/master/test/resources/bi print(f"The program finished in {round(end-start, 2)} seconds.")
'https://raw.githubusercontent.com/first20hours/google-10000-english/mas The program finished in 0.3 seconds.
'https://raw.githubusercontent.com/itsfoss/text-files/master/sherlock.tx
'https://raw.githubusercontent.com/itsfoss/text-files/master/sample_log_ In [19]: #shared varaible across all the threads
]
start = time.perf_counter()
shared_counter = 0
data_list = ['data1.txt', 'data2.txt', 'data3.txt', 'data4.txt'] counter_lock = threading.Lock()
The program finished in 0.8 seconds. print(f"The program finished in {round(end-start, 2)} seconds.")
def increment_shared_counter(x):
data_list = ['data1.txt', 'data2.txt', 'data3.txt', 'data4.txt'] global shared_counter
with counter_lock:
import urllib.request shared_counter = shared_counter+1
def file_download(url, filename): print(f"Thread {x}: incremented shared counter to {shared_counter}")
urllib.request.urlretrieve(url, filename) time.sleep(1)
with concurrent.futures.ThreadPoolExecutor() as executor:
thread_args =[1, 2, 3, 4, 5, 6] In [2]: import time
executor.map(increment_shared_counter, thread_args) start = time.perf_counter()
def test_func():
end = time.perf_counter() print("do something")
print("sleep for 1 sec")
time.sleep(1)
print(f"The program finished in {round(end-start, 2)} seconds.") print("done with sleeping")
import time
start = time.perf_counter()
def test_func():
print("do something")
print("sleep for 1 sec")
time.sleep(1)
print("done with sleeping")
p1 = multiprocessing.Process(target = test_func)
p2 = multiprocessing.Process(target = test_func)
p1.start()
p2.start()
p1.join()
p2.join()
end = time.perf_counter()
def test_func():
print("do something") do something
print("sleep for 1 sec") sleep for 1 sec
time.sleep(1) do something
print("done with sleeping") sleep for 1 sec
do something
processes = [] sleep for 1 sec
for i in range(10): done with sleeping
p=multiprocessing.Process(target = test_func) done with sleeping
p.start() done with sleeping
processes.append(p) done with sleeping
for process in processes: done with sleeping
process.join() done with sleeping
done with sleeping
end = time.perf_counter() done with sleeping
done with sleeping
done with sleeping
print(f"The program finished in {round(end-start, 2)} seconds.") The program finished in 1.08 seconds.
import time
start = time.perf_counter()
processes = []
for i in range(5):
p=multiprocessing.Process(target = square, args = (i, arr))
p.start()
processes.append(p)
for process in processes:
process.join()
print(list(arr))
end = time.perf_counter()
def enroll_students(student_queue):
for student in ["Ajay", "Bijay", "Sanjay", "Rizwan"]:
student_queue.put(f"enroll {student}")
def register_students(student_queue):
while True:
enrollment_req = student_queue.get()
if enrollment_req is None:
break
print(f"Registrar: {enrollment_req}")
if True:
student_queue = multiprocessing.Queue()
enrollment_process = multiprocessing.Process(target = enroll_students, a
reg_process = multiprocessing.Process(target = register_students, args =
enrollment_process.start()
reg_process.start()
enrollment_process.join()
reg_process.join()
--------------------------------------------------------------------------- do somethingdo somethingdo somethingdo somethingdo somethingdo somethingdo s
KeyboardInterrupt Traceback (most recent call last) omethingdo somethingdo somethingdo something
Cell In[1], line 24
20 reg_process.start()
23 enrollment_process.join()
---> 24 reg_process.join()
def test_func(i):
print("do something")
print("sleep for a sec")
time.sleep(1)
print("done sleeping")
start = time.perf_counter()
end = time.perf_counter()
The docstring examples assume that `numpy` has been imported as `np`::
>>> x = 42
>>> x = x + 1
>>> help(np.sort)
... # doctest: +SKIP
>>> np.lookfor('keyword')
... # doctest: +SKIP
Available subpackages
---------------------
lib
Basic functions used by several sub-packages. Out[8]: list
random
Core Random Tools
linalg In [9]: lis
Core Linear Algebra Tools
fft Out[9]: [1, 2, 3, 'pwskills', (3+5j), True, 1.2]
Core FFT routines
polynomial In [10]: #Numpy stores the data in an array
Polynomial tools #An array is a container used to store the data of same data type
testing
NumPy testing tools In [11]: #why numpy
distutils #Computation is fast>> due to array>>>continous memory location
Enhancements to distutils with support for #mathematical computation
Fortran compilers support and more.
In [12]: l = [1, 2, 3, 4, 5]
Utilities
---------
test In [13]: arr = np.array(l)
Run numpy unittests
show_config In [14]: arr
Show numpy build configuration
dual Out[14]: array([1, 2, 3, 4, 5])
Overwrite certain functions with high-performance SciPy tools.
Note: `numpy.dual` is deprecated. Use the functions from NumPy or Scipy
In [15]: l = [1, 2, 3, 4, 5.1]
directly instead of importing them from `numpy.dual`.
arr = np.array(l)
matlib
Make everything matrices.
__version__ In [16]: arr
NumPy version string
Out[16]: array([1. , 2. , 3. , 4. , 5.1])
Viewing documentation using IPython
----------------------------------- In [17]: l = [1, 2, 3, 4, 5.1, "Ajay"]
Start IPython with the NumPy profile (``ipython -p numpy``), which will arr = np.array(l)
import `numpy` under the alias `np`. Then, use the ``cpaste`` command to
paste examples into the shell. To see which functions are available in In [18]: arr
`numpy`, type ``np.<TAB>`` (where ``<TAB>`` refers to the TAB key), or use
``np.*cos*?<ENTER>`` (where ``<ENTER>`` refers to the ENTER key) to narrow Out[18]: array(['1', '2', '3', '4', '5.1', 'Ajay'], dtype='<U32')
down the list. To view the docstring for a function, use
``np.cos?<ENTER>`` (to view the docstring) and ``np.cos??<ENTER>`` (to view
In [19]: #arr stores homogenous data
the source code).
In [23]: arr1
In [8]: type(lis)
Out[23]: array([[1, 2, 3], In [35]: np.asanyarray(mat) #Convert the input to an ndarray, but pass ndarray subclasses throu
[4, 5, 6]])
Out[35]: matrix([[1, 2, 3, 4]])
In [25]: arr1.ndim
In [36]: t = ([1, 2, 3], [4, 5, 6]) #tuple to array
Out[25]: 2
In [37]: type(t)
In [27]: mat = np.matrix([1, 2, 3, 4]) #matrix is a specialised array, 2 dimensional
Out[37]: tuple
In [28]: type(mat)
In [38]: np.array(t)
Out[28]: numpy.matrix
Out[38]: array([[1, 2, 3],
In [29]: np.matrix([[[1, 2], [3,4], [5,6]]]) [4, 5, 6]])
In [47]: a
In [31]: mat
Out[47]: array([500, 2, 3, 4, 5])
Out[31]: matrix([[1, 2, 3, 4]])
In [50]: a = arr.copy()
Out[33]: array([1, 2, 3])
In [51]: a
In [34]: mat
In [72]: np.fromiter(iterable, int) #Create a new 1-dimensional array from an iterable object.
In [ ]: #multiple ways to generate array
Out[72]: array([0, 1, 2, 3, 4])
In [58]: arr1 = np.fromfunction(lambda i, j : i==j, (3,3)) #Construct an array by executing a function over each coordinate.
In [73]: np.fromstring('22 23 24', sep = " ") #values should be numerical
In [59]: arr1
Out[73]: array([22., 23., 24.])
Out[59]: array([[ True, False, False],
[False, True, False], In [78]: np.fromstring('22,23,24', sep = ",")
[False, False, True]])
Out[78]: array([22., 23., 24.])
In [60]: arr1.ndim
Out[65]: array([[0., 0., 0.], In [82]: #other methods of numpy>> to generate sequnce of nos
[0., 1., 2.], np.arange(1, 10, 0.1) #Return evenly spaced values within a given interval.
[0., 2., 4.]])
Out[83]: array([1. , 1.44444444, 1.88888889, 2.33333333, 2.77777778, In [4]: arr1.flatten() #Return a copy of the array collapsed into one dimension.
3.22222222, 3.66666667, 4.11111111, 4.55555556, 5. ])
Out[4]: array([2, 1, 2, 1, 2, 2, 1, 1, 2])
In [ ]:
In [6]: arr.ndim
Out[6]: 1
Out[10]: array([[1],
[2],
[3],
[4]])
Out[12]: 2
In [11]: arr
In [15]: arr.ndim
Out[15]: 2
Out[21]: 2 In [31]: a
In [22]: np.squeeze(a) #Remove axes of length one from `a` Out[31]: array([[1],
[2],
Out[22]: array([1, 2, 3]) [3]])
import numpy.matlib as nm In [109… a = np.array([[7, 5.3, -3], [3, -5, 2], [5, 3, -7]])
Out[95]: matrix([[0., 0., 0., 0., 0.]]) Out[110… array([[ 7. , 5.3, -3. ],
[ 3. , -5. , 2. ],
In [97]: nm.ones((3, 4)) [ 5. , 3. , -7. ]])
In [ ]:
In [1]: import numpy as np In [12]: arr.ndim
In [2]: a = np.zeros(5) #Return a new array of given shape and type, filled with zeros. Out[12]: 2
In [4]: a.ndim
In [14]: np.zeros((2, 3))
Out[4]: 1
Out[14]: array([[0., 0., 0.],
[0., 0., 0.]])
In [5]: b = np.zeros((3, 4))
In [15]: arr = np.zeros((3, 4))
In [6]: b.ndim arr.ndim
Out[6]: 2 Out[15]: 2
In [11]: arr
In [21]: arr.ndim
Out[22]: array([[[[[0., 0., 0., 0.], Out[33]: array([[4., 4., 4., 4.],
[0., 0., 0., 0.], [4., 4., 4., 4.],
[0., 0., 0., 0.]]]]]) [4., 4., 4., 4.]])
In [25]: arr.shape
In [35]: random.choice('Ajay')
Out[25]: (3, 4)
Out[35]: 'a'
In [26]: arr
In [36]: random.randrange(1, 10)
Out[26]: array([[1., 1., 1., 1.],
[1., 1., 1., 1.], Out[36]: 7
[1., 1., 1., 1.]])
In [37]: random.random() # a random no is generated between 0(included) and 1 (excluded)
In [27]: arr + 5
Out[37]: 0.017769477320319083
Out[27]: array([[6., 6., 6., 6.],
[6., 6., 6., 6.], In [38]: lis = [1, 2, 3, 4, 5]
[6., 6., 6., 6.]]) lis
In [28]: a = arr * 5
Out[38]: [1, 2, 3, 4, 5]
In [29]: a
In [40]: random.shuffle(lis)
Out[62]: 12
In [48]: np.random.rand(2, 3)
Out[48]: array([[0.19956744, 0.85663567, 0.76798966], In [63]: #array size will not change, always resape parameter should be multiplication of origi
[0.01678481, 0.29287439, 0.5507118 ]])
In [64]: arr
In [49]: np.random.randn(2,3)
Out[64]: array([[1, 3, 3, 4],
Out[49]: array([[-0.85502332, -0.06881989, 0.76879661], [4, 3, 3, 3],
[ 0.64187717, 1.46547139, -1.07173591]]) [4, 4, 1, 4]])
arr1 = arr.reshape(2, 6)
In [72]: arr.reshape(3, 3)
In [61]: arr
Out[73]: array([[1, 3, 3, 4],
[4, 3, 3, 3],
[4, 4, 1, 4]])
In [74]: #case when you dont know in reshape what should be the column count In [86]: arr.reshape(1, 2, 2, 3)
In [85]: arr.reshape(2, 2, 3)
In [92]: arr1.ndim
In [98]: #To access elements in the array In [112… arr1[0, [1, 3]]
arr1[0]
Out[112… array([8, 1])
Out[98]: array([3, 8, 8, 1, 1, 7])
In [113… arr1[2:4, [0, 3]]
In [99]: arr1[1]
Out[113… array([[2, 9],
Out[99]: array([4, 4, 5, 5, 2, 5]) [8, 1]])
In [142… a
In [143… #Transpose
a.T
Out[143… array([[1],
[2],
[3],
[4]])
In [145… arr1
In [146… arr1.T
In [ ]:
Out[4]: id location_id program_id accepted_payments alternate_name application_process id location_id program_id accepted_payments alternate_name application_proces
Walk in or apply by
0 1 1 NaN NaN NaN
Walk in. P
9 10 10 NaN NaN NaN California r
Walk in or apply by
5 6 6 NaN NaN NaN phone for Walk in or thr
14 15 15 NaN NaN NaN
membership appli... other agency r
Walk in or apply by
Call for scr 1 1 1 NaN NaN NaN
phone.
18 19 19 NaN NaN NaN appointment
(650-347-3648).
By phone during
20 21 21 NaN NaN NaN
business hours.
Phone for
information
3 3 3 NaN NaN NaN
(403-4300 Ext.
Cash, Check, Credit Fotos para Walk in or apply by 4322).
21 22 22 NaN
Card pasaportes phone or mail
23 rows × 22 columns
Phone for
5 5 5 NaN NaN NaN
information.
In [5]: df = pd.read_csv('services.csv', header = None) #internal integer indexes are provided by pandas
df
Walk in or apply by
phone for
6 6 6 NaN NaN NaN
membership
appli...
Apply by phone or
7 7 7 NaN NaN NaN be referred by a
doctor, soc...
residency in
Medical visits...
California requ...
By phone during
21 21 21 NaN NaN NaN
business hours.
Walk in or apply by
12 12 12 NaN NaN NaN phone, email or
webpage re...
Cash, Check, Credit Fotos para Walk in or apply by
22 22 22 NaN
Card pasaportes phone or mail
Call for
appointment.
14 14 14 NaN NaN NaN
Referral from
24 rows × 22 columns
human serv...
Call for
17 17 17 NaN NaN NaN
information.
Provides Provide
training and training an
Residents Residents
job placement job placemen
of San of San
Apply by to eligible Apply by to eligibl
Unnamed: Unnamed: Unnamed: Mateo Unnamed: Unnamed: Unnamed: Mateo
2 2.1 phone for an people age 55 2 2.1 phone for an people age 5
2 3 4 County 2 3 4 County
appointment. or over who appointment. or over wh
age 55 or age 55 or
meet certain meet certai
over over
qualifications. qualifications
Older speaking
Phone for
adults age supportive
information
0 3 3 NaN NaN NaN 55 or over counseling Walk in. Proof of Provides genera
(403-4300 Ext.
who can services to San California reading an
4322). 7 10 10 NaN NaN NaN NaN
benefit fr... residency to media materials
rece...
Parents,
children,
families supervised Walk in. Proof of Provides genera
1 4 4 NaN NaN NaN Apply by phone.
with visitation California
problems services and a ... 8 11 11 NaN NaN NaN NaN
residency materials
of c... require... including .
Low-
income Adults,
Provides fix Offers a
working Walk in or apply parents,
Phone for 8% short ter intergenerationa
2 5 5 NaN NaN NaN families 9 12 12 NaN NaN NaN by phone, email children in
information. literacy pr
with or webpage re... 1st-12th
eligible...
children grades i...
tran...
Older Individuals
Call for Provides food
Apply by phone adults who or families
Rosener House is appointment. clothing, bu
or be referred have 11 14 14 NaN NaN NaN with low or
4 7 7 NaN NaN NaN a day center for Referral from tok
by a doctor, memory or no income
older adults... human serv...
soc... sensory ...
loss, ...
Adult
Senior alcoholic/ Provides a long
Walk in or
citizens Delivers a hot drug ter
12 15 15 NaN NaN NaN through other
age 60 or meal to the addictive
5 8 8 NaN NaN NaN Apply by phone. agency referral.
over, home of persons men and residential .
disabled women w...
indiv...
21 rows × 22 columns
Walk in or apply by
0 1 1 NaN NaN NaN
Walk in. P
9 10 10 NaN NaN NaN California r
Walk in or apply by
5 6 6 NaN NaN NaN phone for Walk in or thr
14 15 15 NaN NaN NaN
membership appli... other agency r
In [17]: type(df['application_process'])
Out[17]: pandas.core.series.Series In [37]: type(d)
Out[37]: pandas.core.series.Series
In [18]: type(df)
Out[38]: 100
In [19]: l = [1, 2, 3, 4]
s = pd.Series(l)
s In [39]: d.index
Out[24]: 2 3
3 4 In [46]: df
dtype: int64
In [25]: s[2:]
Out[25]: 2 3
3 4
dtype: int64
In [36]: d
Walk in or apply by
0 1 1 NaN NaN NaN
Walk in. P
9 10 10 NaN NaN NaN California r
Walk in or apply by
5 6 6 NaN NaN NaN phone for Walk in or thr
14 15 15 NaN NaN NaN
membership appli... other agency r
In [51]: df.head(5)
Walk in or apply by
22 23 22 NaN NaN NaN
phone or mail
23 rows × 22 columns
In [47]: df.shape
In [48]: df.columns
In [49]: list(df.columns)
Out[51]: id location_id program_id accepted_payments alternate_name application_process Out[52]: id location_id program_id accepted_payments alternate_name application_process
2 rows × 22 columns
Phone for infor
2 3 3 NaN NaN NaN
(403-4300 Ext. 4322).
In [53]: df.tail(5)
Phone for
4 5 5 NaN NaN NaN
information. 19 20 20 NaN NaN NaN
In [52]: df.head(2)
Walk in or apply b
22 23 22 NaN NaN NaN
phone or ma
5 rows × 22 columns
In [55]: df.sample(1)
Out[55]: id location_id program_id accepted_payments alternate_name application_process Out[56]: id location_id program_id accepted_payments alternate_name application_proces
Walk in. P
Walk in or apply b
8 9 9 NaN NaN NaN residency in Califor 0 1 1 NaN NaN NaN
1 rows × 22 columns
Apply by phone for a
1 2 2 NaN NaN NaN
appointment
In [56]: df
Phone fo
4 5 5 NaN NaN NaN
infor
Walk in or apply b
5 6 6 NaN NaN NaN phone fo
membership appli..
Apply by phone or b
6 7 7 NaN NaN NaN referred by a doctor
Walk in. P
8 9 9 NaN NaN NaN residency in Califor
id location_id program_id accepted_payments alternate_name application_process id location_id program_id accepted_payments alternate_name application_proces
Walk in. P
Call for scr
9 10 10 NaN NaN NaN California r
18 19 19 NaN NaN NaN appointmen
(650-347-3648)
Walk in. P
10 11 11 NaN NaN NaN California r 19 20 20 NaN NaN NaN
r
By phone during
20 21 21 NaN NaN NaN
business hours
Walk in or apply by
11 12 12 NaN NaN NaN phone, email or
webpage r
Cash, Check, Credit Fotos para Walk in or apply b
21 22 22 NaN
Card pasaportes phone or ma
Walk in. P
12 13 13 NaN NaN NaN California r
r Walk in or apply b
22 23 22 NaN NaN NaN
phone or ma
In [57]: df.info()
Walk in or thr
14 15 15 NaN NaN NaN
other agency r
Walk in. W
15 16 16 NaN NaN NaN application,
identification r
In [63]: df['application_process']
Out[63]: 0 Walk in or apply by phone. Out[66]: list[0 Walk in or apply by phone.
1 Apply by phone for an appointment. 1 Apply by phone for an appointment.
2 Phone for information (403-4300 Ext. 4322). 2 Phone for information (403-4300 Ext. 4322).
3 Apply by phone. 3 Apply by phone.
4 Phone for information. 4 Phone for information.
5 Walk in or apply by phone for membership appli... 5 Walk in or apply by phone for membership appli...
6 Apply by phone or be referred by a doctor, soc... 6 Apply by phone or be referred by a doctor, soc...
7 Apply by phone. 7 Apply by phone.
8 Walk in. Proof of residency in California requ... 8 Walk in. Proof of residency in California requ...
9 Walk in. Proof of California residency to rece... 9 Walk in. Proof of California residency to rece...
10 Walk in. Proof of California residency require... 10 Walk in. Proof of California residency require...
11 Walk in or apply by phone, email or webpage re... 11 Walk in or apply by phone, email or webpage re...
12 Walk in. Proof of California residency require... 12 Walk in. Proof of California residency require...
13 Call for appointment. Referral from human serv... 13 Call for appointment. Referral from human serv...
14 Walk in or through other agency referral. 14 Walk in or through other agency referral.
15 Walk in. Written application, identification r... 15 Walk in. Written application, identification r...
16 Call for information. 16 Call for information.
17 Call for screening appointment. Medical visits... 17 Call for screening appointment. Medical visits...
18 Call for screening appointment (650-347-3648). 18 Call for screening appointment (650-347-3648).
19 Walk in. 19 Walk in.
20 By phone during business hours. 20 By phone during business hours.
21 Walk in or apply by phone or mail 21 Walk in or apply by phone or mail
22 Walk in or apply by phone or mail 22 Walk in or apply by phone or mail
Name: application_process, dtype: object Name: application_process, dtype: object]
In [69]: d = pd.DataFrame(s)
In [70]: d
Out[70]: 0
100 2
Ajay 3
2 4
In [82]: d
Out[82]: 0 new column new_column In [91]: df
2 4 Bijay Bijay
In [83]: d['new_column']
In [85]: d.new_column
In [88]: d
2 4 Bijay Bijay
In [89]: d.reset_index()
2 2 4 Bijay Bijay
In [90]: d.reset_index(drop=True)
0 2 Bijay Bijay
1 3 Bijay Bijay
2 4 Bijay Bijay
Out[91]: id location_id program_id accepted_payments alternate_name application_process id location_id program_id accepted_payments alternate_name application_proces
Walk in or apply by
0 1 1 NaN NaN NaN
Walk in. P
9 10 10 NaN NaN NaN California r
Walk in or apply by
5 6 6 NaN NaN NaN phone for Walk in or thr
14 15 15 NaN NaN NaN
membership appli... other agency r
Cash, Check, Credit Fotos para Walk in or apply by 10 Schaberg Branch NaN active
21 22 22 NaN
Card pasaportes phone or mail
11 Project Read NaN active
Out[92]: Index(['id', 'location_id', 'program_id', 'accepted_payments', 19 Service with blank fields NaN defunct
'alternate_name', 'application_process', 'audience', 'description',
'eligibility', 'email', 'fees', 'funding_sources', 20 Service for Admin Test Location NaN inactive
'interpretation_services', 'keywords', 'languages', 'name', 21 Passport Photos Spanish active
'required_documents', 'service_areas', 'status', 'wait_time', 'websit
e', 22 Example Service Name NaN active
'taxonomy_ids'],
dtype='object')
In [103… df1 = pd.read_excel("LUSID Excel - Setting up your market data.xlsx")
In [99]: df_subset = df[['name', 'languages', 'status']]
df_subset In [104… df1
Out[104… Unnamed: Unnamed: Unnamed: Unnamed: Unnamed: Unnamed: Unnamed: Unnamed: Unnamed: Unnamed: Unnamed: Unnamed:
Unnamed: 5 Unnamed: 5
0 1 2 3 4 0 1 2 3 4
0 NaN NaN NaN NaN NaN NaN 16 NaN NaN NaN NaN NaN NaN
In [111… df1.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 28 entries, 0 to 27
Data columns (total 10 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Unnamed: 0 0 non-null float64
1 Unnamed: 1 0 non-null float64
2 Unnamed: 2 0 non-null float64
3 Unnamed: 3 3 non-null object
4 Unnamed: 4 12 non-null object
5 Unnamed: 5 7 non-null object
6 Unnamed: 6 0 non-null float64
7 Unnamed: 7 6 non-null object
8 Unnamed: 8 4 non-null object
9 Unnamed: 9 1 non-null object
dtypes: float64(4), object(6)
memory usage: 2.3+ KB
In [112… pd.read_csv("taxonomy.csv")
Out[114… PassengerId Survived Pclass Name Sex Age SibSp Parch Ticket In [116… df3.shape
In [115… df3.columns
0 1 7.2500 3 Quincy
0 1 PF 24 NYK 68 22 1287 152 331 ... .784 79 222
Acy
1 2 71.2833 1
Jordan
2 3 7.9250 3 1 2 SG 20 MEM 30 0 248 35 86 ... .609 9 19
Adams
3 4 53.1000 1 Steven
2 3 C 21 OKC 70 67 1771 217 399 ... .502 199 324
Adams
4 5 8.0500 3
Jeff
... ... ... ... 3 4 PF 28 MIN 17 0 215 19 44 ... .579 23 54
Adrien
886 887 13.0000 2 Arron
4 5 SG 29 TOT 78 72 2502 375 884 ... .843 27 220
Afflalo
887 888 30.0000 1
... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
888 889 23.4500 3
Thaddeus
889 890 30.0000 1 670 490 PF 26 TOT 76 68 2434 451 968 ... .655 127 284
Young
890 891 7.7500 3 Thaddeus
671 490 PF 26 MIN 48 48 1605 289 641 ... .682 75 170
Young
891 rows × 3 columns
Thaddeus
672 490 PF 26 BRK 28 20 829 162 327 ... .606 52 114
Young
In [122… #pip install lxml Cody
673 491 C 22 CHO 62 45 1487 172 373 ... .774 97 265
Zeller
Collecting lxml
Downloading lxml-5.1.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_6 Tyler
4.whl (8.0 MB) 674 492 C 25 BOS 82 59 1731 340 619 ... .823 146 319
Zeller
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 8.0/8.0 MB 53.6 MB/s eta 0:00:000
0:0100:01 675 rows × 30 columns
Installing collected packages: lxml
Successfully installed lxml-5.1.0
Note: you may need to restart the kernel to use updated packages. In [10]: df4.shape
In [5]: type(url_df)
Out[5]: list
In [9]: df4
Out[11]: Rk Player Pos Age Tm G GS MP FG FGA ... FT% ORB DRB TRB <class 'pandas.core.frame.DataFrame'>
RangeIndex: 675 entries, 0 to 674
Quincy Data columns (total 30 columns):
0 1 PF 24 NYK 68 22 1287 152 331 ... .784 79 222 301
Acy # Column Non-Null Count Dtype
--- ------ -------------- -----
Jordan
1 2 SG 20 MEM 30 0 248 35 86 ... .609 9 19 28 0 Rk 675 non-null object
Adams
1 Player 675 non-null object
Steven 2 Pos 675 non-null object
2 3 C 21 OKC 70 67 1771 217 399 ... .502 199 324 523
Adams 3 Age 675 non-null object
4 Tm 675 non-null object
Jeff 5 G 675 non-null object
3 4 PF 28 MIN 17 0 215 19 44 ... .579 23 54 77
Adrien
6 GS 675 non-null object
Arron 7 MP 675 non-null object
4 5 SG 29 TOT 78 72 2502 375 884 ... .843 27 220 247 8 FG 675 non-null object
Afflalo
9 FGA 675 non-null object
10 FG% 673 non-null object
5 rows × 30 columns
11 3P 675 non-null object
12 3PA 675 non-null object
In [12]: df4.info() 13 3P% 594 non-null object
14 2P 675 non-null object
15 2PA 675 non-null object
16 2P% 671 non-null object
17 eFG% 673 non-null object
18 FT 675 non-null object
19 FTA 675 non-null object
20 FT% 650 non-null object
21 ORB 675 non-null object
22 DRB 675 non-null object
23 TRB 675 non-null object
24 AST 675 non-null object
25 STL 675 non-null object
26 BLK 675 non-null object
27 TOV 675 non-null object
28 PF 675 non-null object
29 PTS 675 non-null object
dtypes: object(30)
memory usage: 158.3+ KB
In [13]: df4.columns
Out[13]: Index(['Rk', 'Player', 'Pos', 'Age', 'Tm', 'G', 'GS', 'MP', 'FG', 'FGA', 'F
G%',
'3P', '3PA', '3P%', '2P', '2PA', '2P%', 'eFG%', 'FT', 'FTA', 'FT%',
'ORB', 'DRB', 'TRB', 'AST', 'STL', 'BLK', 'TOV', 'PF', 'PTS'],
dtype='object')
df = pd.read_csv("https://raw.githubusercontent.com/datasciencedojo/datasets/master/ti
In [2]: df
Out[2]: PassengerId Survived Pclass Name Sex Age SibSp Parch Ticket In [4]: df.head()
Braund, Out[4]: PassengerId Survived Pclass Name Sex Age SibSp Parch Ticket
A/5
0 1 0 3 Mr. Owen male 22.0 1 0
21171 Braund,
Harris A/5
0 1 0 3 Mr. Owen male 22.0 1 0
21171
Cumings, Harris
Mrs. John
Bradley PC Cumings,
1 2 1 1 female 38.0 1 0 Mrs. John
(Florence 17599
Briggs Bradley PC
1 2 1 1 female 38.0 1 0
Th... (Florence 17599
Briggs
Heikkinen, STON/ Th...
2 3 1 3 Miss. female 26.0 0 0 O2.
Laina 3101282 Heikkinen, STON/
2 3 1 3 Miss. female 26.0 0 0 O2.
Futrelle, Laina 3101282
Mrs.
Jacques Futrelle,
3 4 1 1 female 35.0 1 0 113803 Mrs.
Heath
(Lily May Jacques
3 4 1 1 female 35.0 1 0 113803
Peel) Heath
(Lily May
Allen, Mr. Peel)
4 5 0 3 William male 35.0 0 0 373450
Henry Allen, Mr.
4 5 0 3 William male 35.0 0 0 373450
... ... ... ... ... ... ... ... ... ... Henry
Montvila,
886 887 0 2 Rev. male 27.0 0 0 211536
Juozas In [5]: df.tail()
Graham, Out[5]: PassengerId Survived Pclass Name Sex Age SibSp Parch Ticket
Miss.
887 888 1 1 female 19.0 0 0 112053 Montvila,
Margaret
Edith 886 887 0 2 Rev. male 27.0 0 0 211536
Juozas
Johnston,
Miss. Graham,
W./C. Miss.
888 889 0 3 Catherine female NaN 1 2 887 888 1 1 female 19.0 0 0 112053
6607 Margaret
Helen
"Carrie" Edith
Dooley,
In [3]: df.columns 890 891 0 3 Mr. male 32.0 0 0 370376
Patrick
Out[3]: Index(['PassengerId', 'Survived', 'Pclass', 'Name', 'Sex', 'Age', 'SibSp',
'Parch', 'Ticket', 'Fare', 'Cabin', 'Embarked'],
dtype='object') In [6]: df.dtypes
Out[6]: PassengerId int64 In [10]: df[['PassengerId', 'Survived', 'Pclass']]
Survived int64
Pclass int64 Out[10]: PassengerId Survived Pclass
Name object
0 1 0 3
Sex object
Age float64 1 2 1 1
SibSp int64
Parch int64 2 3 1 3
Ticket object 3 4 1 1
Fare float64
Cabin object 4 5 0 3
Embarked object
dtype: object ... ... ... ...
886 887 0 2
In [7]: df.info()
887 888 1 1
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 891 entries, 0 to 890 888 889 0 3
Data columns (total 12 columns):
# Column Non-Null Count Dtype 889 890 1 1
--- ------ -------------- ----- 890 891 0 3
0 PassengerId 891 non-null int64
1 Survived 891 non-null int64
2 Pclass 891 non-null int64 891 rows × 3 columns
3 Name 891 non-null object
4 Sex 891 non-null object
In [12]: df.dtypes == 'object' #object is string data type
5 Age 714 non-null float64
6 SibSp 891 non-null int64
Out[12]: PassengerId False
7 Parch 891 non-null int64
Survived False
8 Ticket 891 non-null object
Pclass False
9 Fare 891 non-null float64
Name True
10 Cabin 204 non-null object
Sex True
11 Embarked 889 non-null object
Age False
dtypes: float64(2), int64(5), object(5)
SibSp False
memory usage: 83.7+ KB
Parch False
Ticket True
In [9]: df.describe() #statistical information about numerical column
Fare False
Cabin True
Out[9]: PassengerId Survived Pclass Age SibSp Parch
Embarked True
count 891.000000 891.000000 891.000000 714.000000 891.000000 891.000000 891.000000 dtype: bool
mean 446.000000 0.383838 2.308642 29.699118 0.523008 0.381594 In [16]: df.dtypes[df.dtypes == 'object']
std 257.353842 0.486592 0.836071 14.526497 1.102743 0.806057
Out[16]: Name object
min 1.000000 0.000000 1.000000 0.420000 0.000000 0.000000 Sex object
Ticket object
25% 223.500000 0.000000 2.000000 20.125000 0.000000 0.000000 Cabin object
Embarked object
50% 446.000000 0.000000 3.000000 28.000000 0.000000 0.000000
dtype: object
75% 668.500000 1.000000 3.000000 38.000000 1.000000 0.000000
In [17]: df.dtypes[df.dtypes == 'object'].index
max 891.000000 1.000000 3.000000 80.000000 8.000000 6.000000 512.329200
Out[17]: Index(['Name', 'Sex', 'Ticket', 'Cabin', 'Embarked'], dtype='object')
In [18]: df[df.dtypes[df.dtypes == 'object'].index] In [20]: df[df.dtypes[df.dtypes != 'object'].index].describe()
Out[18]: Name Sex Ticket Cabin Embarked Out[20]: PassengerId Survived Pclass Age SibSp Parch
0 Braund, Mr. Owen Harris male A/5 21171 NaN S count 891.000000 891.000000 891.000000 714.000000 891.000000 891.000000 891.0000
Cumings, Mrs. John Bradley mean 446.000000 0.383838 2.308642 29.699118 0.523008 0.381594
1 female PC 17599 C85 C
(Florence Briggs Th...
std 257.353842 0.486592 0.836071 14.526497 1.102743 0.806057
STON/O2.
2 Heikkinen, Miss. Laina female NaN S min 1.000000 0.000000 1.000000 0.420000 0.000000 0.000000
3101282
Futrelle, Mrs. Jacques Heath (Lily 25% 223.500000 0.000000 2.000000 20.125000 0.000000 0.000000
3 female 113803 C123 S
May Peel)
50% 446.000000 0.000000 3.000000 28.000000 0.000000 0.000000
4 Allen, Mr. William Henry male 373450 NaN S
75% 668.500000 1.000000 3.000000 38.000000 1.000000 0.000000
... ... ... ... ... ...
max 891.000000 1.000000 3.000000 80.000000 8.000000 6.000000 512.3292
886 Montvila, Rev. Juozas male 211536 NaN S
887 Graham, Miss. Margaret Edith female 112053 B42 S In [21]: df.describe()
Johnston, Miss. Catherine Helen
888 female W./C. 6607 NaN S Out[21]: PassengerId Survived Pclass Age SibSp Parch
"Carrie"
889 Behr, Mr. Karl Howell male 111369 C148 C count 891.000000 891.000000 891.000000 714.000000 891.000000 891.000000 891.0000
890 Dooley, Mr. Patrick male 370376 NaN Q mean 446.000000 0.383838 2.308642 29.699118 0.523008 0.381594
In [19]: df[df.dtypes[df.dtypes != 'object'].index] 25% 223.500000 0.000000 2.000000 20.125000 0.000000 0.000000
2 3 1 3 26.0 0 0 7.9250
In [22]: df.describe(include = 'object')
3 4 1 1 35.0 1 0 53.1000
Out[22]: Name Sex Ticket Cabin Embarked
4 5 0 3 35.0 0 0 8.0500
count 891 891 891 204 889
... ... ... ... ... ... ... ...
unique 891 2 681 147 3
886 887 0 2 27.0 0 0 13.0000
top Braund, Mr. Owen Harris male 347082 B96 B98 S
887 888 1 1 19.0 0 0 30.0000
freq 1 577 7 4 644
888 889 0 3 NaN 1 2 23.4500
count 891.000000 891.000000 891.000000 891 891 714.000000 891.000000 count 891 891 891 891 891 714.0 891 891 891
unique NaN NaN NaN 891 2 NaN NaN unique 891 2 3 891 2 88.0 7 7 681
Braund, Braund,
Mr. Mr.
top NaN NaN NaN male NaN NaN top 1 0 3 male 24.0 0 0 347082
Owen Owen
Harris Harris
freq NaN NaN NaN 1 577 NaN NaN freq 1 549 491 1 577 30.0 608 678 7
std 257.353842 0.486592 0.836071 NaN NaN 14.526497 1.102743 In [32]: df[0:100:5]
Holverson,
Mr.
35 36 0 1 male 42.0 1 0 113789
Alexander
Oskar
Ahlin, Mrs.
Johan
40 41 0 3 (Johanna female 40.0 1 0 7546
Persdotter
Larsson)
Rogers, Mr.
S.C./A.4.
45 46 0 3 William male NaN 0 0
23567
John
Panula,
50 51 0 3 Master. male 7.0 4 1 3101295
Juha Niilo
Woolner,
55 56 1 1 male NaN 0 0 19947
Mr. Hugh
Sirayanian,
60 61 0 3 male 22.0 0 0 2669
Mr. Orsen
Moubarek,
65 66 1 3 Master. male NaN 1 1 2661
Gerios
Out[36]: PassengerId Survived Pclass Name Sex Age SibSp Parch Ticket Out[38]: PassengerId Survived Pclass Name Sex Age SibSp Parch Ticket
Braund, Braund,
A/5 A/5
0 1 0 3 Mr. Owen male 22.0 1 0 0 1 0 3 Mr. Owen male 22.0 1 0
21171 21171
Harris Harris
Cumings, Cumings,
Mrs. John Mrs. John
Bradley PC Bradley PC
1 2 1 1 female 38.0 1 0 1 2 1 1 female 38.0 1 0
(Florence 17599 (Florence 17599
Briggs Briggs
Th... Th...
Futrelle, Futrelle,
Mrs. Mrs.
Jacques Jacques
3 4 1 1 female 35.0 1 0 113803 3 4 1 1 female 35.0 1 0 113803
Heath Heath
(Lily May (Lily May
Peel) Peel)
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
Montvila, Montvila,
886 887 0 2 Rev. male 27.0 0 0 211536 886 887 0 2 Rev. male 27.0 0 0 211536
Juozas Juozas
Graham, Graham,
Miss. Miss.
887 888 1 1 female 19.0 0 0 112053 887 888 1 1 female 19.0 0 0 112053
Margaret Margaret
Edith Edith
Johnston, Johnston,
Miss. Miss.
W./C. W./C.
888 889 0 3 Catherine female NaN 1 2 888 889 0 3 Catherine female NaN 1 2
6607 6607
Helen Helen
"Carrie" "Carrie"
Dooley, Dooley,
890 891 0 3 male 32.0 0 0 370376 890 891 0 3 male 32.0 0 0 370376
Mr. Patrick Mr. Patrick
In [38]: df
<class 'pandas.core.frame.DataFrame'> Out[44]: array([nan, 'C85', 'C123', 'E46', 'G6', 'C103', 'D56', 'A6',
RangeIndex: 891 entries, 0 to 890 'C23 C25 C27', 'B78', 'D33', 'B30', 'C52', 'B28', 'C83', 'F33',
Data columns (total 14 columns): 'F G73', 'E31', 'A5', 'D10 D12', 'D26', 'C110', 'B58 B60', 'E101',
# Column Non-Null Count Dtype 'F E69', 'D47', 'B86', 'F2', 'C2', 'E33', 'B19', 'A7', 'C49', 'F4',
--- ------ -------------- ----- 'A32', 'B4', 'B80', 'A31', 'D36', 'D15', 'C93', 'C78', 'D35',
0 PassengerId 891 non-null int64 'C87', 'B77', 'E67', 'B94', 'C125', 'C99', 'C118', 'D7', 'A19',
1 Survived 891 non-null int64 'B49', 'D', 'C22 C26', 'C106', 'C65', 'E36', 'C54',
2 Pclass 891 non-null int64 'B57 B59 B63 B66', 'C7', 'E34', 'C32', 'B18', 'C124', 'C91', 'E40',
3 Name 891 non-null object 'T', 'C128', 'D37', 'B35', 'E50', 'C82', 'B96 B98', 'E10', 'E44',
4 Sex 891 non-null object 'A34', 'C104', 'C111', 'C92', 'E38', 'D21', 'E12', 'E63', 'A14',
5 Age 714 non-null float64 'B37', 'C30', 'D20', 'B79', 'E25', 'D46', 'B73', 'C95', 'B38',
6 SibSp 891 non-null int64 'B39', 'B22', 'C86', 'C70', 'A16', 'C101', 'C68', 'A10', 'E68',
7 Parch 891 non-null int64 'B41', 'A20', 'D19', 'D50', 'D9', 'A23', 'B50', 'A26', 'D48',
8 Ticket 891 non-null object 'E58', 'C126', 'B71', 'B51 B53 B55', 'D49', 'B5', 'B20', 'F G63',
9 Fare 891 non-null float64 'C62 C64', 'E24', 'C90', 'C45', 'E8', 'B101', 'D45', 'C46', 'D30',
10 Cabin 204 non-null object 'E121', 'D11', 'E77', 'F38', 'B3', 'D6', 'B82 B84', 'D17', 'A36',
11 Embarked 889 non-null object 'B102', 'B69', 'E49', 'C47', 'D28', 'E17', 'A24', 'C50', 'B42',
12 new_col 891 non-null object 'C148'], dtype=object)
13 family 891 non-null int64
dtypes: float64(2), int64(6), object(6) In [45]: df['Cabin'].nunique()
memory usage: 97.6+ KB
Out[45]: 147
In [41]: pd.Categorical(df['Pclass']) #different categories of pclass
In [47]: df['Cabin'].value_counts()
Out[48]: PassengerId Survived Pclass Name Sex Age SibSp Parch Ticket Palsson,
Master.
Braund, 7 8 0 3 male 2.00 3 1 349909
A/5 Gosta
0 1 0 3 Mr. Owen male 22.0 1 0 Leonard
21171
Harris
Sandstrom,
Cumings, Miss.
Mrs. John 10 11 1 3 female 4.00 1 1 PP 9549
Marguerite
Bradley PC Rut
1 2 1 1 female 38.0 1 0
(Florence 17599
Briggs Rice,
Th... 16 17 0 3 Master. male 2.00 4 1 382652
Eugene
Heikkinen, STON/
2 3 1 3 Miss. female 26.0 0 0 O2. Laroche,
Laina 3101282 Miss.
SC/P
43 44 1 2 Simonne female 3.00 1 2
Futrelle, 2123
Marie Anne
Mrs. Andree
Jacques
3 4 1 1 female 35.0 1 0 113803
Heath Skoog,
(Lily May 63 64 0 3 Master. male 4.00 3 2 347088
Peel) Harald
... ... ... ... ... ... ... ... ... ... Andersson,
119 120 0 3 Miss. Ellis female 2.00 4 2 347082
Montvila, Anna Maria
886 887 0 2 Rev. male 27.0 0 0 211536
Juozas Panula,
164 165 0 3 Master. Eino male 1.00 4 1 3101295
Graham, Viljami
Miss.
887 888 1 1 female 19.0 0 0 112053
Margaret Rice,
Edith 171 172 0 3 Master. male 4.00 4 1 382652
Arthur
Johnston,
Miss. Johnson,
W./C.
888 889 0 3 Catherine female NaN 1 2 Miss.
6607 172 173 1 3 female 1.00 1 1 347742
Helen Eleanor
"Carrie" Ileen
Dooley, Kink-
890 891 0 3 male 32.0 0 0 370376
Mr. Patrick Heilmann,
184 185 1 3 female 4.00 0 2 315153
Miss. Luise
Gretchen
891 rows × 14 columns
Navratil,
193 194 1 2 Master. male 3.00 1 1 230080
In [52]: #Q. How many passengers are less than 5 years old Michel M
df[df['Age'] < 5]
205 206 0 3 Strom, Miss. female 2.00 0 1 347054
PassengerId Survived Pclass Name Sex Age SibSp Parch Tick PassengerId Survived Pclass Name Sex Age SibSp Parch Tick
530 531 1 2 Quick, Miss. female 2.00 1 1 26360 In [53]: len(df[df['Age'] < 5])
Out[53]: 40 Out[56]: ['Palsson, Master. Gosta Leonard',
'Sandstrom, Miss. Marguerite Rut',
In [55]: df[df['Age'] < 5].Name 'Rice, Master. Eugene',
'Laroche, Miss. Simonne Marie Anne Andree',
Out[55]: 7 Palsson, Master. Gosta Leonard 'Skoog, Master. Harald',
10 Sandstrom, Miss. Marguerite Rut 'Caldwell, Master. Alden Gates',
16 Rice, Master. Eugene 'Andersson, Miss. Ellis Anna Maria',
43 Laroche, Miss. Simonne Marie Anne Andree 'Panula, Master. Eino Viljami',
63 Skoog, Master. Harald 'Rice, Master. Arthur',
78 Caldwell, Master. Alden Gates 'Johnson, Miss. Eleanor Ileen',
119 Andersson, Miss. Ellis Anna Maria 'Becker, Master. Richard F',
164 Panula, Master. Eino Viljami 'Kink-Heilmann, Miss. Luise Gretchen',
171 Rice, Master. Arthur 'Navratil, Master. Michel M',
172 Johnson, Miss. Eleanor Ileen 'Strom, Miss. Telma Matilda',
183 Becker, Master. Richard F 'Asplund, Master. Edvin Rojj Felix',
184 Kink-Heilmann, Miss. Luise Gretchen 'Allison, Miss. Helen Loraine',
193 Navratil, Master. Michel M 'Allison, Master. Hudson Trevor',
205 Strom, Miss. Telma Matilda 'Navratil, Master. Edmond Roger',
261 Asplund, Master. Edvin Rojj Felix 'Coutts, Master. William Loch "William"',
297 Allison, Miss. Helen Loraine 'Palsson, Miss. Stina Viola',
305 Allison, Master. Hudson Trevor 'Nakid, Miss. Maria ("Mary")',
340 Navratil, Master. Edmond Roger 'Goodwin, Master. Sidney Leonard',
348 Coutts, Master. William Loch "William" 'Richards, Master. William Rowe',
374 Palsson, Miss. Stina Viola 'Dodge, Master. Washington',
381 Nakid, Miss. Maria ("Mary") 'Baclini, Miss. Helene Barbara',
386 Goodwin, Master. Sidney Leonard 'Hirvonen, Miss. Hildur E',
407 Richards, Master. William Rowe 'Quick, Miss. Phyllis May',
445 Dodge, Master. Washington 'Becker, Miss. Marion Louise',
469 Baclini, Miss. Helene Barbara 'Skoog, Miss. Margit Elizabeth',
479 Hirvonen, Miss. Hildur E 'Baclini, Miss. Eugenie',
530 Quick, Miss. Phyllis May 'Karun, Miss. Manca',
618 Becker, Miss. Marion Louise 'Wells, Miss. Joan',
642 Skoog, Miss. Margit Elizabeth 'Hamalainen, Master. Viljo',
644 Baclini, Miss. Eugenie 'Dean, Master. Bertram Vere',
691 Karun, Miss. Manca 'Thomas, Master. Assad Alexander',
750 Wells, Miss. Joan 'Panula, Master. Urho Abraham',
755 Hamalainen, Master. Viljo 'Mallet, Master. Andre',
788 Dean, Master. Bertram Vere 'Richards, Master. George Sibley',
803 Thomas, Master. Assad Alexander 'Andersson, Master. Sigvard Harald Elias',
824 Panula, Master. Urho Abraham 'Johnson, Master. Harold Theodor']
827 Mallet, Master. Andre
831 Richards, Master. George Sibley In [58]: #how many person are less than 18 years old
850 Andersson, Master. Sigvard Harald Elias
869 Johnson, Master. Harold Theodor len(df[df['Age'] > 18])
Name: Name, dtype: object
Out[58]: 575
In [56]: list(df[df['Age'] < 5].Name)
In [60]: df['Fare'].mean()
Out[60]: 32.204207968574636
In [63]: #Q. How many passengers hav epaid less than avg fare
df[df['Fare'] < df['Fare'].mean()] In [69]: df[df['Sex'] == "male"]
Out[63]: PassengerId Survived Pclass Name Sex Age SibSp Parch Ticket Out[69]: PassengerId Survived Pclass Name Sex Age SibSp Parch Ticket
Braund, Braund,
A/5
0 1 0 3 Mr. Owen male 22.0 1 0 0 1 0 3 Mr. Owen male 22.0 1 0 A/5 21171
21171
Harris Harris
Palsson, Palsson,
Master. Master.
7 8 0 3 male 2.0 3 1 349909 7 8 0 3 male 2.0 3 1 349909
Gosta Gosta
Leonard Leonard
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
Montvila, Banfield,
886 887 0 2 Rev. male 27.0 0 0 211536 Mr. C.A./SOTON
883 884 0 2 male 28.0 0 0
Juozas Frederick 34068
James
Graham,
Miss. Sutehall,
887 888 1 1 female 19.0 0 0 112053 SOTON/OQ
Margaret 884 885 0 3 Mr. Henry male 25.0 0 0
392076
Edith Jr
Johnston, Montvila,
Miss. 886 887 0 2 Rev. male 27.0 0 0 211536
W./C.
888 889 0 3 Catherine female NaN 1 2 Juozas
6607
Helen
"Carrie" Behr, Mr.
889 890 1 1 Karl male 26.0 0 0 111369
Behr, Mr. Howell
889 890 1 1 Karl male 26.0 0 0 111369
Howell Dooley,
890 891 0 3 Mr. male 32.0 0 0 370376
Dooley, Patrick
890 891 0 3 male 32.0 0 0 370376
Mr. Patrick
577 rows × 14 columns
680 rows × 14 columns
Out[67]: 15
In [73]: df['Sex'].value_counts(normalize = True)
Out[73]: male 0.647587 Out[75]: PassengerId Survived Pclass Name Sex Age SibSp Parch Ticket
female 0.352413
Name: Sex, dtype: float64 Cumings,
Mrs. John
Bradley PC
In [75]: #how many passengers of class 1 1 2 1 1 female 38.0 1 0
(Florence 17599
Briggs
df[df['Pclass'] == 1] Th...
Futrelle,
Mrs.
3 4 1 1 Jacques female 35.0 1 0 113803
Heath (Lily
May Peel)
McCarthy,
6 7 0 1 Mr. male 54.0 0 0 17463
Timothy J
Bonnell,
11 12 1 1 Miss. female 58.0 0 0 113783
Elizabeth
Sloper, Mr.
23 24 1 1 William male 28.0 0 0 113788
Thompson
... ... ... ... ... ... ... ... ... ...
Beckwith,
Mrs.
Richard
871 872 1 1 female 47.0 1 1 11751
Leonard
(Sallie
Monypeny)
Carlsson,
872 873 0 1 Mr. Frans male 33.0 0 0 695
Olof
Potter,
Mrs.
Thomas Jr
879 880 1 1 female 56.0 0 1 11767
(Lily
Alexenia
Wilson)
Graham,
Miss.
887 888 1 1 female 19.0 0 0 112053
Margaret
Edith
Behr, Mr.
889 890 1 1 Karl male 26.0 0 0 111369
Howell
Heikkinen, STON/
2 3 1 3 Miss. female 26.0 0 0 O2.
Laina 3101282
Futrelle,
Mrs.
Jacques
3 4 1 1 female 35.0 1 0 113803
Heath
(Lily May
Peel)
Johnson,
Mrs.
Oscar W
8 9 1 3 female 27.0 0 2 347742
(Elisabeth
Vilhelmina
Berg)
Nasser,
Mrs.
9 10 1 2 Nicholas female 14.0 1 0 237736
(Adele
Achem)
... ... ... ... ... ... ... ... ... ...
Najib,
Miss.
875 876 1 3 Adele female 15.0 0 0 2667
Kiamie
"Jane"
Potter,
Mrs.
Thomas Jr
879 880 1 1 female 56.0 0 1 11767
(Lily
Alexenia
Wilson)
Shelley,
Mrs.
William
880 881 1 2 female 25.0 0 1 230433
(Imanita
Parrish
Hall)
Graham,
Miss.
887 888 1 1 female 19.0 0 0 112053
Margaret
Edith
PassengerId Survived Pclass Name Sex Age SibSp Parch Ticket Out[87]: PassengerId Survived Pclass Name Sex Age SibSp Parch Ticket
Futrelle,
In [78]: len(df[df['Survived'] == 1]) Mrs.
3 4 1 1 Jacques female 35.0 1 0 113803
Out[78]: 342 Heath (Lily
May Peel)
Wick, Mrs.
In [84]: df['Fare'].mean() George
856 857 1 1 Dennick female 45.0 1 1 36928
Out[84]: 32.204207968574636 (Mary
Hitchcock)
In [87]: df[(df['Sex'] == 'female') & (df['Fare'] > df['Fare'].mean())] Sage,
Miss.
CA.
863 864 0 3 Dorothy female NaN 8 2
2343
Edith
"Dolly"
Beckwith,
Mrs.
Richard
871 872 1 1 female 47.0 1 1 11751
Leonard
(Sallie
Monypeny)
PassengerId Survived Pclass Name Sex Age SibSp Parch Ticket Out[95]: PassengerId Survived Pclass Name Sex Age SibSp Parch Ticket
Potter, Ward,
PC
Mrs. 258 259 1 1 Miss. female 35.0 0 0 512.3292
17755
Thomas Jr Anna
879 880 1 1 female 56.0 0 1 11767
(Lily
Alexenia Cardeza,
Wilson) Mr.
PC
679 680 1 1 Thomas male 36.0 0 1 512.3292
17755
Drake
104 rows × 14 columns Martinez
Lesurer,
In [88]: len(df[(df['Sex'] == 'female') & (df['Fare'] > df['Fare'].mean())]) Mr. PC
737 738 1 1 male 35.0 0 0 512.3292
Gustave 17755
Out[88]: 104 J
Out[89]: 107
Out[96]: 258 Ward, Miss. Anna
679 Cardeza, Mr. Thomas Drake Martinez
In [90]: import numpy as np 737 Lesurer, Mr. Gustave J
np.mean(df.Fare) Name: Name, dtype: object
df = pd.read_csv("https://raw.githubusercontent.com/datasciencedojo/datasets/master/titanic.csv"
Out[39]: PassengerId Survived Pclass Name Sex Age SibSp Parch Ticket
Out[38]: PassengerId Survived Pclass Name Sex Age SibSp Parch Ticket Cumings,
Mrs. John
PC
Braund, 1 2 1 1 Bradley female 38.0 1 0
A/5 17599
0 1 0 3 Mr. Owen male 22.0 1 0 (Florence
21171
Harris Briggs Th...
Cumings, STON/
Heikkinen,
Mrs. John 2 3 1 3 female 26.0 0 0 O2.
Miss. Laina
Bradley PC 3101282
1 2 1 1 female 38.0 1 0
(Florence 17599
Briggs Futrelle, Mrs.
Th... Jacques
3 4 1 1 female 35.0 1 0 113803
Heath (Lily
Heikkinen, STON/ May Peel)
2 3 1 3 Miss. female 26.0 0 0 O2.
Laina 3101282 Allen, Mr.
4 5 0 3 William male 35.0 0 0 373450
Futrelle, Henry
Mrs.
Jacques ... ... ... ... ... ... ... ... ...
3 4 1 1 female 35.0 1 0 113803
Heath
(Lily May Shorney, Mr.
Peel) 95 96 0 3 Charles male NaN 0 0 374910
Joseph
Allen, Mr.
4 5 0 3 William male 35.0 0 0 373450 Goldschmidt, PC
96 97 0 1 male 71.0 0 0
Henry Mr. George B 17754
... ... ... ... ... ... ... ... ... ... Greenfield,
PC
97 98 1 1 Mr. William male 23.0 0 1
17759
Montvila, Bertram
886 887 0 2 Rev. male 27.0 0 0 211536
Juozas Doling, Mrs.
98 99 1 2 John T (Ada female 34.0 0 1 231919
Graham, Julia Bone)
Miss.
887 888 1 1 female 19.0 0 0 112053 Kantor, Mr.
Margaret 99 100 0 2 male 34.0 1 0 244367
Edith Sinai
Johnston,
100 rows × 12 columns
Miss.
W./C.
888 889 0 3 Catherine female NaN 1 2
6607
Helen
"Carrie" In [40]: #access rows and column of dataframe >>implicit/internal/integer indexing, explicit/na
Dooley,
890 891 0 3 male 32.0 0 0 370376
Mr. Patrick
Out[48]: a Heikkinen, Miss. Laina Out[56]: a Heikkinen, Miss. LainaMoran, Mr. James
b Futrelle, Mrs. Jacques Heath (Lily May Peel) b Futrelle, Mrs. Jacques Heath (Lily May Peel)Mc...
c Allen, Mr. William Henry c Allen, Mr. William HenryPalsson, Master. Gosta...
dtype: object dtype: object
In [50]: s
In [51]: "pw"+"skills"
Out[51]: 'pwskills'
Out[52]: a NaN
b NaN
c NaN
0 NaN
1 NaN
2 NaN
dtype: object
In [53]: #if you keep the index same, then for series concatenation can happen
Braund, Braund,
A/5 A/5
0 1 0 3 Mr. Owen male 22.0 1 0 0 0 3 Mr. Owen male 22.0 1 0 7.2500 NaN
21171 21171
Harris Harris
Cumings, Cumings,
Mrs. John Mrs. John
Bradley PC Bradley PC
1 2 1 1 female 38.0 1 0 1 1 1 female 38.0 1 0 71.2833 C85
(Florence 17599 (Florence 17599
Briggs Briggs
Th... Th...
Futrelle, Futrelle,
Mrs. Mrs.
Jacques Jacques
3 4 1 1 female 35.0 1 0 113803 3 1 1 female 35.0 1 0 113803 53.1000 C123
Heath Heath
(Lily May (Lily May
Peel) Peel)
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
Montvila, Montvila,
886 887 0 2 Rev. male 27.0 0 0 211536 886 0 2 Rev. male 27.0 0 0 211536 13.0000 NaN
Juozas Juozas
Graham, Graham,
Miss. Miss.
887 888 1 1 female 19.0 0 0 112053 887 1 1 female 19.0 0 0 112053 30.0000 B42
Margaret Margaret
Edith Edith
Johnston, Johnston,
Miss. Miss.
W./C. W./C.
888 889 0 3 Catherine female NaN 1 2 888 0 3 Catherine female NaN 1 2 23.4500 NaN
6607 6607
Helen Helen
"Carrie" "Carrie"
Dooley, Dooley,
890 891 0 3 male 32.0 0 0 370376 890 0 3 male 32.0 0 0 370376 7.7500 NaN
Mr. Patrick Mr. Patrick
In [58]: df.drop('PassengerId', axis=1, inplace=True)#axis =0>> rows, axis = 1 column, inplace =True means
In [61]:
modify df.drop(1,
the original
inplace=True)
dataframe
In [59]: df In [62]: df
Out[62]: Survived Pclass Name Sex Age SibSp Parch Ticket Fare Cabin Out[63]: Survived Pclass Name Sex Age SibSp Parch Ticket Fare Cabin
Braund, Braund,
A/5 A/5
0 0 3 Mr. Owen male 22.0 1 0 7.2500 NaN 0 0 3 Mr. Owen male 22.0 1 0 7.2500 NaN
21171 21171
Harris Harris
Futrelle, Futrelle,
Mrs. Mrs.
Jacques Jacques
3 1 1 female 35.0 1 0 113803 53.1000 C123 2 1 1 female 35.0 1 0 113803 53.1000 C123
Heath Heath
(Lily May (Lily May
Peel) Peel)
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
Montvila, Montvila,
886 0 2 Rev. male 27.0 0 0 211536 13.0000 NaN 885 0 2 Rev. male 27.0 0 0 211536 13.0000 NaN
Juozas Juozas
Graham, Graham,
Miss. Miss.
887 1 1 female 19.0 0 0 112053 30.0000 B42 886 1 1 female 19.0 0 0 112053 30.0000 B42
Margaret Margaret
Edith Edith
Johnston, Johnston,
Miss. Miss.
W./C. W./C.
888 0 3 Catherine female NaN 1 2 23.4500 NaN 887 0 3 Catherine female NaN 1 2 23.4500 NaN
6607 6607
Helen Helen
"Carrie" "Carrie"
Dooley, Dooley,
890 0 3 male 32.0 0 0 370376 7.7500 NaN 889 0 3 male 32.0 0 0 370376 7.7500 NaN
Mr. Patrick Mr. Patrick
Moran, Mr.
4 0 3 male NaN 0 0 330877 8.4583 NaN
James
... ... ... ... ... ... ... ... ... ...
Montvila,
885 Rev. 0 2 male 27.0 0 0 211536 13.0000 NaN
Juozas
Graham,
Miss.
886 1 1 female 19.0 0 0 112053 30.0000 B42
Margaret
Edith
Johnston,
Miss.
W./C.
887 Catherine 0 3 female NaN 1 2 23.4500 NaN
6607
Helen
"Carrie"
Behr, Mr.
888 Karl 1 1 male 26.0 0 0 111369 30.0000 C148
Howell
Dooley,
889 0 3 male 32.0 0 0 370376 7.7500 NaN
Mr. Patrick
In [72]: d
Out[72]: {'key1': [2, 3, 4, 5], 'key2': [4, 5, 6, 7], 'key3': [2, 3, 4, 5]}
In [73]: pd.DataFrame(d) Out[78]: taxonomy_id name parent_id parent_name
In [76]: pd.DataFrame(d)
290 rows × 4 columns
Out[76]: key1 key2 key3
0 2 4 2 In [79]: df1.shape
1 3 5 3 Out[79]: (290, 4)
2 4 6 4
In [81]: df1.describe()
3 5 7 5
Out[81]: taxonomy_id name parent_id parent_name
freq 1 4 11 15
In [82]: df1.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 290 entries, 0 to 289
Data columns (total 4 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 taxonomy_id 290 non-null object
1 name 290 non-null object
2 parent_id 279 non-null object
3 parent_name 279 non-null object
dtypes: object(4)
memory usage: 9.2+ KB
In [83]: df1 Out[87]: taxonomy_id name parent_id parent_name
1 101-01 Disaster Response 101 Emergency 2 101-02 Emergency Cash 101 Emergency
2 101-02 Emergency Cash 101 Emergency 3 101-02-01 Help Pay for Food 101-02 Emergency Cash
3 101-02-01 Help Pay for Food 101-02 Emergency Cash 4 101-02-02 Help Pay for Healthcare 101-02 Emergency Cash
4 101-02-02 Help Pay for Healthcare 101-02 Emergency Cash ... ... ... ... ...
... ... ... ... ... 285 111-01-07 Workplace Rights 111-01 Advocacy & Legal Aid
285 111-01-07 Workplace Rights 111-01 Advocacy & Legal Aid 286 111-02 Mediation 111 Legal
286 111-02 Mediation 111 Legal 287 111-03 Notary 111 Legal
287 111-03 Notary 111 Legal 288 111-04 Representation 111 Legal
288 111-04 Representation 111 Legal 289 111-05 Translation & Interpretation 111 Legal
In [91]: df1.dropna(axis = 1)
Out[91]: taxonomy_id name In [95]: df1.fillna("somevalue")
3 101-02-01 Help Pay for Food 2 101-02 Emergency Cash 101 Emergency
4 101-02-02 Help Pay for Healthcare 3 101-02-01 Help Pay for Food 101-02 Emergency Cash
... ... ... 4 101-02-02 Help Pay for Healthcare 101-02 Emergency Cash
286 111-02 Mediation 285 111-01-07 Workplace Rights 111-01 Advocacy & Legal Aid
289 111-05 Translation & Interpretation 288 111-04 Representation 111 Legal
4 Help Pay for Healthcare 3 101-02-01 Help Pay for Food 101-02 Emergency Cash
... ... 4 101-02-02 Help Pay for Healthcare 101-02 Emergency Cash
286 Mediation 285 111-01-07 Workplace Rights 111-01 Advocacy & Legal Aid
0 1.0 0.0
In [101… df2.A.fillna(df2['A'].mean())
1 2.0 11.0
Out[101… 0 1.00 2 0.0 12.0
1 2.00
2 5.75 3 4.0 13.0
3 4.00
4 5.0 0.0
4 5.00
5 5.75 5 0.0 15.0
6 7.00
7 8.00 6 7.0 16.0
8 9.00
7 8.0 0.0
9 10.00
Name: A, dtype: float64 8 9.0 18.0
Out[102… 0 1.0
1 2.0 In [106… df2.fillna("something")
2 6.0
3 4.0
4 5.0
5 6.0
6 7.0
7 8.0
8 9.0
9 10.0
Name: A, dtype: float64
In [103… df2
Out[106… A B Out[110… A B
In [108… df2.fillna(method = 'ffill') #forward fill>> start seeing from the bottom of dataframe and fillInwith
[113…
the df2.duplicated().sum()
next value observed
Out[108… A B Out[113… 0
0 1.0 NaN
In [117… df.mean()
1 2.0 11.0
/tmp/ipykernel_122/3698961737.py:1: FutureWarning: The default value of numeri
2 2.0 12.0 c_only in DataFrame.mean is deprecated. In a future version, it will default to
False. In addition, specifying 'numeric_only=None' is deprecated. Select only v
3 4.0 13.0
alid columns or specify the value of numeric_only to silence this warning.
4 5.0 13.0 df.mean()
In [110… df2.fillna(method = 'bfill') #backward fill>> see from top and fill with next observed value /tmp/ipykernel_122/530051474.py:1: FutureWarning: The default value of numeri
c_only in DataFrame.median is deprecated. In a future version, it will default
to False. In addition, specifying 'numeric_only=None' is deprecated. Select onl
y valid columns or specify the value of numeric_only to silence this warning.
df.median()
Out[118… Survived 0.0000
Pclass 3.0000
Age 28.0000
SibSp 0.0000
Parch 0.0000
Fare 14.4542
dtype: float64
In [119… df.std() Out[121… Name Survived Pclass Sex Age SibSp Parch Ticket Fare Cabin
Johnston,
Miss.
W./C.
887 Catherine 0 3 female NaN 1 2 23.4500 NaN
6607
Helen
"Carrie"
Behr, Mr.
888 Karl 1 1 male 26.0 0 0 111369 30.0000 C148
Howell
Dooley,
889 0 3 male 32.0 0 0 370376 7.7500 NaN
Mr. Patrick
In [122… #Q. What is the average fare paid by people who survive?
Out[127… 48.32828768328446
1 1 7.9250
In [130… #groupby
2 1 53.1000
df.groupby('Survived').mean(numeric_only = True)
3 0 8.0500
Out[130… Pclass Age SibSp Parch Fare 4 0 8.4583
Survived ... ... ...
0 2.531876 30.626179 0.553734 0.329690 22.117887 885 0 13.0000
1 1.953079 28.310277 0.472141 0.466276 48.328288 886 1 30.0000
887 0 23.4500
In [131… df.groupby('Survived').min(numeric_only = True)
888 1 30.0000
Out[131… Pclass Age SibSp Parch Fare 889 0 7.7500
Survived
890 rows × 2 columns
0 1 1.00 0 0 0.0
In [132… df.groupby('Survived').sum(numeric_only = True) Out[135… min max mean median count std var
0 1390 12985.50 304 181 12142.7199 1 0.0 512.3292 48.328288 26.0 341 66.683277 4446.659477
In [133… df.groupby('Survived').describe()
count mean std min 25% 50% 75% max count mean
Survived
0 549.0 2.531876 0.735805 1.0 2.0 3.0 3.0 3.0 424.0 30.626179
1 341.0 1.953079 0.863047 1.0 1.0 2.0 3.0 3.0 289.0 28.310277
2 rows × 40 columns
female 90 70 72
male 45 17 47
In [146… a
Pclass
2 87 5168.83 74 70 3801.8417
In [145… a.T
Out[145… Pclass 1 2 3
In [138… df.groupby(['Sex', 'Pclass'])['Survived'].sum()
Survived 135.0000 87.0000 119.0000
Out[138… Sex Pclass
female 1 90 Age 7073.4200 5168.8300 8924.9200
2 70 SibSp 89.0000 74.0000 302.0000
3 72
male 1 45 Parch 77.0000 70.0000 193.0000
2 17
Fare 18106.1292 3801.8417 6714.6951
3 47
Name: Survived, dtype: int64
2 17
In [150… df.head().T
3 47
Out[10]: 'PWSkill'
In [11]: df1
Out[12]: Name Sex Age 0 Braund, Mr. Owen Harris male 22.0 NaN NaN NaN
7 Palsson, Master. Gosta Leonard male 2.0 2 Heikkinen, Miss. Laina female 26.0 NaN NaN NaN
8 Johnson, Mrs. Oscar W (Elisabeth Vilhelmina Berg) female 27.0 Futrelle, Mrs. Jacques
3 female 35.0 NaN NaN NaN
Heath (Lily May Peel)
9 Nasser, Mrs. Nicholas (Adele Achem) female 14.0
4 Allen, Mr. William Henry male 35.0 NaN NaN NaN
1 Cumings, Mrs. John Bradley (Florence Briggs Th... female 38.0 Johnson, Mrs. Oscar W
8 NaN NaN NaN (Elisabeth Vilhelmina female 27.0
2 Heikkinen, Miss. Laina female 26.0 Berg)
3 Futrelle, Mrs. Jacques Heath (Lily May Peel) female 35.0 Nasser, Mrs. Nicholas
9 NaN NaN NaN female 14.0
(Adele Achem)
4 Allen, Mr. William Henry male 35.0
In [16]: df1
0 Braund, Mr. Owen Harris male 22.0 Moran, Mr. James male NaN 1 2 5 56
In [ ]: #merge 2 4 6 5
3 5 7 6
In [20]: df1 = pd.DataFrame({'key1':[1,2,4,5,6],
'key2':[4,5,6,7,8], 4 6 8 6
'key3':[3,4,5,6,6]
})
In [25]: df2
In [21]: df1
Out[25]: key1 key4 key5
Out[21]: key1 key2 key3
0 1 56 3
0 1 4 3 1 2 5 56
1 2 5 4 2 45 6 5
2 4 6 5 3 6 7 6
3 5 7 6 4 67 8 6
4 6 8 6
2 6 8 6 7 6
In [23]: df2
1 2 5 4 5.0 56.0 0 1 4 3
2 4 6 5 NaN NaN 1 2 5 4
3 5 7 6 NaN NaN 2 4 6 5
4 6 8 6 7.0 6.0 3 5 7 6
4 6 8 6
In [28]: df2
1 2 5 56 0 1 4 3
2 45 6 5 1 2 5 4
3 6 7 6 2 4 6 5
4 67 8 6 3 5 7 6
4 6 8 6
In [29]: pd.merge(df1, df2, how = 'right')
1 2 5.0 4.0 5 56 0 1 56 3
2 45 NaN NaN 6 5 1 2 5 56
3 6 8.0 6.0 7 6 2 45 6 5
4 67 NaN NaN 8 6 3 6 7 6
4 67 8 6
In [30]: pd.merge(df1, df2, how = 'outer') #all the keys from both the datframe
Out[30]: key1 key2 key3 key4 key5 In [34]: pd.merge(df1, df2, how = 'cross')
5 2 5 4 1 56 3
6 2 5 4 2 5 56 In [37]: #join
df1 = pd.DataFrame({'key1':[1,2,4,5,6],
7 2 5 4 45 6 5 'key2':[4,5,6,7,8],
'key3':[3,4,5,6,6]},
8 2 5 4 6 7 6
index = ['a', 'b', 'c', 'd', 'e']
9 2 5 4 67 8 6 )
10 4 6 5 1 56 3
In [38]: df2 = pd.DataFrame({'key6':[1,2,45,6,67],
11 4 6 5 2 5 56 'key4':[56,5,6,7,8],
'key5':[3,56,5,6,6]
12 4 6 5 45 6 5 },
index = ['a', 'b', 'h', 'i', 'j']
13 4 6 5 6 7 6
)
14 4 6 5 67 8 6
In [39]: df1
15 5 7 6 1 56 3
17 5 7 6 45 6 5 a 1 4 3
18 5 7 6 6 7 6 b 2 5 4
19 5 7 6 67 8 6 c 4 6 5
20 6 8 6 1 56 3 d 5 7 6
21 6 8 6 2 5 56 e 6 8 6
22 6 8 6 45 6 5
In [40]: df2
23 6 8 6 6 7 6
a 1 56 3
In [36]: pd.merge(df1, df2, how = "left", left_on = "key2", right_on = "key4") b 2 5 56
h 45 6 5
i 6 7 6
j 67 8 6
a 1 4 3 1 56 3 0 1 4 3 1 56 3
b 2 5 4 2 5 56 1 1 4 3 2 5 56
2 1 4 3 45 6 5
In [42]: df1.join(df2, how = "left") 3 1 4 3 6 7 6
10 4 6 5 1 56 3
In [43]: df1.join(df2, how = "right") 11 4 6 5 2 5 56
18 5 7 6 6 7 6
Braund, Braund,
A/5 A/5
0 1 0 3 Mr. Owen male 22.0 1 0 0 1 0 3 Mr. Owen male 22.0 1 0
21171 21171
Harris Harris
Cumings, Cumings,
Mrs. John Mrs. John
Bradley PC Bradley PC
1 2 1 1 female 38.0 1 0 1 2 1 1 female 38.0 1 0
(Florence 17599 (Florence 17599
Briggs Briggs
Th... Th...
Futrelle, Futrelle,
Mrs. Mrs.
Jacques Jacques
3 4 1 1 female 35.0 1 0 113803 3 4 1 1 female 35.0 1 0 113803
Heath Heath
(Lily May (Lily May
Peel) Peel)
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
Montvila, Montvila,
886 887 0 2 Rev. male 27.0 0 0 211536 886 887 0 2 Rev. male 27.0 0 0 211536
Juozas Juozas
Graham, Graham,
Miss. Miss.
887 888 1 1 female 19.0 0 0 112053 887 888 1 1 female 19.0 0 0 112053
Margaret Margaret
Edith Edith
Johnston, Johnston,
Miss. Miss.
W./C. W./C.
888 889 0 3 Catherine female NaN 1 2 888 889 0 3 Catherine female NaN 1 2
6607 6607
Helen Helen
"Carrie" "Carrie"
Dooley, Dooley,
890 891 0 3 male 32.0 0 0 370376 890 891 0 3 male 32.0 0 0 370376
Mr. Patrick Mr. Patrick
In [50]: df
Out[51]: 0 652.500 Out[62]: 0 652.500
1 6415.497 1 6415.497
2 713.250 2 713.250
3 4779.000 3 4779.000
4 724.500 4 724.500
... ...
886 1170.000 886 1170.000
887 2700.000 887 2700.000
888 2110.500 888 2110.500
889 2700.000 889 2700.000
890 697.500 890 697.500
Name: Fare_inr, Length: 891, dtype: float64 Name: Fare_1, Length: 891, dtype: float64
Out[56]: 0 23
1 51 In [66]: df['flag_fare'] = df['Fare'].apply(create_flag)
2 22
3 44 In [67]: df
4 24
..
886 21
887 28
888 40
889 21
890 19
Name: Name_len, Length: 891, dtype: int64
In [62]: df['Fare_1']
Out[67]: PassengerId Survived Pclass Name Sex Age SibSp Parch Ticket In [95]: df1
Braund, Out[95]: a b c
A/5
0 1 0 3 Mr. Owen male 22.0 1 0
21171 0 1 5 pw
Harris
Cumings, 1 2 5 skills
Mrs. John
Bradley PC 2 3 6 aj
1 2 1 1 female 38.0 1 0
(Florence 17599
Briggs 3 4 7 cj
Th...
... ... ... ... ... ... ... ... ... ... cj 4 7
Montvila,
886 887 0 2 Rev. male 27.0 0 0 211536 In [98]: df1.reset_index(drop = True, inplace=True)
Juozas
1 2 5
891 rows × 16 columns
2 3 6
0 1 5 Out[107… a 10
b 23
1 2 5 dtype: int64
2 3 6
In [108… df1
3 4 7
Out[108… a b
Out[104… a b 0 1 5
0 1 5 1 2 5
1 2 5 2 3 6
2 3 6 3 4 7
3 4 7
In [113… df1
0 1 5 Braund,
A/5
0 1 0 3 Mr. Owen male 22.0 1 0
1 2 5 21171
Harris
2 3 6 Cumings,
Mrs. John
3 4 7 Bradley PC
1 2 1 1 female 38.0 1 0
(Florence 17599
Briggs
In [117… data = {"a": [100, 200, 13, 4], Th...
"b": [5, 5, 6, 7],
Heikkinen, STON/
"c": ["pw", "skills", "aj", "cj"]}
2 3 1 3 Miss. female 26.0 0 0 O2.
df2 = pd.DataFrame(data) Laina 3101282
Montvila,
In [119… df2.sort_values(by = 'a') 886 887 0 2 Rev. male 27.0 0 0 211536
Juozas
Out[119… a b c
Graham,
3 4 7 cj Miss.
887 888 1 1 female 19.0 0 0 112053
Margaret
2 13 6 aj Edith
0 100 5 pw Johnston,
Miss.
1 200 5 skills W./C.
888 889 0 3 Catherine female NaN 1 2
6607
Helen
"Carrie"
In [120… df
Behr, Mr.
889 890 1 1 Karl male 26.0 0 0 111369
Howell
Dooley,
890 891 0 3 male 32.0 0 0 370376
Mr. Patrick
In [ ]:
In [2]: import pandas as pd Out[4]: PassengerId Survived Pclass Name Sex Age SibSp Parch Ticket
import numpy as np
Braund,
A/5
0 1 0 3 Mr. Owen male 22.0 1 0
In [3]: df = pd.read_csv("https://raw.githubusercontent.com/datasciencedojo/datasets/master/titanic.csv" 21171
Harris
In [4]: df Cumings,
Mrs. John
Bradley PC
1 2 1 1 female 38.0 1 0
(Florence 17599
Briggs
Th...
Heikkinen, STON/
2 3 1 3 Miss. female 26.0 0 0 O2.
Laina 3101282
Futrelle,
Mrs.
Jacques
3 4 1 1 female 35.0 1 0 113803
Heath
(Lily May
Peel)
Allen, Mr.
4 5 0 3 William male 35.0 0 0 373450
Henry
... ... ... ... ... ... ... ... ... ...
Montvila,
886 887 0 2 Rev. male 27.0 0 0 211536
Juozas
Graham,
Miss.
887 888 1 1 female 19.0 0 0 112053
Margaret
Edith
Johnston,
Miss.
W./C.
888 889 0 3 Catherine female NaN 1 2
6607
Helen
"Carrie"
Behr, Mr.
889 890 1 1 Karl male 26.0 0 0 111369
Howell
Dooley,
890 891 0 3 male 32.0 0 0 370376
Mr. Patrick
In [5]: df1 = pd.DataFrame({"description":["PW Skills is the one-stop destination for your ups
In [6]: df1
Out[6]: description Out[10]: PassengerId Survived Pclass Name Sex Age SibSp Parch Ticket
Futrelle,
Mrs.
In [10]: df Jacques
3 4 1 1 female 35.0 1 0 113803
Heath
(Lily May
Peel)
Allen, Mr.
4 5 0 3 William male 35.0 0 0 373450
Henry
... ... ... ... ... ... ... ... ... ...
Montvila,
886 887 0 2 Rev. male 27.0 0 0 211536
Juozas
Graham,
Miss.
887 888 1 1 female 19.0 0 0 112053
Margaret
Edith
Johnston,
Miss.
W./C.
888 889 0 3 Catherine female NaN 1 2
6607
Helen
"Carrie"
Behr, Mr.
889 890 1 1 Karl male 26.0 0 0 111369
Howell
Dooley,
890 891 0 3 male 32.0 0 0 370376
Mr. Patrick
In [16]: df[:500]
Out[16]: PassengerId Survived Pclass Name Sex Age SibSp Parch Ticket Out[19]: description char_len
Braund, PW Skills is the one-stop destination for your upskilling journey. Brace
A/5
0 1 0 3 Mr. Owen male 22.0 1 0 yourself to find the best job-ready courses and high-end technologies
21171
Harris 0 available in the sector. And if that weren't good enough, get the highest 313
quality course content at the most affordable prices!What are we waiting
Cumings, for ? Let's push Start!
Mrs. John
Bradley PC
1 2 1 1 female 38.0 1 0
(Florence 17599
In [20]: a = "I am Ajay"
Briggs
Thayer) len(a)
... ... ... ... ... ... ... ... ... ... PW Skills is the one-stop destination for your upskilling
journey. Brace yourself to find the best job-ready courses
Yousseff, and high-end technologies available in the sector. And if
0 313 50
495 496 0 3 Mr. male NaN 0 0 2627 that weren't good enough, get the highest quality course
Gerious content at the most affordable prices!What are we waiting
for ? Let's push Start!
Eustis,
Miss.
496 497 1 1 female 54.0 1 0 36947
Elizabeth In [30]: data = {'text':['Hello data science', 'I love ML', 'I read ml books']}
Mussey a = pd.DataFrame(data)
Shellard,
Mr. C.A In [31]: a
497 498 0 3 male NaN 0 0
Frederick 6212
William Out[31]: text
Allison, 0 Hello data science
Mrs.
Hudson J 1 I love ML
498 499 0 1 female 25.0 1 2 113781
C (Bessie
Waldo 2 I read ml books
Daniels)
Svensson,
499 500 0 3 male 24.0 0 0 350035 In [33]: a['text_lower'] = a['text'].str.lower()
Mr. Olof
In [19]: df1
Out[34]: text text_lower Out[46]: a b mul_a
1 I love ML i love ml 1 2 5 10
3 5 7 25
In [35]: a['text_upper'] = a['text'].str.upper() 4 6 8 30
In [36]: a
In [47]: df1.a.mean()
Out[36]: text text_lower text_upper
Out[47]: 3.6
0 Hello data science hello data science HELLO DATA SCIENCE
In [49]: df1.a.mode()
In [42]: a['text'][0].startswith('H')
Out[49]: 0 1
Out[42]: True
1 2
2 4
In [43]: df1 = pd.DataFrame({'a':[1,2,4,5,6], 3 5
'b':[4,5,6,7,8], 4 6
}) Name: a, dtype: int64
Out[44]: a b Out[50]: 1
0 1 4
In [51]: df1.a.max()
1 2 5
Out[51]: 6
2 4 6
3 5 7 In [52]: df1.a.sum()
4 6 8 Out[52]: 18
In [53]: df1.a.std()
In [45]: df1['mul_a'] = df1['a'] *5
Out[53]: 2.073644135332772
In [46]: df1
In [54]: df1.a.var()
Out[54]: 4.3
In [55]: df1.a.describe()
Out[55]: count 5.000000 Out[59]: a
mean 3.600000
std 2.073644 0 1.0
min 1.000000 1 2.0
25% 2.000000
50% 4.000000 2 3.0
75% 5.000000
max 6.000000 3 4.0
Name: a, dtype: float64 4 5.0
6 7.0
In [57]: df2
7 8.0
Out[57]: a
8 9.0
0 1
6 6.5
In [58]: df2.mean()
7 7.5
Out[58]: a 5.0
dtype: float64 8 8.5
In [64]: df3.dropna(inplace=True)
In [65]: df3
Out[65]: a In [71]: df2.rolling(window =2).min()
2 2.0 Out[71]: a
3 3.0 0 NaN
4 4.0 1 1.0
5 5.0 2 2.0
6 6.0 3 3.0
7 7.0 4 4.0
8 8.0 5 5.0
6 6.0
In [67]: df3['a_int'] = df3['a'].astype(int) 7 7.0
Out[68]: a a_int
In [72]: df2.rolling(window =2).max()
2 2.0 2
Out[72]: a
3 3.0 3
0 NaN
4 4.0 4
1 2.0
5 5.0 5
2 3.0
6 6.0 6
3 4.0
7 7.0 7
4 5.0
8 8.0 8
5 6.0
7 8.0
Out[70]: a
8 9.0
0 NaN
1 3.0
In [73]: df2
2 5.0
3 7.0
4 9.0
5 11.0
6 13.0
7 15.0
8 17.0
Out[73]: a In [81]: df2['a'].cumsum()
0 1 Out[81]: 0 1
1 3
1 2 2 6
2 3 3 10
4 15
3 4 5 21
6 28
4 5
7 36
5 6 8 45
Name: a, dtype: int64
6 7
In [82]: df2
7 8
8 9 Out[82]: a Group
0 1 A
2 3 A
In [77]: df2
3 4 A
Out[77]: a Group
4 5 B
0 1 A
5 6 A
1 2 A
6 7 A
2 3 A
7 8 B
3 4 A
8 9 A
4 5 B
5 6 A
In [83]: df = pd.DataFrame({"date": ['2024-03-08', '2024-03-09', '2024-03-10']})
6 7 A
In [85]: df.dtypes
7 8 B
In [122… data['Close'].resample('D').mean()
In [115… import yfinance as yf
Out[122… Date
In [118… data = yf.download('GOOG', start = '2023-03-01', end = '2024-03-01') 2023-03-01 90.510002
2023-03-02 92.309998
[*********************100%%**********************] 1 of 1 completed 2023-03-03 94.019997
2023-03-04 NaN
In [119… data 2023-03-05 NaN
...
Out[119… Open High Low Close Adj Close Volume 2024-02-25 NaN
Date 2024-02-26 138.750000
2024-02-27 140.100006
2023-03-01 90.160004 91.199997 89.849998 90.510002 90.510002 26323900 2024-02-28 137.429993
2024-02-29 139.779999
2023-03-02 89.860001 92.480003 89.769997 92.309998 92.309998 23328600 Freq: D, Name: Close, Length: 366, dtype: float64
2023-03-03 92.739998 94.110001 92.660004 94.019997 94.019997 30242500
In [124… #plotting function in pandas
2023-03-06 94.360001 96.300003 94.300003 95.580002 95.580002 28288200
d = pd.Series([1, 2, 3, 8, 9, 6])
2023-03-07 95.419998 96.089996 93.844002 94.169998 94.169998 24101500
2024-02-23 144.970001 145.955002 144.789993 145.289993 145.289993 14519400 Out[125… <AxesSubplot: >
2024-02-26 143.449997 143.839996 138.740005 138.750000 138.750000 33513000
In [121… data['Close'].resample('M').mean()
In [126… data = {"A": [1, 2, 3], "B": [4, 5, 6]} In [128… df['A'].plot(kind = 'line')
df = pd.DataFrame(data)
df Out[128… <AxesSubplot: >
Out[126… A B
0 1 4
1 2 5
2 3 6
In [127… df.plot()
In [4]: x = np.random.rand(50)
y = np.random.rand(50)
In [5]: x
In [6]: y
In [9]: df plt.plot(x, y)
Out[9]: data
Out[11]: [<matplotlib.lines.Line2D at 0x187801c0350>]
2024-03-29 0.139326
2024-03-30 -0.269635
2024-03-31 -0.744394
2024-04-01 0.232519
2024-04-02 0.112398
... ...
2026-12-19 -1.029113
2026-12-20 -0.472041
2026-12-21 -0.631387
2026-12-22 -0.469771
2026-12-23 -0.072284
plt.show()
plt.plot(x, np.cos(x))
plt.subplot(1, 2, 2) plt.subplot(2, 2, 2)
plt.plot(x, np.cos(x)) plt.plot(x, np.cos(x))
plt.show() plt.subplot(2, 2, 3)
plt.plot(x, np.tan(x))
plt.subplot(2, 2, 4)
plt.plot(x, np.cos(x+1))
plt.show()
In [29]: plt.figure() In [30]: plt.plot(x, np.sin(x-0), color = "blue")
plt.subplot(2, 1, 1)
plt.plot(x, np.sin(x)) Out[30]: [<matplotlib.lines.Line2D at 0x18781ed6a90>]
plt.subplot(2, 1, 2)
plt.plot(x, np.cos(x))
plt.show()
In [31]: plt.plot(x, np.sin(x-0), color = 'blue')
plt.ylim(-1.5, 1.5)
plt.xlim(1, 15)
plt.show()
plt.title("basic syntax")
plt.xlabel("x label") data = np.random.randn(1000) #10 bins
plt.ylabel("y label") plt.hist(data, color='r', bins = 20)
plt.axis('equal') plt.show()
plt.grid()
plt.show()
In [38]: data1
In [39]: data2
In [40]: plt.hist([data1, data2], bins = 20, color = ['r', 'b'], label = ['dist1', 'dist2'
plt.xlabel('Values')
plt.ylabel('frequency')
plt.title("stacked histogram")
plt.show()
In [36]: x
In [42]: y
plt.pie(students, labels = langs, autopct = '%1.1f%%') plt.pie(students, labels = langs, autopct = '%1.1f%%', explode = explode, shadow
plt.title("Pie chart") plt.title("Pie chart")
plt.show() plt.show()
In [49]: data = pd.read_csv("Bank_Churn.csv")
In [48]: #3d plot
data
x = np.random.rand(20)
Out[49]: RowNumber CustomerId Surname CreditScore Geography Gender Age
y = np.random.rand(20)
z = np.random.rand(20) 0 1 15634602 Hargrave 619 France Female 42
Out[52]: (array([ 611., 2179., 3629., 1871., 828., 523., 208., 127., 20.,
4.]),
array([18. , 25.4, 32.8, 40.2, 47.6, 55. , 62.4, 69.8, 77.2, 84.6, 92. ]),
<BarContainer object of 10 artists>)
#areaplot An area plot, also called a stacked area plot, is a type of data visualization that shows how
various quantitative factors have changed over time while highlighting how they all work together to
form the whole. It's particularly good for illustrating how elements of a whole change over time or along
a continuous dimension. A layered representation of the data is produced in an area plot by layering
many variables on top of one another. The cumulative influence of all levels provides insight into the
overall trend. Each layer represents a separate category or variable. To distinguish between categories,
the space beneath each layer is frequently colored.
# Sample data
years = [2021, 2018, 2022, 2023, 2017]
category1 = [20, 25, 30, 28, 35]
category2 = [10, 15, 20, 18, 25]
category3 = [5, 8, 10, 9, 15]
import warnings
warnings.filterwarnings("ignore")
In [3]: sns.get_dataset_names()
Out[3]: ['anagrams',
'anscombe',
'attention',
'brain_networks',
'car_crashes',
'diamonds',
'dots',
'dowjones',
'exercise',
'flights',
'fmri',
'geyser',
'glue',
'healthexp',
'iris',
'mpg',
'penguins',
'planets',
'seaice',
In [ ]: 'taxis',
'tips',
'titanic']
In [4]: df = sns.load_dataset('tips')
In [5]: df
Out[5]: total_bill tip sex smoker day time size
In [14]: df
Out[14]: time value
0 0 1.034972
1 1 4.242024
2 2 2.310250
3 3 2.014738
4 4 0.929523
In [15]: sns.relplot(x = 'time', y='value', kind = 'line', data = df, sort = True)
C:\Users\ajayk\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarn
ing: use_inf_as_na option is deprecated and will be removed in a future versio
n. Convert inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
C:\Users\ajayk\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarn In [16]: fmri = sns.load_dataset('fmri')
ing: use_inf_as_na option is deprecated and will be removed in a future versio
n. Convert inf values to NaN before operating instead.
In [17]: fmri
with pd.option_context('mode.use_inf_as_na', True):
Out[15]: <seaborn.axisgrid.FacetGrid at 0x1caf1b522d0>
Out[17]: subject timepoint event region signal
C:\Users\ajayk\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarn
ing: use_inf_as_na option is deprecated and will be removed in a future versio
n. Convert inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
C:\Users\ajayk\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarn In [20]: sns.relplot(x = 'timepoint', y='signal', kind = 'line', data=fmri, ci = 'sd')
ing: use_inf_as_na option is deprecated and will be removed in a future versio
n. Convert inf values to NaN before operating instead. C:\Users\ajayk\anaconda3\Lib\site-packages\seaborn\axisgrid.py:848: FutureWarni
with pd.option_context('mode.use_inf_as_na', True): ng:
Out[18]: <seaborn.axisgrid.FacetGrid at 0x1caf2053f50>
The `ci` parameter is deprecated. Use `errorbar='sd'` for the same effect.
func(*plot_args, **plot_kwargs)
C:\Users\ajayk\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarn
ing: use_inf_as_na option is deprecated and will be removed in a future versio
n. Convert inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
C:\Users\ajayk\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarn
ing: use_inf_as_na option is deprecated and will be removed in a future versio
n. Convert inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
Out[20]: <seaborn.axisgrid.FacetGrid at 0x1caf1d5a510>
In [21]: sns.relplot(x = 'timepoint', y='signal',estimator = None, kind = 'line', data=fmri In [22]: sns.relplot(x = 'timepoint', y='signal', kind = 'line', data=fmri, hue = 'event'
In [27]: sns.relplot(x = 'total_bill', y = 'tip', hue = 'smoker', data = tips, col = 'time'
In [26]: tips
In [28]: sns.relplot(x = 'total_bill', y = 'tip', hue = 'smoker', data = tips, col = 'size'
In [30]: sns.scatterplot(x = 'total_bill', y = 'tip', data = tips) 3 23.68 3.31 Male No Sun Dinner 2
C:\Users\ajayk\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarn
ing: use_inf_as_na option is deprecated and will be removed in a future versio
n. Convert inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
C:\Users\ajayk\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarn
ing: use_inf_as_na option is deprecated and will be removed in a future versio
n. Convert inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
C:\Users\ajayk\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1075: FutureWarn
ing: When grouping with a length-1 list-like, you will need to pass a length-1
tuple to get_group in a future version of pandas. Pass `(name,)` instead of `na
In [31]: tips me` to silence this warning.
data_subset = grouped_data.get_group(pd_key)
Out[32]: <seaborn.axisgrid.FacetGrid at 0x1caf75e8310>
In [33]: sns.catplot(x = 'day', y = 'total_bill', data = tips, kind = 'swarm') In [34]: sns.catplot(x = 'day', y = 'total_bill', data = tips, kind = 'box')
C:\Users\ajayk\anaconda3\Lib\site-packages\seaborn\categorical.py:641: FutureWa
rning: The default of observed=False is deprecated and will be changed to True
in a future version of pandas. Pass observed=False to retain current behavior o
r observed=True to adopt the future default and silence this warning.
grouped_vals = vals.groupby(grouper)
C:\Users\ajayk\anaconda3\Lib\site-packages\seaborn\categorical.py:641: FutureWa
rning: The default of observed=False is deprecated and will be changed to True
in a future version of pandas. Pass observed=False to retain current behavior o
r observed=True to adopt the future default and silence this warning.
grouped_vals = vals.groupby(grouper)
Out[35]: <seaborn.axisgrid.FacetGrid at 0x1caf77aef90>
C:\Users\ajayk\anaconda3\Lib\site-packages\seaborn\categorical.py:641: FutureWa
rning: The default of observed=False is deprecated and will be changed to True
in a future version of pandas. Pass observed=False to retain current behavior o
r observed=True to adopt the future default and silence this warning.
grouped_vals = vals.groupby(grouper)
C:\Users\ajayk\anaconda3\Lib\site-packages\seaborn\categorical.py:641: FutureWa
rning: The default of observed=False is deprecated and will be changed to True
in a future version of pandas. Pass observed=False to retain current behavior o
r observed=True to adopt the future default and silence this warning.
grouped_vals = vals.groupby(grouper)
C:\Users\ajayk\anaconda3\Lib\site-packages\seaborn\categorical.py:1794: FutureW
arning: use_inf_as_na option is deprecated and will be removed in a future vers
ion. Convert inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
C:\Users\ajayk\anaconda3\Lib\site-packages\seaborn\categorical.py:1794: FutureW
arning: use_inf_as_na option is deprecated and will be removed in a future vers
ion. Convert inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
C:\Users\ajayk\anaconda3\Lib\site-packages\seaborn\categorical.py:1794: FutureW
arning: use_inf_as_na option is deprecated and will be removed in a future vers
ion. Convert inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
C:\Users\ajayk\anaconda3\Lib\site-packages\seaborn\categorical.py:1794: FutureW
arning: use_inf_as_na option is deprecated and will be removed in a future vers
ion. Convert inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
Out[36]: <seaborn.axisgrid.FacetGrid at 0x1caf785e110>
C:\Users\ajayk\anaconda3\Lib\site-packages\seaborn\categorical.py:641: FutureWa
rning: The default of observed=False is deprecated and will be changed to True
in a future version of pandas. Pass observed=False to retain current behavior o
r observed=True to adopt the future default and silence this warning.
grouped_vals = vals.groupby(grouper)
Out[38]: <Axes: xlabel='day', ylabel='total_bill'>
In [39]: sns.jointplot(x = tips.total_bill, y = tips.tip) #raw a plot of two variables with bivariate and univariate graphs.
C:\Users\ajayk\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarn
ing: use_inf_as_na option is deprecated and will be removed in a future versio
n. Convert inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
C:\Users\ajayk\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarn
ing: use_inf_as_na option is deprecated and will be removed in a future versio
n. Convert inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True): In [40]: sns.jointplot(x = tips.total_bill, y = tips.tip, kind = 'reg')
Out[39]: <seaborn.axisgrid.JointGrid at 0x1caf9dad890>
C:\Users\ajayk\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarn
ing: use_inf_as_na option is deprecated and will be removed in a future versio
n. Convert inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
C:\Users\ajayk\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarn
ing: use_inf_as_na option is deprecated and will be removed in a future versio
n. Convert inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
Out[40]: <seaborn.axisgrid.JointGrid at 0x1caf9e0aa10>
In [41]: sns.jointplot(x = tips.total_bill, y = tips.tip, kind = 'hex') In [42]: sns.jointplot(x = tips.total_bill, y = tips.tip, kind = "kde") #kernel density estimat
In [44]: sns.pairplot(tips)
C:\Users\ajayk\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarn
ing: use_inf_as_na option is deprecated and will be removed in a future versio
n. Convert inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
C:\Users\ajayk\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarn
ing: use_inf_as_na option is deprecated and will be removed in a future versio
n. Convert inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
C:\Users\ajayk\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarn
ing: use_inf_as_na option is deprecated and will be removed in a future versio
n. Convert inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
In [43]: #pairplot >> it will plot all the pairwise relationship btw dataframes
Out[44]: <seaborn.axisgrid.PairGrid at 0x1cafa7ee450>
tips
C:\Users\ajayk\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarn
ing: use_inf_as_na option is deprecated and will be removed in a future versio
n. Convert inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
C:\Users\ajayk\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1057: FutureWarn
ing: The default of observed=False is deprecated and will be changed to True in
a future version of pandas. Pass observed=False to retain current behavior or o
bserved=True to adopt the future default and silence this warning.
grouped_data = data.groupby(
C:\Users\ajayk\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1075: FutureWarn
ing: When grouping with a length-1 list-like, you will need to pass a length-1
tuple to get_group in a future version of pandas. Pass `(name,)` instead of `na
me` to silence this warning.
data_subset = grouped_data.get_group(pd_key)
C:\Users\ajayk\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarn
ing: use_inf_as_na option is deprecated and will be removed in a future versio
n. Convert inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
C:\Users\ajayk\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1057: FutureWarn
ing: The default of observed=False is deprecated and will be changed to True in
a future version of pandas. Pass observed=False to retain current behavior or o
bserved=True to adopt the future default and silence this warning.
grouped_data = data.groupby(
C:\Users\ajayk\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1075: FutureWarn
ing: When grouping with a length-1 list-like, you will need to pass a length-1
tuple to get_group in a future version of pandas. Pass `(name,)` instead of `na
me` to silence this warning.
data_subset = grouped_data.get_group(pd_key)
C:\Users\ajayk\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarn
ing: use_inf_as_na option is deprecated and will be removed in a future versio
n. Convert inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
C:\Users\ajayk\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1057: FutureWarn
ing: The default of observed=False is deprecated and will be changed to True in
a future version of pandas. Pass observed=False to retain current behavior or o
bserved=True to adopt the future default and silence this warning.
grouped_data = data.groupby(
C:\Users\ajayk\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1075: FutureWarn
In [45]: sns.pairplot(tips, hue = 'smoker') ing: When grouping with a length-1 list-like, you will need to pass a length-1
tuple to get_group in a future version of pandas. Pass `(name,)` instead of `na
me` to silence this warning.
data_subset = grouped_data.get_group(pd_key)
Out[45]: <seaborn.axisgrid.PairGrid at 0x1cafa8a5690>
In [50]: sns.pairplot(tips, hue = 'smoker', palette = 'coolwarm') In [51]: #boxplot
# https://seaborn.pydata.org/generated/seaborn.color_palette.html
# https://seaborn.pydata.org/tutorial/color_palettes.html sns.boxplot(x = 'day', y = 'total_bill', data = tips, palette = "rainbow")
In [57]: tips.corr()
In [61]: df = sns.load_dataset("tips")
In [62]: df
Out[62]: total_bill tip sex smoker day time size In [64]: sns.lmplot(x = 'total_bill', y = 'tip', data=df)
In [4]: flowers
In [ ]:
In [6]: output_file('test.html')
p = figure(title = 'test_flower')
p.xaxis.axis_label = "petal_length"
p.yaxis.axis_label = "petal_width"
p.circle(flowers['petal_length'], flowers['petal_width'])
show(p)
In [7]: output_file('test.html')
p = figure(title = 'test_flower')
p.xaxis.axis_label = "petal_length"
p.yaxis.axis_label = "petal_width"
p.line(flowers['petal_length'], flowers['petal_width'])
show(p)
In [9]: x = [1, 2, 3, 4]
y = [3, 8, 4, 7]
output_file("line.html")
p = figure(title = "line chart")
p.line(x, y)
show(p)
In [11]: x = [1, 2, 3, 4]
y = [3, 8, 4, 7]
output_file("line.html")
p = figure(title = "line chart")
p.scatter(x, y, size = 20, fill_color='red', legend_label = "this is my data")
show(p)
In [ ]: