0% found this document useful (0 votes)
43 views56 pages

Unit 5-1

Sql cheet shite for bca

Uploaded by

rekhakanani14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views56 pages

Unit 5-1

Sql cheet shite for bca

Uploaded by

rekhakanani14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 56

Python Collections (Arrays)

There are four collection data types in the Python programming language:

 List is a collection which is ordered and changeable. Allows


duplicate members.
 Tuple is a collection which is ordered and unchangeable. Allows
duplicate members.
 Set is a collection which is unordered, unchangeable*, and
unindexed. No duplicate members.
 Dictionary is a collection which is ordered** and changeable. No
duplicate members.

Tuples
Tuple is an immutable (unchangeable) collection of elements of different
data types. It is an ordered collection, so it preserves the order of
elements in which they were defined.

Tuples are defined by enclosing elements in parentheses (), separated


by a comma. The following declares a tuple type variable.

Example: Tuple Variable Declaration

tpl=() # empty tuple


print(tpl)

names = ('Jeff', 'Bill', 'Steve', 'Yash') # string tuple


print(names)

nums = (1, 2, 3, 4, 5) # int tuple


print(nums)

employee=(1, 'Steve', True, 25, 12000) # heterogeneous data tuple


print(employee)
Output:

()
('Jeff', 'Bill', 'Steve', 'Yash')
(1, 2, 3, 4, 5)
(1, 'Steve', True, 25, 12000)
However, it is not necessary to enclose the tuple elements in
parentheses. The tuple object can include elements separated by a
comma without parentheses.

Example: Tuple Variable Declaration

names = 'Jeff', 'Bill', 'Steve', 'Yash' # string tuple


print(names)

nums = 1, 2, 3, 4, 5 # int tuple


print(nums)

employee=1, 'Steve', True, 25, 12000 # heterogeneous data tuple


print(employee)
Output:

('Jeff', 'Bill', 'Steve', 'Yash')


(1, 2, 3, 4, 5)
(1, 'Steve', True, 25, 12000)

Tuples cannot be declared with a single element unless followed by a


comma.

Example: Tuple Variable Declaration

names = ('Jeff') # considered as string type


print(names)
print(type(names))

names = ('Jeff',) # tuple with single element


print(names)
print(type(names))
Output:

'Jeff'
<class 'string'>
(Jeff)
<class 'tuple'>

Access Tuple Elements


Each element in the tuple is accessed by the index in the square
brackets []. An index starts with zero and ends with (number of
elements - 1), as shown below.

Example: Access Tuple Elements using Indexes


names = ('Jeff', 'Bill', 'Steve', 'Yash')
print(names[0]) # prints 'Jeff'
print(names[1]) # prints 'Bill'
print(names[2]) # prints 'Steve'
print(names[3]) # prints 'Yash'

nums = (1, 2, 3, 4, 5)
print(nums[0]) # prints 1
print(nums[1]) # prints 2
print(nums[4]) # prints 5
Output:
Jeff
Bill
Steve
Yash
1
2
5

The tuple supports negative indexing also, the same as list type. The
negative index for the first element starts from -number of
elements and ends with -1 for the last element.

Example: Negative Indexing


names = ('Jeff', 'Bill', 'Steve', 'Yash')
print(names[-4]) # prints 'Jeff'
print(names[-3]) # prints 'Bill'
print(names[-2]) # prints 'Steve'
print(names[-1]) # prints 'Yash'
Output:
Jeff
Bill
Steve
Yash

If the element at the specified index does not exist, then the error
"index out of range" will be thrown.

>>> names[5]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: tuple index out of range

Tuple elements can be unpacked and assigned to variables, as shown


below. However, the number of variables must match with the number
of elements in a tuple; otherwise, an error will be thrown.

Example: Access Tuple Elements using Indexes


names = ('Jeff', 'Bill', 'Steve', 'Yash')
a, b, c, d = names # unpack tuple
print(a, b, c, d)

Output:
Jeff Bill Steve Yash

Update or Delete Tuple Elements - Tuple is Immutable

Tuple is unchangeable. So, once a tuple is created, any operation that


seeks to change its contents is not allowed. For instance, trying to
modify or delete an element of names tuple will result in an error.

>>> names = ('Jeff', 'Bill', 'Steve', 'Yash')


>>> names[0] = 'Swati'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment

>>> del names[0]


Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object doesn't support item deletion

However, you can delete an entire tuple using the del keyword.

>>> del names

Tuple Class
The underlying type of a tuple is the tuple class. Check the type of a variable using
the type() function.

Example: Tuple Variable Declaration


names = ('Jeff', 'Bill', 'Steve', 'Yash')
print('names type: ', type(names))

nums = (1,2,3,4,5)
print('nums type: ', type(nums))
Output:
names type: <class 'tuple'>
nums type: <class 'tuple'>

The tuple() constructor is used to convert any iterable to tuple type.

Example: Tuple Variable Declaration


tpl = tuple('Hello') # converts string to tuple
print(tpl)
tpl = tuple([1,2,3,4,5]) # converts list to tuple
print(tpl)
tpl = tuple({1,2,3,4,5}) # converts set to tuple
print(tpl)
tpl = tuple({1:"One",2:"Two"}) # converts dictionary to tuple
print(tpl)
Output:
('H','e','l','l','o')
(1,2,3,4,5)
(1,2,3,4,5)
(1,2)

Tuple Operations

Concatenation

Python allows us to join tuples using concatenation operator depicted by symbol +.

Example –
>>> tuple1 = (1,3,5,7,9)
>>> tuple2 = (2,4,6,8,10)
>>> tuple1 + tuple2
(1, 3, 5, 7, 9, 2, 4, 6, 8, 10)

Repetition

Repetition operation is depicted by the symbol *. It is used to repeat elements of a tuple.

Example –
>>> tuple1 = (‘Hello’,’World’)
>>> tuple1 * 3
(‘Hello’, ‘World’, ‘Hello’, ‘World’, ‘Hello’, ‘World’)

Tuple Methods and Built-in Functions

Python provides many functions to work on tuples.

len()

Returns the length or the number of elements of the tuple passed as the argument.

Example –
>>> tuple1 = (10,20,30,40,50)
>>> len(tuple1)
5

tuple()

Creates an empty tuple if no argument is passed, if a sequence is passed as argument.

Example 1 –
>>> tuple1 = tuple()
>>> tuple1
( )

Example 2 –
>>> tuple1 = tuple(‘aeiou’)
>>> tuple1
(‘a’, ‘e’, ‘i’, ‘o’, ‘u’)

count()

Returns the number of times the given element appears in the tuple

Example –
>>> tuple1 = (10,20,30,10,40,10,50)
>>> tuple1.count(10)
3
>>> tuple1.count(90)
0

index()

Returns the index of the first occurrence of the element in the given tuple.

Example –
>>> tuple1 = (10,20,30,40,50)
>>> tuple1.index(30)
2
>>> tuple1.index(90)
ValueError: tuple.index(x): x not in tuple

sorted()

Takes elements in the tuple and returns a new sorted list. It should be noted that, sorted() does not make any
change to the original tuple.

Example –
>>> tuple1 = (“Rama”,”Heena”,”Raj”, “Mohsin”,”Aditya”)
>>> sorted(tuple1)
[‘Aditya’, ‘Heena’, ‘Mohsin’, ‘Raj’, ‘Rama’]

min()

Returns minimum or smallest element of the tuple.

Example –
>>> tuple1 = (19,12,56,18,9,87,34)
>>> min(tuple1)
9

max()

Returns maximum or largest element of the tuple


Example –
>>> tuple1 = (19,12,56,18,9,87,34)
>>> max(tuple1)
87

sum()

Returns sum of the elements of the tuple.

Example –
>>> tuple1 = (19,12,56,18,9,87,34)
>>> sum(tuple1)
235

Python Set
A Python set is the collection of the unordered items. Each element in the set must be unique, immutable, and
the sets remove the duplicate elements. Sets are mutable which means we can modify it after its creation.

Unlike other collections in Python, there is no index attached to the elements of the set, i.e., we cannot directly
access any element of the set by the index. However, we can print them all together, or we can get the list of
elements by looping through the set.

Creating a set
The set can be created by enclosing the comma-separated immutable items with the curly braces {}. Python also
provides the set() method, which can be used to create the set by the passed sequence.

Example 1: Using curly braces


Days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}
print(Days)
print(type(Days))
print("looping through the set elements ... ")
for i in Days:
print(i)

Output:

{'Friday', 'Tuesday', 'Monday', 'Saturday', 'Thursday', 'Sunday',


'Wednesday'}
<class 'set'>
looping through the set elements ...
Friday
Tuesday
Monday
Saturday
Thursday
Sunday
Wednesday
Example 2: Using set() method

Days = set(["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"])

print(Days)
print(type(Days))
print("looping through the set elements ... ")
for i in Days:
print(i)

Output:

{'Friday', 'Wednesday', 'Thursday', 'Saturday', 'Monday', 'Tuesday',


'Sunday'}
<class 'set'>
looping through the set elements ...
Friday
Wednesday
Thursday
Saturday
Monday
Tuesday
Sunday

It can contain any type of element such as integer, float, tuple etc. But mutable elements (list, dictionary, set) can't
be a member of set. Consider the following example.

# Creating a set which have immutable elements


set1 = {1,2,3, "JavaTpoint", 20.5, 14}
print(type(set1))
#Creating a set which have mutable element
set2 = {1,2,3,["Javatpoint",4]}
print(type(set2))

Output:

<class 'set'>

Traceback (most recent call last)


<ipython-input-5-9605bb6fbc68> in <module>
4
5 #Creating a set which holds mutable elements
----> 6 set2 = {1,2,3,["Javatpoint",4]}
7 print(type(set2))

TypeError: unhashable type: 'list'

In the above code, we have created two sets, the set set1 have immutable elements and set2 have one mutable
element as a list. While checking the type of set2, it raised an error, which means set can contain only immutable
elements.

Creating an empty set is a bit different because empty curly {} braces are also used to create a dictionary as well.
So Python provides the set() method used without an argument to create an empty set.
# Empty curly braces will create dictionary
set3 = {}
print(type(set3))

# Empty set using set() function


set4 = set()
print(type(set4))

Output:

<class 'dict'>
<class 'set'>

Let's see what happened if we provide the duplicate element to the set.

set5 = {1,2,4,4,5,8,9,9,10}
print("Return set with unique elements:",set5)

Output:

Return set with unique elements: {1, 2, 4, 5, 8, 9, 10}

In the above code, we can see that set5 consisted of multiple duplicate elements when we printed it remove the
duplicity from the set.

Adding items to the set


Python provides the add() method and update() method which can be used to add some particular item to the
set. The add() method is used to add a single element whereas the update() method is used to add multiple
elements to the set. Consider the following example.

Example: 1 - Using add() method


Months = set(["January","February", "March", "April", "May", "June"])
print("\nprinting the original set ... ")
print(months)
print("\nAdding other months to the set...");
Months.add("July");
Months.add ("August");
print("\nPrinting the modified set...");
print(Months)
print("\nlooping through the set elements ... ")
for i in Months:
print(i)

Output:

printing the original set ...


{'February', 'May', 'April', 'March', 'June', 'January'}

Adding other months to the set...


Printing the modified set...
{'February', 'July', 'May', 'April', 'March', 'August', 'June', 'January'}

looping through the set elements ...


February
July
May
April
March
August
June
January

To add more than one item in the set, Python provides the update() method. It accepts iterable as an argument.

Consider the following example.

Example - 2 Using update() function

Months = set(["January","February", "March", "April", "May", "June"])


print("\nprinting the original set ... ")
print(Months)
print("\nupdating the original set ... ")
Months.update(["July","August","September","October"]);
print("\nprinting the modified set ... ")
print(Months);

Output:

printing the original set ...


{'January', 'February', 'April', 'May', 'June', 'March'}

updating the original set ...


printing the modified set ...
{'January', 'February', 'April', 'August', 'October', 'May', 'June',
'July', 'September', 'March'}

Removing items from the set


Python provides the discard() method and remove() method which can be used to remove the items from the
set. The difference between these function, using discard() function if the item does not exist in the set then the
set remain unchanged whereas remove() method will through an error.

Consider the following example.

Example-1 Using discard() method


months = set(["January","February", "March", "April", "May", "June"])
print("\nprinting the original set ... ")
print(months)
print("\nRemoving some months from the set...");
months.discard("January");
months.discard("May");
print("\nPrinting the modified set...");
print(months)
print("\nlooping through the set elements ... ")
for i in months:
print(i)

Output:

printing the original set ...


{'February', 'January', 'March', 'April', 'June', 'May'}

Removing some months from the set...

Printing the modified set...


{'February', 'March', 'April', 'June'}

looping through the set elements ...


February
March
April
June

Python provides also the remove() method to remove the item from the set. Consider the following example to
remove the items using remove() method.

Example-2 Using remove() function


months = set(["January","February", "March", "April", "May", "June"])
print("\nprinting the original set ... ")
print(months)
print("\nRemoving some months from the set...");
months.remove("January");
months.remove("May");
print("\nPrinting the modified set...");
print(months)

Output:

printing the original set ...


{'February', 'June', 'April', 'May', 'January', 'March'}

Removing some months from the set...

Printing the modified set...


{'February', 'June', 'April', 'March'}

We can also use the pop() method to remove the item. Generally, the pop() method will always remove the last
item but the set is unordered, we can't determine which element will be popped from set.

Consider the following example to remove the item from the set using pop() method.

Months = set(["January","February", "March", "April", "May", "June"])


print("\nprinting the original set ... ")
print(Months)
print("\nRemoving some months from the set...");
Months.pop();
Months.pop();
print("\nPrinting the modified set...");
print(Months)

Output:

printing the original set ...


{'June', 'January', 'May', 'April', 'February', 'March'}

Removing some months from the set...

Printing the modified set...


{'May', 'April', 'February', 'March'}

In the above code, the last element of the Month set is March but the pop() method removed the June and
January because the set is unordered and the pop() method could not determine the last element of the set.

Python provides the clear() method to remove all the items from the set.

Consider the following example.

Months = set(["January","February", "March", "April", "May", "June"])


print("\nprinting the original set ... ")
print(Months)
print("\nRemoving all the items from the set...");
Months.clear()
print("\nPrinting the modified set...")
print(Months)

Output:

printing the original set ...


{'January', 'May', 'June', 'April', 'March', 'February'}

Removing all the items from the set...

Printing the modified set...


set()

Difference between discard() and remove()


Despite the fact that discard() and remove() method both perform the same task, There is one main difference
between discard() and remove().

If the key to be deleted from the set using discard() doesn't exist in the set, the Python will not give the error. The
program maintains its control flow.

On the other hand, if the item to be deleted from the set using remove() doesn't exist in the set, the Python will
raise an error.

Consider the following example.


Example-
Months = set(["January","February", "March", "April", "May", "June"])
print("\nprinting the original set ... ")
print(Months)
print("\nRemoving items through discard() method...");
Months.discard("Feb"); #will not give an error although the key feb is not available in the
set
print("\nprinting the modified set...")
print(Months)
print("\nRemoving items through remove() method...");
Months.remove("Jan") #will give an error as the key jan is not available in the set.
print("\nPrinting the modified set...")
print(Months)

Output:

printing the original set ...


{'March', 'January', 'April', 'June', 'February', 'May'}

Removing items through discard() method...

printing the modified set...


{'March', 'January', 'April', 'June', 'February', 'May'}

Removing items through remove() method...


Traceback (most recent call last):
File "set.py", line 9, in
Months.remove("Jan")
KeyError: 'Jan'

Python Set Operations


Set can be performed mathematical operation such as union, intersection, difference, and symmetric difference.
Python provides the facility to carry out these operations with operators or methods. We describe these
operations as follows.

Union of two Sets

The union of two sets is calculated by using the pipe (|) operator. The union of the two sets contains all the items
that are present in both the sets.
Consider the following example to calculate the union of two sets.

Example 1: using union | operator

Days1 = {"Monday","Tuesday","Wednesday","Thursday", "Sunday"}


Days2 = {"Friday","Saturday","Sunday"}
print(Days1|Days2) #printing the union of the sets

Output:

{'Friday', 'Sunday', 'Saturday', 'Tuesday', 'Wednesday', 'Monday',


'Thursday'}

Python also provides the union() method which can also be used to calculate the union of two sets. Consider the
following example.

Example 2: using union() method

Days1 = {"Monday","Tuesday","Wednesday","Thursday"}
Days2 = {"Friday","Saturday","Sunday"}
print(Days1.union(Days2)) #printing the union of the sets

Output:

{'Friday', 'Monday', 'Tuesday', 'Thursday', 'Wednesday', 'Sunday',


'Saturday'}

More Methods (pop(), clear(), copy())

Python Set pop() Method


Python pop() method pops an element from the set. It does not take any argument but returns the popped
element. It raises an error if the element is not present in the set. See the examples and signature of the method
is given below.

Signature

pop()

Parameters
No parameter.

Return
It returns deleted element or throws an error if the set is empty.

Let's see some examples of pop() method to understand it's functionality.


Python Set pop() Method Example
A simple example to use pop() method which removes an element and modifies the set.

# Python set pop() Method


# Creating a set
set = {1,2,3,4,5}
# Displaying elements
print(set)
# Calling function
el = set.pop()
print("Element popped:",el)
print("Remaining elements: ",set)
el = set.pop()
print("Element popped:",el)
print("Remaining elements: ",set)
el = set.pop()
print("Element popped:",el)
print("Remaining elements: ",set)
el = set.pop()
print("Element popped:",el)
print("Remaining elements: ",set)

Output:

{1, 2, 3, 4, 5}
Element popped: 1
Remaining elements: {2, 3, 4, 5}
Element popped: 2
Remaining elements: {3, 4, 5}
Element popped: 3
Remaining elements: {4, 5}
Element popped: 4
Remaining elements: {5}

If the set is empty, it throws an error KeyError to the caller function. See the example.

# Python set pop() Method


# Creating a set
set = {1,2}
# Displaying elements
print(set)
# Calling function
el = set.pop()
print("Element popped:",el)
print("Remaining elements: ",set)
el = set.pop()
print("Element popped:",el)
print("Remaining elements: ",set)
el = set.pop()
print("Element popped:",el)
print("Remaining elements: ",set)

Output:

{1, 2}
Element popped: 1
Remaining elements: {2}
Element popped: 2
Remaining elements: set()
Traceback (most recent call last):
File "main.py", line 13, in
el = set.pop()
KeyError: 'pop from an empty set'

Python Set copy() Method

The copy() method returns a shallow copy of the set in python. If we use “=” to copy a set to
another set, when we modify in the copied set, the changes are also reflected in the original set. So
we have to create a shallow copy of the set such that when we modify something in the copied set,
changes are not reflected back in the original set. Syntax:
set_name.copy()

set_name: Name of the set whose copy


we want to generate.

Parameters:The copy() method for sets doesn’t take any parameters. Return value:The function
returns a shallow copy of the original set. Below is the implementation of the above function:

set1 = {1, 2, 3, 4}

set2 = set1.copy()

print(set2)

Output:
{1, 2, 3, 4}

first = {'g', 'e', 'e', 'k', 's'}


second = first.copy()

# before adding
print 'before adding: '
print 'first: ',first
print 'second: ', second

# Adding element to second, first does not


# change.
second.add('f')

# after adding
print 'after adding: '
print 'first: ', first
print 'second: ', second

Output:

before adding:
first: set(['s', 'e', 'k', 'g'])
second: set(['s', 'e', 'k', 'g'])
after adding:
first: set(['s', 'e', 'k', 'g'])
second: set(['s', 'e', 'k', 'g', 'f'])

Python Set clear() Method


Python Set clear() method removes all elements from the set.

Python Set clear() Method Syntax:


Syntax: set.clear()
parameters:
 The clear() method doesn’t take any parameters.
Return: None

Example 1: Python Set clear() Method Example

test_set = {1, 2, 3, 4}
test_set.clear()
print("After clear() on test_set:", test_set)

Output:
After clear() on test_set: set()

Example 2: Python Set clear() Method on a Set of Strings

# set of letters
GEEK = {"A", "B", "C"}
print('GEEK before clear:', GEEK)

# clearing vowels
GEEK.clear()
print('GEEK after clear:', GEEK)
Output:
GEEK before clear: {'B', 'C', 'A'}
GEEK after clear: set()

Python Dictionary
Python dictionary is an ordered collection (starting from Python 3.7) of items. It stores elements
in key/value pairs. Here, keys are unique identifiers that are associated with each value.
Let's see an example,

If we want to store information about countries and their capitals, we can create a dictionary with
country names as keys and capitals as values.
Keys Values

Nepal Kathmandu

Italy Rome

England London

Create a dictionary in Python


Here's how we can create a dictionary in Python.
capital_city = {"Nepal": "Kathmandu", "Italy": "Rome", "England": "London"}

print(capital_city)

Run Code

Output

{'Nepal': 'Kathmandu', 'Italy': 'Rome', 'England': 'London'}

In the above example, we have created a dictionary named capital_city. Here,

Keys are "Nepal", "Italy", "England"

Values are "Kathmandu", "Rome", "London"

Note: Here, keys and values both are of string type. We can also have keys and values of
different data types.
Example 1: Python Dictionary
# dictionary with keys and values of different data types
numbers = {1: "One", 2: "Two", 3: "Three"}
print(numbers)
Output

[3: "Three", 1: "One", 2: "Two"]

In the above example, we have created a dictionary named numbers. Here, keys are of integer
type and values are of string type.
Add Elements to a Python Dictionary
We can add elements to a dictionary using the name of the dictionary with []. For example,
capital_city = {"Nepal": "Kathmandu", "England": "London"}
print("Initial Dictionary: ",capital_city)

capital_city["Japan"] = "Tokyo"

print("Updated Dictionary: ",capital_city)

Output

Initial Dictionary: {'Nepal': 'Kathmandu', 'England': 'London'}


Updated Dictionary: {'Nepal': 'Kathmandu', 'England': 'London', 'Japan': 'Tokyo'}

In the above example, we have created a dictionary named capital_city. Notice the line,

capital_city["Japan"] = "Tokyo"

Here, we have added a new element to capital_city with key: Japan and value: Tokyo.
Change Value of Dictionary
We can also use [] to change the value associated with a particular key. For example,

student_id = {111: "Eric", 112: "Kyle", 113: "Butters"}


print("Initial Dictionary: ", student_id)

student_id[112] = "Stan"

print("Updated Dictionary: ", student_id)


Output

Initial Dictionary: {111: 'Eric', 112: 'Kyle', 113: 'Butters'}


Updated Dictionary: {111: 'Eric', 112: 'Stan', 113: 'Butters'}

In the above example, we have created a dictionary named student_id. Initially, the value
associated with the key 112 is "Kyle". Now, notice the line,

student_id[112] = "Stan"

Here, we have changed the value associated with the key 112 to "Stan".
Accessing Elements from Dictionary
In Python, we use the keys to access their corresponding values. For example,
student_id = {111: "Eric", 112: "Kyle", 113: "Butters"}

print(student_id[111]) # prints Eric


print(student_id[113]) # prints Butters
Here, we have used the keys to access their corresponding values.

If we try to access the value of a key that doesn't exist, we'll get an error. For example,
student_id = {111: "Eric", 112: "Kyle", 113: "Butters"}
print(student_id[211])

# Output: KeyError: 211

Removing elements from Dictionary


We use the del statement to remove an element from the dictionary. For example,
student_id = {111: "Eric", 112: "Kyle", 113: "Butters"}

print("Initial Dictionary: ", student_id)

del student_id[111]

print("Updated Dictionary ", student_id)

Output

Initial Dictionary: {111: 'Eric', 112: 'Kyle', 113: 'Butters'}


Updated Dictionary {112: 'Kyle', 113: 'Butters'}

Here, we have created a dictionary named student_id. Notice the code,

del student_id[111]

The del statement removes the element associated with the key 111.
We can also delete the whole dictionary using the del statement,

student_id = {111: "Eric", 112: "Kyle", 113: "Butters"}

# delete student_id dictionary


del student_id

print(student_id)

# Output: NameError: name 'student_id' is not defined


We are getting an error message because we have deleted the student_id dictionary
and student_id doesn't exist anymore.
Python Dictionary clear()
The clear() method removes all items from the dictionary.
# dictionary
numbers = {1: "one", 2: "two"}

# removes all the items from the dictionary


numbers.clear()

print(numbers)

# Output: {}

clear() Syntax
The syntax of the clear() method is:

dictionary.clear()

Here, clear() removes all the items present in the dictionary.

Example : Python Dictionary clear()


cityTemperature = {"New York": 18, "Texas": 26}

print("Dictionary before clear():", cityTemperature)

# removes all the items from the dictionary


cityTemperature.clear()

print("Dictionary after clear():", cityTemperature)

Output

Dictionary before clear(): {'New York': 18, 'Texas': 26}


Dictionary after clear(): {}

In the above example, we have used the clear() method to remove all the
items from the dictionary cityTemperature.
Here, the method removes every item from {"New York": 18, "Texas":

26} and returns an empty dictionary {}.


Python Dictionary copy()
In this tutorial, we will learn about the Python dictionary copy() method with the help of examples.

original_marks = {'Physics':67, 'Maths':87}

copied_marks = original_marks.copy()

print('Original Marks:', original_marks)


print('Copied Marks:', copied_marks)

# Output: Original Marks: {'Physics': 67, 'Maths': 87}


# Copied Marks: {'Physics': 67, 'Maths': 87}

Syntax of Dictionary copy()


The syntax of copy() is:

dict.copy()

copy() Arguments
The copy() method doesn't take any arguments.

copy() Return Value


This method returns a shallow copy of the dictionary. It doesn't modify the original dictionary.

original = {1:'one', 2:'two'}


new = original.copy()

print('Orignal: ', original)


print('New: ', new)

Output

Orignal: {1: 'one', 2: 'two'}


New: {1: 'one', 2: 'two'}
Dictionary copy() Method Vs = Operator
When the copy() method is used, a new dictionary is created which is filled with a copy of the
references from the original dictionary.
When the = operator is used, a new reference to the original dictionary is create

original = {1:'one', 2:'two'}


new = original

# removing all elements from the list


new.clear()

print('new: ', new)


print('original: ', original)

Output

new: {}
original: {}

Here, when the new dictionary is cleared, the original dictionary is also
cleared.

Python Dictionary get()


In this tutorial, we will learn about the Python Dictionary get() method with the help of examples.

The get() method returns the value for the specified key if the key is in the dictionary

marks = {'Physics':67, 'Maths':87}


print(marks.get('Physics'))

# Output: 67
Syntax of Dictionary get()
The syntax of get() is:

dict.get(key[, value])

get() Parameters
get() method takes maximum of two parameters:
 key - key to be searched in the dictionary
 value (optional) - Value to be returned if the key is not found. The default value is None.

Return Value from get()


get() method returns:
 the value for the specified key if key is in the dictionary.
 None if the key is not found and value is not specified.
 value if the key is not found and value is specified.

Example 1: How does get() work for dictionaries?


person = {'name': 'Phill', 'age': 22}

print('Name: ', person.get('name'))

print('Age: ', person.get('age'))

# value is not provided


print('Salary: ', person.get('salary'))

# value is provided
print('Salary: ', person.get('salary', 0.0))
Output

Name: Phill
Age: 22
Salary: None
Salary: 0.0

Python get() method Vs dict[key] to Access Elements


get() method returns a default value if the key is missing.
However, if the key is not found when you use dict[key] , KeyError exception is raised.

person = {}

# Using get() results in None


print('Salary: ', person.get('salary'))

# Using [] results in KeyError


print(person['salary'])
Output

Salary: None
Traceback (most recent call last):
File "", line 7, in
print(person['salary'])
KeyError: 'salary'

Python Dictionary pop()


In this tutorial, we will learn about the Python Dictionary pop() method with the help of examples.

The pop() method removes and returns an element from a dictionary having the given key.

# create a dictionary
marks = { 'Physics': 67, 'Chemistry': 72, 'Math': 89 }

element = marks.pop('Chemistry')

print('Popped Marks:', element)

# Output: Popped Marks: 72


Syntax of Dictionary pop()
The syntax of pop() method is

dictionary.pop(key[, default])

pop() Parameters
pop() method takes two parameters:
 key - key which is to be searched for removal
 default - value which is to be returned when the key is not in the dictionary

Return value from pop()


The pop() method returns:
 If key is found - removed/popped element from the dictionary
 If key is not found - value specified as the second argument (default)
 If key is not found and default argument is not specified - KeyError exception is raised

Example 1: Pop an element from the dictionary


# random sales dictionary
sales = { 'apple': 2, 'orange': 3, 'grapes': 4 }

element = sales.pop('apple')

print('The popped element is:', element)


print('The dictionary is:', sales)

Output

The popped element is: 2


The dictionary is: {'orange': 3, 'grapes': 4}

Example 2: Pop an element not present from the dictionary


# random sales dictionary
sales = { 'apple': 2, 'orange': 3, 'grapes': 4 }

element = sales.pop('guava')

Output

KeyError: 'guava'

Example 3: Pop an element not present from the dictionary,


provided a default value

# random sales dictionary


sales = { 'apple': 2, 'orange': 3, 'grapes': 4 }

element = sales.pop('guava', 'banana')

print('The popped element is:', element)


print('The dictionary is:', sales)

Output

The popped element is: banana


The dictionary is: {'orange': 3, 'apple': 2, 'grapes': 4}

Python Dictionary popitem()


The Python popitem() method removes and returns the last element (key, value) pair inserted
into the dictionary.
The syntax of popitem() is:

dict.popitem()

Parameters for popitem() method


The popitem() doesn't take any parameters.

Return Value from popitem() method


The popitem() method removes and returns the (key, value) pair from the dictionary in the Last In,
First Out (LIFO) order.
 Returns the latest inserted element (key,value) pair from the dictionary.
 Removes the returned element pair from the dictionary.

Note: Before Python 3.7, the popitem() method returned and removed an arbitrary element (key,
value) pair from the dictionary.

Example: Working of popitem() method


person = {'name': 'Phill', 'age': 22, 'salary': 3500.0}

# ('salary', 3500.0) is inserted at the last, so it is removed.


result = person.popitem()

print('Return Value = ', result)


print('person = ', person)

# inserting a new element pair


person['profession'] = 'Plumber'

# now ('profession', 'Plumber') is the latest element


result = person.popitem()

print('Return Value = ', result)


print('person = ', person)
Output

Return Value = ('salary', 3500.0)


person = {'name': 'Phill', 'age': 22}
Return Value = ('profession', 'Plumber')
person = {'name': 'Phill', 'age': 22}

Note: The popitem() method raises a KeyError error if the dictionary is empty.

Introduction to Numpy and Pandas

Overview of numpy

What is NumPy in Python?


NumPy in Python is a library that is used to work with arrays and was created in
2005 by Travis Oliphant. NumPy library in Python has functions for working in
domain of Fourier transform, linear algebra, and matrices. Python NumPy is an
open-source project that can be used freely. NumPy stands for Numerical Python.

How to install NumPy Python?


Installing the NumPy library is a straightforward process. You can use pip to install
the library.Go to the command line and type the following:

pip install numpy

If you are using Anaconda distribution, then you can use conda to
install NumPy. conda install numpy

Once the installation is complete, you can verify it by importing


the NumPy library in the python interpreter. One can use the
numpy library by importing it as shown below.

import numpy

If the import is successful, then you will see the following


output.

>>> import numpy

>>> numpy.__version__
'1.17.2'

NumPy is a library for the Python programming language, and it’s specifically
designed to help you work with data.

With NumPy, you can easily create arrays, which is a data structure that allows you
to store multiple values in a single variable.

In particular, NumPy arrays provide an efficient way of storing and manipulating


data.NumPy also includes a number of functions that make it easy to perform
mathematical operations on arrays. This can be really useful for scientific or
engineering applications. And if you’re working with data from a Python script, using
NumPy can make your life a lot easier.

Let us take a look at how to create NumPy arrays, copy and view arrays, reshape
arrays, and iterate over arrays.

NumPy Creating Arrays


Arrays are different from Python lists in several ways. First, NumPy arrays are multi-
dimensional, while Python lists are one-dimensional. Second, NumPy arrays are
homogeneous, while Python lists are heterogeneous. This means that all the
elements of a NumPy array must be of the same type. Third, NumPy arrays are
more efficient than Python lists.NumPy arrays can be created in several ways. One
way is to create an array from a Python list. Once you have created a NumPy array,
you can manipulate it in various ways. For example, you can change the shape of an
array, or you can index into an array to access its elements. You can also perform
mathematical operations on NumPy arrays, such as addition, multiplication, and
division.

One has to import the library in the program to use it. The module NumPy has an
array function in it which creates an array.

Creating an Array:

import numpy as np

arr = np.array([1, 2, 3, 4, 5])

print(arr)

Output:

[1 2 3 4 5]
We can also pass a tuple in the array function to create an array. 2

(note you can also use from numpy import *0

import numpy as np

arr = np.array((1, 2, 3, 4, 5))

print(arr)

The output would be similar to the above case.

Dimensions- Arrays:

0-D Arrays:

The following code will create a zero-dimensional array with a value 36.

import numpy as np

arr = np.array(36)

print(arr)

Output:

36

1-Dimensional Array:

The array that has Zero Dimensional arrays as its elements is a uni-dimensional or
1-D array.

The code below creates a 1-D array,

import numpy as np

arr = np.array([1, 2, 3, 4, 5])

print(arr)

Output:
[1 2 3 4 5]

Two Dimensional Arrays:

2-D Arrays are the ones that have 1-D arrays as its element. The following code will
create a 2-D array with 1,2,3 and 4,5,6 as its values. We can imagine 2d array as
combination of several 1d array.

import numpy as np

arr1 = np.array([[1, 2, 3], [4, 5, 6]])

print(arr1)

Output:

[[1 2 3]

[4 5 6]]

Three Dimensional Arrays:

Let us see an example of creating a 3-D array with two 2-D arrays:we can imagine
3d array to be combination of several 2d array.

import numpy as np

arr1 = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])


print(arr1)

Output:

[[[1 2 3]

[4 5 6]]

[[1 2 3]

[4 5 6]]]

To identify the dimensions of the array, we can use ndim as shown below:
import numpy as np
a = np.array(36)
d = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])
print(a.ndim)
print(d.ndim)

Output:
0
3

Numpy methods (Mean, Median, Mode,Standard Deviation and


Variance)

Introduction
While doing your data science or machine learning projects, you would
often be required to carry out some statistical operations. In this tutorial,
we will cover numpy statistical functions numpy mean, numpy mode,
numpy median and numpy standard deviation. All of
these statistical functions help in better understanding of data and also
facilitates in deciding what actions should be taken further on data.

Importing Numpy Library


We will start with the import of numpy library

In [1]:

import numpy as np
Commencing this tutorial with the mean function.

Mean, Median, and Mode


What can we learn from looking at a group of numbers?
In Machine Learning (and in mathematics) there are often three values
that interests us:

 Mean - The average value


 Median - The mid point value
 Mode - The most common value

Example: We have registered the speed of 13 cars:

speed = [99,86,87,88,111,86,103,87,94,78,77,85,86]

What is the average, the middle, or the most common speed value?
Mean

The mean value is the average value.

To calculate the mean, find the sum of all values, and divide the sum by
the number of values:

(99+86+87+88+111+86+103+87+94+78+77+85+86) / 13 = 89.77

Use the NumPy mean() method to find the average speed:

import numpy

speed = [99,86,87,88,111,86,103,87,94,78,77,85,86]

x = numpy.mean(speed)

print(x)

Median
The median value is the value in the middle, after you have sorted all the
values:

77, 78, 85, 86, 86, 86, 87, 87, 88, 94, 99, 103, 111

It is important that the numbers are sorted before you can find the
median.

The NumPy module has a method for this:

ExampleGet your own Python Server

Use the NumPy median() method to find the middle value:

import numpy

speed = [99,86,87,88,111,86,103,87,94,78,77,85,86]

x = numpy.median(speed)

print(x)

If there are two numbers in the middle, divide the sum of those numbers
by two.
77, 78, 85, 86, 86, 86, 87, 87, 94, 98, 99, 103

(86 + 87) / 2 = 86.5

ExampleGet your own Python Server

Using the NumPy module:

import numpy

speed = [99,86,87,88,86,103,87,94,78,77,85,86]

x = numpy.median(speed)

print(x)

Mode

The Mode value is the value that appears the most number of times:

99, 86, 87, 88, 111, 86, 103, 87, 94, 78, 77, 85, 86 = 86

The SciPy module has a method for this. Learn about the SciPy module in
our SciPy Tutorial.

Example

Use the SciPy mode() method to find the number that appears the most:

from scipy import stats

speed = [99,86,87,88,111,86,103,87,94,78,77,85,86]

x = stats.mode(speed)

print(x)

What is Standard Deviation?

Standard deviation is a number that describes how spread out the values
are.
A low standard deviation means that most of the numbers are close to the
mean (average) value.

A high standard deviation means that the values are spread out over a
wider range.

Example: This time we have registered the speed of 7 cars:

speed = [86,87,88,86,87,85,86]

The standard deviation is:

0.9

Meaning that most of the values are within the range of 0.9 from the
mean value, which is 86.4.

Let us do the same with a selection of numbers with a wider range:

speed = [32,111,138,28,59,77,97]

The standard deviation is:

37.85

Meaning that most of the values are within the range of 37.85 from the
mean value, which is 77.4.

As you can see, a higher standard deviation indicates that the values are
spread out over a wider range.

The NumPy module has a method to calculate the standard deviation:

Use the NumPy std() method to find the standard deviation:

import numpy

speed = [86,87,88,86,87,85,86]

x = numpy.std(speed)

print(x)
import numpy

speed = [32,111,138,28,59,77,97]

x = numpy.std(speed)
print(x)
Variance

Variance is another number that indicates how spread out the values are.

In fact, if you take the square root of the variance, you get the standard
deviation!

Or the other way around, if you multiply the standard deviation by itself,
you get the variance!

To calculate the variance you have to do as follows:

1. Find the mean:

(32+111+138+28+59+77+97) / 7 = 77.4

2. For each value: find the difference from the mean:

32 - 77.4 = -45.4
111 - 77.4 = 33.6
138 - 77.4 = 60.6
28 - 77.4 = -49.4
59 - 77.4 = -18.4
77 - 77.4 = - 0.4
97 - 77.4 = 19.6

3. For each difference: find the square value:

(-45.4)2 = 2061.16
(33.6)2 = 1128.96
(60.6)2 = 3672.36
(-49.4)2 = 2440.36
(-18.4)2 = 338.56
(- 0.4)2 = 0.16
(19.6)2 = 384.16

4. The variance is the average number of these squared differences:

(2061.16+1128.96+3672.36+2440.36+338.56+0.16+384.16) / 7 = 143
2.2

Luckily, NumPy has a method to calculate the variance:

Use the NumPy var() method to find the variance:

import numpy
speed = [32,111,138,28,59,77,97]

x = numpy.var(speed)

print(x)
Standard Deviation

As we have learned, the formula to find the standard deviation is the


square root of the variance:

√1432.25 = 37.85

Or, as in the example from before, use the NumPy to calculate the
standard deviation:

Use the NumPy std() method to find the standard deviation:

import numpy

speed = [32,111,138,28,59,77,97]

x = numpy.std(speed)

print(x)

5.2.2 Pandas Dataframe:


5.2.2.1 Creating dataframe using list
5.2.2.2 Creating dataframe using dict of equal length list
5.2.2.3 Reading data using csv file (read_csv())
5.2.2.4 Retrieving rows and columns from dataframe using index
5.2.2.5 Retrieving rows and columns using loc and iloc functions.

Python Pandas

Python Pandas is defined as an open-source library that provides high-performance data


manipulation in Python. This tutorial is designed for both beginners and professionals.

It is used for data analysis in Python and developed by Wes McKinney in 2008. Our Tutorial
provides all the basic and advanced concepts of Python Pandas, such as Numpy, Data
operation and Time Series

Python Pandas Introduction

Pandas is defined as an open-source library that provides high-performance data


manipulation in Python. The name of Pandas is derived from the word Panel Data, which
means an Econometrics from Multidimensional data. It is used for data analysis in Python
and developed by Wes McKinney in 2008.

Data analysis requires lots of processing, such as restructuring, cleaning or merging, etc.
There are different tools are available for fast data processing, such as Numpy, Scipy,
Cython, and Panda. But we prefer Pandas because working with Pandas is fast, simple and
more expressive than other tools.

Pandas is built on top of the Numpy package, means Numpy is required for operating the
Pandas.

Before Pandas, Python was capable for data preparation, but it only provided limited support
for data analysis. So, Pandas came into the picture and enhanced the capabilities of data
analysis. It can perform five significant steps required for processing and analysis of data
irrespective of the origin of the data, i.e., load, manipulate, prepare, model, and analyze.

Key Features of Pandas

o It has a fast and efficient DataFrame object with the default and customized indexing.
o Used for reshaping and pivoting of the data sets.
o Group by data for aggregations and transformations.
o It is used for data alignment and integration of the missing data.
o Provide the functionality of Time Series.
o Process a variety of data sets in different formats like matrix data, tabular
heterogeneous, time series.
o Handle multiple operations of the data sets such as subsetting, slicing, filtering,
groupBy, re-ordering, and re-shaping.
o It integrates with the other libraries such as SciPy, and scikit-learn.
o Provides fast performance, and If you want to speed it, even more, you can use
the Cython.

Benefits of Pandas

The benefits of pandas over using other language are as follows:

o Data Representation: It represents the data in a form that is suited for data analysis
through its DataFrame and Series.
o Clear code: The clear API of the Pandas allows you to focus on the core part of the
code. So, it provides clear and concise code for the user.

Python Pandas Data Structure

The Pandas provides two data structures for processing the data, i.e., Series and DataFrame,
which are discussed below:
1) Series (Not in Syllabus)

It is defined as a one-dimensional array that is capable of storing various data types. The row
labels of series are called the index. We can easily convert the list, tuple, and dictionary into
series using "series' method. A Series cannot contain multiple columns. It has one parameter:

Data: It can be any list, dictionary, or scalar value.

Creating Series from Array:

Before creating a Series, Firstly, we have to import the numpy module and then use array()
function in the program.

1. import pandas as pd
2. import numpy as np
3. info = np.array(['P','a','n','d','a','s'])
4. a = pd.Series(info)
5. print(a)

Output

0 P
1 a
2 n
3 d
4 a
5 s
dtype: object

Explanation: In this code, firstly, we have imported the pandas and numpy library with
the pd and np alias. Then, we have taken a variable named "info" that consist of an array of
some values. We have called the info variable through a Series method and defined it in an
"a" variable. The Series has printed by calling the print(a) method.

Python Pandas DataFrame

It is a widely used data structure of pandas and works with a two-dimensional array with
labeled axes (rows and columns). DataFrame is defined as a standard way to store data and
has two different indexes, i.e., row index and column index. It consists of the following
properties:

o The columns can be heterogeneous types like int, bool, and so on.
o It can be seen as a dictionary of Series structure where both the rows and columns are
indexed. It is denoted as "columns" in case of columns and "index" in case of rows.

Create a DataFrame using List:

We can easily create a DataFrame in Pandas using list.


1. import pandas as pd
2. # a list of strings
3. x = ['Python', 'Pandas']
4.
5. # Calling DataFrame constructor on list
6. df = pd.DataFrame(x)
7. print(df)

Output

0
0 Python
1 Pandas

Explanation: In this code, we have defined a variable named "x" that consist of string
values. The DataFrame constructor is being called on a list to print the values.

Creating dataframe using dict of equal length list

Given a dictionary of equal length lists, task is to create a Pandas DataFrame from it.
There are various ways of creating a DataFrame in Pandas. One way is to convert a
dictionary containing lists of equal lengths as values. Let’s discuss how to create a Pandas
Dataframe from a dict of equal length lists with help of examples.
Example #1: Given a dictionary which contains format of cricket as keys and list of top five
teams as values.

# Import pandas package


import pandas as pd

# Define a dictionary containing ICC rankings


rankings = {'test': ['India', 'South Africa', 'England',
'New Zealand', 'Australia'],
'odi': ['England', 'India', 'New Zealand',
'South Africa', 'Pakistan'],
't20': ['Pakistan', 'India', 'Australia',
'England', 'New Zealand']}

# Convert the dictionary into DataFrame


rankings_pd = pd.DataFrame(rankings)

# Increment the index so that index


# starts at 1 (starts at 0 by default)
rankings_pd.index += 1

rankings_pd
Output:

Example #2: Given three lists test_batsmen, odi_batsmen, t20_batsmen. So we first need to
convert this data into a dictionary and then convert the dictionary into DataFrame.
# Import pandas package
import pandas as pd

# Lists of top 5 batsmen for each format


test_batsmen = ['Virat Kohli', 'Steve Smith', 'Kane Williamson', 'Joe Root', 'David Warner']

odi_batsmen = ['Virat Kohli', 'Rohit Sharma', 'Joe Root',


'David Warner', 'Babar Azam']
t20_batsmen = ['Babar Azam', 'Aaron Finch', 'Colin Munro',
'Lokesh Rahul', 'Fakhar Zaman']

# Define a dictionary containing ICC rankings for batsmen


rankings_batsmen = {'test': test_batsmen,
'odi': odi_batsmen,
't20': t20_batsmen}

# Convert the dictionary into DataFrame


rankings_batsmen_pd = pd.DataFrame(rankings_batsmen)

# Increment the index so that index


# starts at 1 (starts at 0 by default)
rankings_batsmen_pd.index += 1

rankings_batsmen_pd
Output:

Reading data using csv file (read_csv())

To access data from the CSV file, we require a function read_csv() that
retrieves data in the form of the Dataframe.

Syntax of read_csv()

Syntax:

pd.read_csv(filepath_or_buffer, sep=’ ,’ ,
header=’infer’, index_col=None, usecols=None, engine=None,
skiprows=None, nrows=None)

Parameters:
 filepath_or_buffer: It is the location of the file which is to be retrieved
using this function. It accepts any string path or URL of the file.
 sep: It stands for separator, default is ‘, ‘ as in CSV(comma separated
values).
 header: It accepts int, a list of int, row numbers to use as the column
names, and the start of the data. If no names are passed, i.e.,
header=None, then, it will display the first column as 0, the second as 1,
and so on.
 usecols: It is used to retrieve only selected columns from the CSV file.
 nrows: It means a number of rows to be displayed from the dataset.
 index_col: If None, there are no index numbers displayed along with
records.
 skiprows: Skips passed rows in the new data frame.
Read CSV using Pandas read_csv
Before using this function, we must import the Pandas library, we will load
the CSV file.
# Import pandas
import pandas as pd

# reading csv file


pd.read_csv("example1.csv")

Example 1: Using sep in read_csv()

In this example, we will manipulate our existing CSV file and then add some
special characters to see how the sep parameter works.

# headbrain1 = "totalbill_tip, sex:smoker,


day_time, size
# 16.99, 1.01:Female|No, Sun, Dinner, 2
# 10.34, 1.66, Male, No|Sun:Dinner, 3
# 21.01:3.5_Male, No:Sun, Dinner, 3
#23.68, 3.31, Male|No, Sun_Dinner, 2
# 24.59:3.61, Female_No, Sun, Dinner, 4
# 25.29, 4.71|Male, No:Sun, Dinner, 4"

# Importing pandas library


import pandas as pd

# Load the data of csv


df = pd.read_csv('headbrain1.csv',
sep='[:, |_]',
engine='python')

# Print the Dataframe


df
(to display all coloumns: pd.set_option(‘display.max_columns’,None)
Output:

Example 2: Using usecols in read_csv()

Here, we are specifying only 3 columns,i.e.[“tip”, “sex”, “time”] to load and we


use the header 0 as its default header

df = pd.read_csv('example1.csv',
header=0,
usecols=["tip", "sex", "time"])

df
Example 3: Using index_col in read_csv()

Here, we use the “sex” index first and then the “tip” index, we can simply
reindex the header with index_col parameter.

# headbrain1 = "totalbill_tip, sex:smoker,


day_time, size
# 16.99, 1.01:Female|No, Sun, Dinner, 2
# 10.34, 1.66, Male, No|Sun:Dinner, 3
# 21.01:3.5_Male, No:Sun, Dinner, 3
#23.68, 3.31, Male|No, Sun_Dinner, 2
# 24.59:3.61, Female_No, Sun, Dinner, 4
# 25.29, 4.71|Male, No:Sun, Dinner, 4"

# Importing pandas library


import pandas as pd
# Load the data of csv
df = pd.read_csv('headbrain1.csv',
sep='[:, |_]',
engine='python')

# Print the Dataframe


df

Output:

Example 2: Using usecols in read_csv()

Here, we are specifying only 3 columns,i.e.[“tip”, “sex”, “time”] to load and we


use the header 0 as its default header.

df = pd.read_csv('example1.csv',
header=0,
usecols=["tip", "sex", "time"])

df

Output:
Example 3: Using index_col in read_csv()

Here, we use the “sex” index first and then the “tip” index, we can simply
reindex the header with index_col parameter.

df = pd.read_csv('example1.csv',
header=0,
index_col=["sex", "tip"],
usecols=["tip", "sex", "time"])

df
Output:
Example 4: Using nrows in read_csv()
Here, we just display only 5 rows using nrows parameter.

df = pd.read_csv('example1.csv',
header=0,
index_col=["tip", "sex"],
usecols=["tip", "sex", "time"],
nrows=5)

df

Output:
Example 5: Using skiprows in read_csv()
The skiprows help to skip some rows in CSV, i.e, here you will observe that
the upper row and the last row from the original CSV data have been
skipped.

pd.read_csv("example1.csv", skiprows =
[1,12])
Output:
Pandas DataFrame.loc[]

Pandas DataFrame is a two-dimensional size-mutable, potentially heterogeneous


tabular data structure with labeled axes (rows and columns). Arithmetic operations
align on both row and column labels. It can be thought of as a dict-like container for
Series objects. This is the primary data structure of the Pandas.

Pandas DataFrame.loc attribute access a group of rows and columns by label(s)


or a boolean array in the given DataFrame.

loc():
loc is label-based, which means that you have to specify rows and columns
based on their row and column labels.

Syntax:
loc[row_label, column_label]

Example #1: Use DataFrame.loc attribute to access a particular cell


in the given Dataframe using the index and column labels.

# importing pandas as pd

import pandas as pd

# Creating the DataFrame


df = pd.DataFrame({'Weight':[45, 88, 56, 15, 71],
'Name':['Sam', 'Andrea', 'Alex', 'Robin',
'Kia'],
'Age':[14, 25, 55, 8, 21]})

# Create the index


index_ = ['Row_1', 'Row_2', 'Row_3', 'Row_4', 'Row_5']

# Set the index


df.index = index_

# Print the DataFrame


print(df)
Output :

Now we will use DataFrame.loc attribute to return the value present in the ‘Name’ column corresponding to the ‘Row_2’ label.
# return the value
result = df.loc['Row_2', 'Name']

# Print the result


print(result)
Output :

As we can see in the output, the DataFrame.loc attribute has successfully returned the value present at the desired location in the
given DataFrame.
Example #2: Use DataFrame.loc attribute to return two of the column in the given Dataframe.

# importing pandas as pd
import pandas as pd

# Creating the DataFrame


df = pd.DataFrame({"A":[12, 4, 5, None, 1],
"B":[7, 2, 54, 3, None],
"C":[20, 16, 11, 3, 8],
"D":[14, 3, None, 2, 6]})

# Create the index


index_ = ['Row_1', 'Row_2', 'Row_3', 'Row_4', 'Row_5']

# Set the index


df.index = index_

# Print the DataFrame


print(df)
Output :
Now we will use DataFrame.loc attribute to return the values present in the ‘A’ and ‘D’ column of the Dataframe.

# return the values.


result = df.loc[:, ['A', 'D']]

# Print the result


print(result)
Output :

As we can see in the output, the DataFrame.loc attribute has successfully returned
the desired columns of the dataframe.
Python | Extracting rows using Pandas .iloc[]

Python is a great language for doing data analysis, primarily because of the
fantastic ecosystem of data-centric Python packages. Pandas is one of those
packages that makes importing and analyzing data much easier.
The Pandas library provides a unique method to retrieve rows from a Data
Frame. Dataframe.iloc[] method is used when the index label of a data frame is
something other than numeric series of 0, 1, 2, 3….n or in case the user doesn’t
know the index label. Rows can be extracted using an imaginary index position
which isn’t visible in the data frame.

iloc():
The Pandas library provides a unique method to retrieve rows from a Data
Frame. Dataframe.iloc[] method is used when the index label of a data
frame is something other than numeric series of 0, 1, 2, 3….n or in case the
user doesn’t know the index label. Rows can be extracted using an
imaginary index position which isn’t visible in the data frame.
Syntax:

iloc[row_position, column_position]

Example #1: Extracting single row and comparing with .loc[] In this example,
same index number row is extracted by both .iloc[] and.loc[] method and
compared. Since the index column by default is numeric, hence the index
label will also be integers.

# importing pandas package


import pandas as pd

# making data frame from csv file


data = pd.read_csv("nba.csv")

# retrieving rows by loc method


row1 = data.loc[3]

# retrieving rows by iloc method


row2 = data.iloc[3]

# checking if values are equal


row1 == row2

Output:
As shown in the output image, the results returned by both methods are the
same.

Output:
As shown in the output image, the results returned by both methods are the
same.
Example #2: Extracting multiple rows with index In this example, multiple
rows are extracted, first by passing a list and then by passing integers to
extract rows between that range. After that, both the values are compared.

# importing pandas package


import pandas as pd

# making data frame from csv file


data = pd.read_csv("nba.csv")

# retrieving rows by loc method


row1 = data.iloc[[4, 5, 6, 7]]

# retrieving rows by loc method


row2 = data.iloc[4:8]

# comparing values
row1 == row2

Output:
As shown in the output image, the results returned by both methods are the
same. All values are True except values in the college column since those
were NaN values.

iloc is integer position-based, so you have to specify rows and columns by their
integer position values (0-based integer position).

You might also like