Python Material 1
Python Material 1
Features-
● Emphasis on code readability.
● Automatic memory management like in Java.
● Dynamically typed.
● large library
● Multi-paradigm programming language (Object Oriented,
procedural etc.)
● Python is interactive interpreter it is easy to check Python
commands.
● Platform independent.
Python Library:
DISADVANTAGES:
-execution speed is generally slower. For application involving large
datasets, complex maths it is generally be efficient to use a
compiled language rather than compiled language.
-Protecting code is difficult-because python is interpreted it is very
difficult to hide code from prime eyes.
Installation of Python
To install Python, we must download the installation package of the
required version from the following link/URL given below:
https://www.python.org/downloads/
To write and run Python programs interactively, we can either use
the command line window or the IDLE.
IDLE is a simple Integrated Development Environment that comes
with Python.
The most important feature of IDLE is that it is a program that
allows the user to edit, run, browse and debug a Python program
from a single interface.
STARTING IDLE
From Start Menu, open IDLE as follows:
Start menu--> Apps by name--> IDLE(Python 3.6 32 bit)
Or
Click on the icon to start IDLE
Practical Implementation
First Python Program
Type the following code in any text editor or an IDE and save it as
helloWorld.py
print ("Hello world!")
Now at the command window, go to the location of this file. You can
use the cd command to change directory.
To run the script, type python helloWorld.py in the command
window.
We should be able to see the output as follows:
Hello world!
MORE COMMANDS
-> print ("Welcome to World of Python programming")
Welcome to Python Programming
->print(20*55)
1100
->print(22/7)
3.142857142857143
Exiting Python
In order to exit from python command, click Ctrl + Z and press
Enter key or type quit () or exit () function/statement and press
Enter key.
15 25
15 25
Variable Definition
In Python, a variable is created when you first assign a value
to it. It also means that a variable is not created until some
value is assigned to it.
Consider the following code:
>>> print(x)
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
print(x)
NameError: name 'x' is not defined
When you run the above code, it will produce an error for the
statement –name ‗x‘ is not defined.
4) After saving, run the exe file from the downloads and Setup
Progress
Python
Assignment Operator :
Syntax :
variable_that_holds_the_value = input(―<message to be displayed>‖)
Example :
school = input(―Write the name of your school : ‖)
Example :
type(school)
type(num)
Example :
print(―Welcome to ―, school)
Syntax for Input Function with predefined data type for numbers :
Variable = data-type(input(―<message>‖))
Example :
a = int(input(―Enter any number : ―)
[ If the user inputs a decimal number then it will show an
error, as ‗a‘ is defined
as an integer. ]
type(a)
type(b)
type(c)
Write a code in Python that accepts two integers from user and
print their sum.
Unsolved :
Q.1.
Create following Variables
i) ―mystring‟ to contain ―Hello‟
ii) ―myfloat‟ to contain ―2.5‟
iii) ―myint‟ to contain ―10‟
Q.2.
If the value of a = 20 and b = 20, then a+=b will assign ________ to
a.
● Assignment Operators
● Logical Operators
● Bitwise Operators
● Membership Operators
● Identity Operators
Let us have a look on all the operators one by one.
Python Arithmetic Operators
Assume variable a holds 10 and variable b holds 20, then –
-11.0//3 = -
4.0
c /= a is
equivalent
to c = c /
Precedence of operators–
•Precedence—when an expression contains two different kinds of
operators, which should be applied first is known as operator
precedence.
• Associativity—when an expression contains two operators with the
same precedence,
which should be applied first is known as associativity.
The operators can be listed from high precedence to low precedence
as follows:
Operator Description Associativity
Exponentiation (raise to the Right to
**
power) Left
+,- unary plus and minus Left to Right
Multiply, divide, modulo and floor Left to Right
* , /, %, //
division
+,- Addition and subtraction Left to Right
<, <=, >, >= Comparison operators Left to Right
Example:
X = 2* 3 ** 2 / 5 + 10 //3 - 1
Ans: 2* 3 ** 2/ 5 + 10 //3 - 1
2* 9/ 5 + 10 //3 - 1
18 / 5 + 10 //3 - 1
3 + 10 // 3 – 1
3+3–1
6–1
Ans = 5
Expressions:
Example1:
>>> y = 3.14
>>> x = len("hello")
>>> print(x)
>>> print(y)
3.14
>>> y
3.14
(i) (ii) x = a3 + b3 + c3
2
√ 2
2
(iii) (iv)
2
Answer: 1. c = (a + b) / (2 *a)
11. Which one of the following have the highest precedence in the
expression?
a) Exponential
b) Addition
c) Multiplication
d) Parentheses
15. Which of the following operators has its associativity from right
to left?
a) +
b) //
c) %
d) **
a=2
b=3
c=2
if a == c and b != a or b == c:
print("if block: executed")
c=3
if c == 2:
print("if block: not executed")
The output of the following code will be
if block: executed
This happens because logical AND operator has more precedence
than logical OR operator. So a == cexpression is true and b != a is
also true. So, the result of logical AND operation is true. As one
variable of OR operation is true. So the result of Logical operation is
also true. And that why the statements under first if block
executed. So the value of variable c changes from 2 to 3. And, As
the value of C is not true. So the statement under second block
doesn‘t execute.
(A) 4.5
(B) 5
(C) 4
(D) 2
(A) 4.25
(B) 5
(C) 4
(D) 2
__________________________________________________________________
1.While loop
2.For loop
While Loop:-
It iterates till the condition is true:-
It is an entry controlled loop
i=1
while i < 6:
print(i)
i += 1
With the break statement we can stop the loop even if the while
condition is true:
Example:-
i=0
while i < 6:
i += 1
if i == 3:
continue
print(i)
FOR LOOP
A for loop is used for iterating over a sequence (that is either a list,
a tuple or a string or just a series of numbers).
With for loop we can execute a set of statements, once for each item
in a list, tuple, set etc
The for loop does not require an indexing variable to set beforehand,
as the for command itself allows for this.
Example :- range()
for x in range(6):
print(x)
Example Programs:-
1.Python Program to Calculate the Average of Numbers in a Given
List
2.Python Program to Reverse a Given Number
3.Python Program to Take in the Marks of 5 Subjects and Display
the Grade
4.Python Program to Read Two Numbers and Print Their Quotient
and Remainder
5.Python Program to Accept Three Digits and Print all Possible
Combinations from the Digits
6.Python Program to Print Odd Numbers Within a Given Range
7.Python Program to Find the Sum of Digits in a Number
8.Python Program to Find the Smallest Divisor of an Integer
9.Python Program to Count the Number of Digits in a Number
10.Python Program to Check if a Number is a Palindrome
11.Python Program to Read a Number n And Print the Series
"1+2+…..+n= "
12.Python Program to Read a Number n and Print the Natural
Numbers Summation Pattern
sum=0
temp= num
while temp>0:
digit = temp % 10
sum += digit**3
temp //=10
if num == sum:
else:
if num == sum:
print(num)
10. Write a Python program to find the sum of natural
numbers up to n where n is provided by user
if num < 0:
print("Enter a positive number")
else:
sum = 0
# use while loop to iterate un till zero
while(num > 0):
sum += num
num -= 1
print("The sum is",sum)
if x > y:
greater = x
while(True):
if((greater % x == 0) and (greater % y == 0)):
lcm = greater
break
greater += 1
return lcm
x= int(input(―enter no ―)
if x % i == 0:
print(i)
vowels = 'aeiou'
ip_str = ip_str.casefold()
count = 0
# count the vowels
for char in ip_str:
if char in count:
count[char] += 1
print(count)
22
333
4444
55555
*
**
***
****
*****
n=6
for i in range(1,n):
for j in range(i):
print(j, end=‘ ‗)
print()
count_even = 0
for x in range(1,101):
if not x % 2:
count_even+=1
else:
count_odd+=1
20. Write a Python program that accepts a string and calculate the
number of digits and letters.
s = input("Input a string")
d=l=0
for c in s:
if c.isdigit():
d=d+1
elif c.isalpha():
l=l+1
else:
pass
print("Letters", l)
print("Digits", d)
21 Write a Python program to find numbers between 100 and 400
(both included) where each digit of a number is an even number.
for i in range(100, 401):
s = str(i)
if (int(s[0])%2==0) and (int(s[1])%2==0) and (int(s[2])%2==0):
print( i))
TYPES OF ERRORS AND EXCEPTIONS IN PYTHON
Syntax Errors
When you forget a colon at the end of a line, accidentally add one
space too many when indenting under an if statement, or forget a
parenthesis, you will encounter a syntax error. This means that
Python couldn‘t figure out how to read your program. This is similar
to forgetting punctuation in English: for example, this text is
difficult to read there is no punctuation there is also no
capitalization why is this hard because you have to figure out where
each sentence ends you also have to figure out where each sentence
begins to some extent it might be ambiguous if there should be a
sentence break or not
defsome_function()
msg = "hello, world!"
print(msg)
returnmsg
File "<ipython-input-3-6bb841ea1423>", line 1
defsome_function()
^
SyntaxError: invalid syntax
Actually, the function above has two issues with syntax. If we fix
the problem with the colon, we see that there is also an
IndentationError, which means that the lines in the function
definition do not all have the same indentation:
defsome_function():
msg = "hello, world!"
print(msg)
returnmsg
File "<ipython-input-4-ae290e7659cb>", line 4
returnmsg
^
IndentationError: unexpected indent
Syntax errors are the most basic type of error. They arise when the
Python parser is unable to understand a line of code. Syntax errors
In IDLE, it will highlight where the syntax error is. Most syntax
errors are typos, incorrect indentation, or incorrect arguments. If
you get this error, try looking at your code for any of these.
Logic errors
These are the most difficult type of error to find, because they will
give unpredictable results and may crash your program. A lot of
different things can happen if you have a logic error. However these
are very easy to fix as you can use a debugger, which will run
through the program and fix any problems.
defsome_function():
msg = "hello, world!"
print(msg)
returnmsg
print(a)
---------------------------------------------------------------------------
NameErrorTraceback (most recent call last)
<ipython-input-7-9d7b17ad5387> in <module>()
----> 1 print(a)
Variable name errors come with some of the most informative error
messages, which are usually of the form ―name ‗the_variable_name‘
is not defined‖.
print(hello)
---------------------------------------------------------------------------
NameErrorTraceback (most recent call last)
<ipython-input-8-9553ee03b645> in <module>()
----> 1 print(hello)
The second is that you just forgot to create the variable before using
it. In the following example, count should have been defined (e.g.,
with count = 0) before the for loop:
Finally, the third possibility is that you made a typo when you were
writing your code. Let‘s say we fixed the error above by adding the
line Count = 0 before the for loop. Frustratingly, this actually does
not fix the error. Remember that variables are case-sensitive, so the
variable count is different from Count. We still get the same error,
because we still have not defined count:
Count = 0
for number in range(10):
count = count + number
print("The count is:", count)
---------------------------------------------------------------------------
NameErrorTraceback (most recent call last)
<ipython-input-10-d77d40059aea> in <module>()
1 Count = 0
2 for number in range(10):
----> 3 count = count + number
4 print("The count is:", count)
Index Errors
File Errors
The last type of error we‘ll cover today are those associated with
reading and writing files: FileNotFoundError. If you try to read a file
that does not exist, you will receive a FileNotFoundError telling you
so. If you attempt to write to a file that was opened read-only,
Python 3 returns an UnsupportedOperationError. More generally,
problems with input and output manifest as IOErrors or OSErrors,
depending on the version of Python you use.
See, now the output produced does not show the scary red-coloured
standard error message. It is now what you defined under the
exception block.
Exception Description
Name
EOFError Raised when one of the built-in functions (input())
hits an end-of-file condition (EOF) without reading
any data.
IOError Raised when an I/O operation (such as a print(),
the built-in open() function or a method of a file
object) fails for an I/O-related reason, e.g., ―file not
found‖ or ―disk full‖.
NameError Raised when an identifier name is not found.
IndexError Raised when a sequence subscript or index is out
of range, e.g., from a string of length 4 if you try to
read a value of index like 4 or more i.e., string[4],
string[5], string[-5] etc. will raise exception as legal
indexes for a string of length 4 are 0, 1, 2, 3 and -
1, -2, -3, -4 only.
ImportError Raised when an import statement fails to find the
module definition or when a from_import fails to
find a name that is to be imported.
TypeError Raised when an operation or function is applied to
an object of inappropriate type, e.g., if you try to
compute a square-root of a string value.
ValueError Raised when a built-in operation or function
receives an argument with inappropriate value
e.g., int(―z10‖) will raise ValueError.
OverflowError Raised when the second argument of a division or
modulo operation is zero.
KeyError Raised when the result of an arithmetic operation
is too large to be represented.
Raised when a mapping (dictionary) key is not
found in the set of existing keys.
Debugging Techniques
print(j**i)
When Python gives you a line number for the error, then:
(i) It means the error has manifested in this line.
(ii) Its origin may be in this line or in the lines above it.
So, start looking backwards from the line of error. Carefully reading
the statements and you will find the real cause of the error, e.g. for
the above error, the real cause of error is line2, where rather than
assigning the variable a, we assigned string ―a‖ to variable j.
1. a = int(input(―Enter a number ―))
2. j = ―a‖
3. for i in range(4): Error occurred because j was
mistakenly
And now, the code run perfectly fine and gives output as:
1
5
25
125
a=0
b=1
print(a)
print(b)
for i in range (5):
c = a+b
print(c, end = ― ‖)
b=c
a=b
0
1
1 2 4 8 16
for i in range(5):
c=a+b
print(―c= ―, c)
a=b
b=c The print() statements added to check on
print(―a= ―,a, end = ― ―) intermediate values of
variables a and b
print(―b= ―, b)
Now upon running the code, you can carefully look at the output
produced to figure out the error:
0
1
c=1
a= 1 b= 1
c= 2
a= 2 b= 2 As per Fibonacci logic, a should have
been 1 but it is 2,
c= 4 which means some problem with the
assignment: either
a= 4 b= 4 the assignment statement is incorrect or
the order of the
Now carefully look at the code. You will find out that the order of
the assignment statements is incorrect: variable b first loses its
values before assigning it to variable a, i.e.,:
a=b
b=c
a=0
b=1
print(a)
print(b)
for i in range(5) :
c = a+b
print( c )
a=b This change of order of statements will correct
the problem and print
b=c the correct Fibonacci terms
3 Code Tracing
Another useful technique is code tracing. Code tracking means
executing code one line at a time and watching its impact on
variables. Code tracing is often done with built-in debugging tools
or debuggers.
Before you start with debugging process, make sure that Variable
Explorer pane is visible. (see on next page)
If, however, this pane is not visible, you can open it using
command:
View menus -> Panes -> Varibale explorer
Or by pressing shortcut key Ctrl+Shift+V.
Q. What is debugging?
A. Debugging refers to the process of locating the place of error,
cause of error, and correcting the code accordingly.
Q. What is debugger?
A. Debugger is a tool that lets you trace and execute code line by
line.
Q. What is exception?
A. Exception in general refers to some contradictory or unusual
situation which can be encountered unexpectedly while executing a
program.
List
1. Declaring a List :
Firstlist = ["France", "Belgium", "England"]
Indexing in a list begins from 0
#Printing all content of list
print(Firstlist)
Built-in Functions
Few examples:
# declaring list
a = [3, 10, 1, 3, 4, 5]
i = flag = 0
i=i+1
if flag == 1:
print("item found at position:", i + 1)
else:
print("item not found")
Tuples in Python
● Introduction
● Creating and Accessing Tuples
● Tuple Operations
● Tuple Functions and Methods
● Questions on Tuples
INTRODUCTION
1. # An empty tuple
emptytuple = ()
print (emptytuple)
To construct a tuple with one element just add a comma after the
single element :
Singtup = (10,)
3. Long Tuple
T=( 2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,
44,46,48,50,52,54 )
4. Nested Tuple
T=(1,2,(3,4))
Accessing Tuples
1. Indexing
We can use the index operator [] to access an item in a tuple where
the index starts from 0.
my_tuple = ('0','r','a','n','g','e')
print(my_tuple[0])
print(my_tuple[5])
# nested tuple
o
e
g
4
2. Negative Indexing
Python allows negative indexing for its sequences.
The index of -1 refers to the last item, -2 to the second last item and
so on.
my_tuple = ('f','o','r','m','a','t')
print(my_tuple[-1])
# Output: 't'
print(my_tuple[-6])
# Output : ‗f‘
3. Slicing
my_tuple = ('p','r','o','g','r','a','m','i','t')
print(my_tuple[1:4])
print(my_tuple[:-7])
print(my_tuple[7:])
# Output: ('p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 't')
print(my_tuple[:])
Modifying Tuples
T=(10,20,30,40)
a,b,,c,d =T
b=22
T= (a,b,c,d)
Deleting a Tuple
As discussed above, we cannot change the elements in a tuple. That
also means we cannot delete or remove items from a tuple.
my_tuple = ('p','r','o','g','r','a','m','i','z')
del my_tuple
my_tuple
Functio
Description
n
Questions
a)(1, 2)
b) (1, 2, 4)
c) (2, 4)
d) (2, 4, 3)
a) [2, 3, 9].
b) [1, 2, 4, 3, 8, 9].
c) [1, 4, 8].
d) (1, 4, 8)
6. What will be the output?
1. d = {"john":40, "peter":45}
2. d["john"]
a) 40
b) 45
c) ―john‖
d) ―peter‖
a) (1, 2, 1, 2)
b) [1, 2, 1, 2].
a) True
b) False
c) Error
d) None
a) 1
b) 2
c) 5
d) Error
a) 30
b) 24
Answers
1 2 3 4 5 6 7 8 9 10
b b c c c a a b d c
DICTIONARY
Overview[
Dictionaries in Python at a glance:
Dictionary notation
Dictionaries may be created directly or converted from sequences.
Dictionaries are enclosed in curly braces, {}
#!/usr/bin/python
Operations on Dictionaries[edit]
del dictionaryName[membername]
Exercises
Write a program that:
2. Replaces the entry whose key is the integer 3, with the value
"Pie".
3. Asks the user for a string of digits, then prints out the values
corresponding to those digits.
First Pass
Second Pass
Third Pass
Fourth Pass
nlist = [14,46,43,27,57,41,45,21,70]
bubbleSort(nlist)
print(nlist)
Calculating Number of operations on Bubble Sort:
Number of operations is an important aspect of any
algorithms/programs as it specify the efficiency of the program.
Less number of operations means higher efficiency. Two different
programs with different logic can deliver the same output but the
efficient one will accomplish the task in lesser number of
operations.
Comparison and swapping are the major operations in bubble
sort and played major role in efficiency calculation(Complexity)
To calculate the number of operation in bubble sort let us keep the
above example
Operations in 1st Pass (for an Array of 5 elements)
6 3 5 8 2
3 6 5 8 2
3 6 5 8 2
3 5 6 8 2
3 5 6 8 2
3 5 6 8 2
3 5 6 8 2
3 5 6 2 8
1. Compare 6 and 3
2. 6 is higher, so swap 6 and 3
3. Compare 6 and 5
Assignment:
Insertion Sort
iii. Then, we make the third element of the array as key and will
compare it with elements to it's left and insert it at the right
position.
iv. And we go on repeating this, until the array is sorted.
Implementation in Python:
Output:
Original list is : [20, 12, 18, 25, 6, 35]
List After Sorting: [6, 12, 18, 20, 25, 35]
Number of Operations
There are two operations carried out in the insertion operation i.e.
Comparison and exchange operation. Let calculate number of
operations of each type for a sequence having N number of
elements.
i) The number of comparison:
If we carefully go through the code, we will find that
● During the first iteration of outer loop, in the inner loop there
is 01 comparison.
● During the second iteration of outer loop, there are at most 02
comparisons.
● During the N-1 th iteration of outer loop, there are at most N-
1 comparisons.
********************
Strings
String Methods
Let our string variable be called ‗var‘
➢ length of string – len(var)
➢ convert to string – str(var)
➢ convert to lowercase – var.lower()
➢ convert to uppercase – var.upper()
➢ Is digit/Is alpha – var.isdigit()/var.isalpha()
➢ Replace characters – var.replace(old,new)
➢ Split a sentence – var.split(delimiter)
➢ Swap case – var.swapcase()
➢ Range slice – var [start index : end index]
➢ Concatenation
➢ Combining of strings is done by using the (+) operator
between them.
➢ In order to combine a string with a non-string variable,
use str() method to convert non-string to string.
Eg. ―tea‖+‖pot‖
Will result into ―teapot‖
➢ Formatting
The String format operator is %
➢ %c - character
➢ %s – string conversion via str() prior to formatting
➢ %d – signed decimal integer
➢ %x %X – hexadecimal integer (lowercase/uppercase)
➢ %f – floating point real number
Prob: Two strings are given you have to modify 1st string such that
all the common characters of the 2nd string have to be removed and
uncommon characters of the 2nd string have to be concatenated
with uncommon characters of the 1st string.
Eg. Input : S1 = ―aacdb‖
S2 = ―gafd‖
Solution:-
Srt1=‗Geeta‘ , str2 = ‗Babita‘
Set1= set(str1) # convert both string into set
Set2= set(str2)
String Comparison
str = ―implementation‖
count = 0
vowel = set (―aeiouAEIOU‖)
for alphabet in str:
if alphabet in vowel:
count= count + 1
Print (―no. of vowels :‖, count)
String slices
-7 -6 -5 -4 -3 -2 -1
In a string slice you give the slicing range in the form [<begin-
index> : <last>]
If, however you skip either of the begin-index or last, python will
consider limit of string i.e for missing begin-index it will consider 0
and for missing last value it will consider length of the string.
String Comparison
Program that reads a line & a substring. It should then display the
number of occurrences of the given substring in the line.
Sol :
line = input (―Enter line‖)
sub = input (―Enter substring‖)
length = len (line)
lensub = len(sub)
start = count = 0
end = length
while True:
Pos = line.find (sub, start, end)
if Pos! = -1:
count + = 1
Programs
1. Programs that reads a line & Prints its statistics like:
Number of uppercase letters:
Number of lowercase letters:
Number of alphabets:
Number of digits:
Line = input(―Enter a line‖)
lowercount = uppercount = 0
digitcount = alphacount = 0
for a in line:
if a. islower () :
lowercount + = 1
elif a. isupper ( ) :
uppercount + = 1
elif a. isdigit ( ) :
digitcount + = 1
elif a. isalpha ( ) :
alphacount + = 1
print (― No. of uppercase letters:‖ uppercount)
print (― No. of lowercase letters:‖ lowercount)
print (― No. of alphabets:‖ alphacount)
print (― No. of digits:‖ digitcount)
Solution:-
String = input(― Enter a string:‖)
length = len (string)
a=0
end = length
string2 = ‗ ‘
while a < length:
if a = = 0:
string2 + = string [0].upper( )
a+=1
elif (string [a] = = ‗ ‗ and string [a + 1]!=‗‘):
string2 + = string [a]
string2 + = string [a + 1]. upper( )
a+=2
else:
string2 + = string [a]
a+=1
Print (― original string : ― , string)
Print (― capitalized words string ― , string2)
Example
of State
Transition
Diagram:
You need to develop a web-based application in such a way that
user can search other users, and after getting search complete, the
user can send the friend request to other users. If the request is
accepted, then both users are added to the friend list of each other.
If one user does not accept the friend request. The second user can
send another friend request. The user can also block each other.
Solution:
1. First of all, identify the object that you will create during the
development of classes in oop
2. Identity the actions or events
3. Identify the possible states for object
4. Draw the diagram.
Object: friends
Events or actions: Search to add a friend, add a friend, accept a
friend, reject a friend, again add, block user and close.
States: Start, the friend added, friend rejected, user blocked and
end.
RDBMS vs NoSQL
RDBMS
- Structured and organized data
- Structured query language (SQL)
- Data and its relationships are stored in separate tables.
NoSQL
- Stands for Not Only SQL
- No declarative query language
- No predefined schema
- Key-Value pair storage, Column Store, Document Store, Graph
databases
- Eventual consistency rather ACID property
- Unstructured and unpredictable data
- CAP ( Consistency, Availability and Partition tolerance ) Theorem
- Prioritizes high performance, high availability and scalability
- BASE (Basically Available Soft state Eventual consistency )
Transaction
● High scalability
● Distributed Computing
● Lower cost
● Open Source
Disadvantages
● Lack of standardization
● Consistency
● Backup of database
where each key is unique and the value can be string, JSON,
BLOB (Binary Large OBjec) etc.
● A key may be strings, hashes, lists, sets, sorted sets and
of CAP theorem.
● Key-Values stores would work well for shopping cart contents,
Document databases :
● A collection of documents
Graph databases:
A graph data structure consists of a finite (and possibly mutable)
set of ordered pairs, called edges or arcs, of certain entities called
nodes or vertices.
The following picture presents a labeled graph of 6 vertices and 7
edges.
Production deployment
There is a large number of companies using NoSQL like :
● Google
● Mozilla
● Adobe
● Foursquare
● Digg
● McGraw-Hill Education
Introduction
What is MongoDB?
MongoDB is an open-source document database and leading NoSQL
database. MongoDB is written in C++.
MongoDB is a cross-platform, document oriented database
Related terms
In RBDMS In MongoDB
Database Database
Table Collection
Rows Document (key-value pairs)
Column Field
Table Join Embedded Documents
Primary Primary key
key (―_id‖ provided automatically by mongodb, or user can
give its value, which is a 12 byte hexadecimal number
which assures uniqueness of every document.)
12 bytes first 4 bytes for the current timestamp,
next 3 bytes for machine id,
next 2 bytes for process id of MongoDB
server
remaining 3 bytes are simple incremental VALUE.
Relationsh No such concept
ip
Terms Definition
Databa Database is a physical container for collections. Each
se database gets its own set of files on the file system.
Collecti Collection is a group of MongoDB documents(RDBMS
on table).
A collection exists within a single database.
Collections do not enforce a schema.
Documents within a collection can have different fields.
Typically, all documents in a collection are of similar or
related purpose.
Docum A document is a set of key-value pairs.
ent Documents have dynamic schema.
● Schema less
● Structure of a single object is clear.
● No complex joins.
● Deep query-ability. MongoDB supports dynamic queries on
documents using a document-based query language that's
nearly as powerful as SQL.
● Tuning. (improving the performance of database better)
● Ease of scale-out − MongoDB is easy to scale.
● Conversion/mapping of application objects to database objects
not needed.
● Uses internal memory for storing the (windowed) working set,
enabling faster access of data.
● Big Data
Installing MongoDB
➔ It is very easy to install MongoDB, just download the installer
file from the internet or from and install it. But do remember
before running the server of MongoDB to make use of the
same do the following steps:
o Create a folder in root directory with the name ―data‖
o Inside the ―data‖ folder create following two folders
▪ A folder named ―db‖ – for database transactions
▪ A folder named ―log‖ – for keeping log of different
transactions/activities
Once the above folders are created switch to MongoDB folder which
is given at the time to installation and open file ―mongod.exe‖ to
execute the server.
Note: Windows 7 supports MongoDB 3.0 where as any other
version higher that windows 7 supports higher version then
MongoDB 3.0 (like MongoDB 3.2 etc.)
How to find whether server is ready: when the following window
appears containing last line as ―connections now open‖ it means the
server is ready to work.
ObjectId{―5b378dce90
c313500d328ac9),
―fname‖ : ‖Vaibhav‖,
arr=[]
for i in range(0,n):
x=int(input('Enter Number'))
l=g=arr[0]
for i in range(0,n):
if(g<arr[i]):
g=arr[i]
if(l>arr[i]):
l=arr[i]
print("Greater Number=",g)
print("Lesser No=",l)
a=[]
for i in range(1,n+1):
b=int(input('Enter Number'))
a.append(b)
a.sort()
Output:
Enter Number of elements 5
Enter Number 22
Enter Number 66
Enter Number 55
Enter Number 77
Enter Number 11
Third largest element is: 55
for i in range(2,no):
ans=no%i
break
elif i==no-1:
print('Prime Number')
Output:
Enter Number : 8
Non Prime
============================
>>>
Enter Number : 7
Prime Number
s=""
l=len(st)
a=range(l-1,-1,-1)
for i in a:
s = s + st[i]
if (st==s):
else :
Output:
madam is Palindrome
================================
>>>
p=x**n
print("pow(x,n)=",p)
Solution:
while(y):
x, y = y, x % y
return x
lcm = (x*y)//gcd(x,y)
return lcm
length = len(str(num))
sum = 0
temp = num
while(temp != 0):
sum = sum + ((temp % 10) ** length)
temp = temp // 10
if sum == num:
Solution:
lower = 100
upper = 1000
l=[ ]
for num in range(lower, upper + 1):
# order of number
order = len(str(num))
# initialize sum
sum = 0
if num == sum:
l.append(num)
print("Smallest number=",l[0])
print("Largest number=",l[-1])
-----------------------------------------------------------------------------------
Case 1:
Enter the principle amount:200
Enter the time(years):5
Enter the rate:5.0
The simple interest is: 50.0
Case 2:
Enter the principle amount:70000
Enter the time(years):1
Enter the rate:4.0
The simple interest is: 2800.0
Output:
i=0
while i < n :
# sum of elements of array X.
sum_X = sum_X + X[i]
i=i+1
# Driver function
X = [15, 18, 21, 24, 27]
Y = [25, 25, 27, 31, 32]
# driver code
principal = 10000;
rate = 10;
time = 2;
emi = emi_calculator(principal, rate, time);
print("Monthly EMI is= ", emi)
# Python3 Program to
# compute GST from original
# and net prices.
print(round(Calculate_GST(org_cost, N_price)),end='')
print("%")
Linear Search
In computer science, linear search or sequential search is a
method for finding a target value within a list It sequentially checks
each element of the list for the target value until a match is found
or until all the elements have been searched.
Algorithm
Linear search sequentially checks each element of the list until it
finds an element that matches the target value If the algorithm
reaches the end of the list, the search terminates unsuccessful.
Linear search is implemented using following steps...
Step 1: Read the search element from the user
Example
Consider the following list of element and search element...
Program Code
#Linear Search
list_of_elements = [4, 2, 8, 9, 3, 7]
found = False
for i in range(len(list_of_elements)):
if(list_of_elements[i] == x):
found = True
if(found == False):
print("%d is not in list"%x)
Creating a Module:
import a
a.label(s)
When import is used, it searches for the module initially in the local
scope by calling __import__() function. The value returned by the
function are then reflected in the output of the initial code. In the
above (Fibonacci number module) example, if we write
Import fibo
>>>
>>> fibo.fib(1000)
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
>>> fibo.fib2(100)
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
>>> fibo.__name__
'fibo'
>>>
>>> fib = fibo.fib
>>> fib(500)
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
>>>
>>> from fibo import fib, fib2
>>> fib(500)
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
SERIES:
2 index
Index values must be unique and hashable, same length
as data. Default np.arrange(n) if no index is passed.
3 dtype
dtype is for data type. If None, data type will be inferred
4 copy
Copy data. Default False
● Dict
Now, let‘s call the Series so we can see what pandas does with it:
>>> s
We‘ll see the following output, with the index in the left column, our
data values in the right column. Below the columns is information
about the Name of the Series and the data type that makes up the
values.
Output
0 0
1 1
2 4
3 9
4 16
5 25
Name: Squares, dtype: int64
Though we did not provide an index for the array, there was one
added implicitly of the integer values 0 through 5.
Declaring an Index
As the syntax above shows us, we can also make Series with an
explicit index. We‘ll use data about the average depth in meters of
the Earth‘s oceans:
Output
Arctic 1205
Atlantic 3646
Indian 3741
Pacific 4080
Southern 3270
dtype: int64
We can see that the index we provided is on the left with the values
on the right.
Indexing and Slicing Series
With pandas Series we can index by corresponding number to
retrieve values:
>>> avg_ocean_depth[2]
Output
3741
>>> avg_ocean_depth[2:4]
Output
Indian 3741
Pacific 4080
dtype: int64
Additionally, we can call the value of the index to return the value
that it corresponds with:
>>> avg_ocean_depth['Indian']
Output
3741
print s
a 0.0
b 1.0
c 2.0
dtype: float64
Example 2
print s
0 5
1 5
2 5
3 5
dtype: int64
import pandas as pd
s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])
print s[0]
1
Example 2
Retrieve the first three elements in the Series. If a : is inserted in
front of it, all items from that index onwards will be extracted. If
two parameters (with : between them) is used, items between the
two indexes (not including the stop index)
import pandas as pd
s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])
print s[:3]
import pandas as pd
s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])
c 3
d 4
e 5
dtype: int64
Retrieve Data Using Label (Index)
A Series is like a fixed-size dict in that you can get and set values
by index label.
Example 1
Retrieve a single element using index label value.
import pandas as pd
s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])
import pandas as pd
s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])
a 1
c 3
d 4
dtype: int64
import pandas as pd
ds = pd.Series([2, 4, 6, 8, 10])
print(ds)
Sample Output:
Python Code :
import pandas as pd
ds = pd.Series([2, 4, 6, 8, 10])
print(ds)
print(type(ds))
print(ds.tolist())
print(type(ds.tolist()))
Sample Output:
print("Series1:")
print(ds1)
print("Series2:")
print(ds2)
print("Equals:")
print(ds1 == ds2)
print("Greater than:")
print("Less than:")
Sample Output:
1. Series1:
2. 0 2
3. 1 4
4. 2 6
5. 3 8
6. 4 10
7. dtype: int64
8. Series2:
9. 0 1
10. 1 3
Features of DataFrame
● Size – Mutable
1 data
data takes various forms like ndarray, series, map, lists,
dict, constants and also another DataFrame.
3 columns
For column labels, the optional default syntax is -
np.arrange(n). This is only true if no index is passed.
4 dtype
Data type of each column.
4 copy
This command (or whatever it is) is used for copying of
data, if the default is False.
Create DataFrame
A pandas DataFrame can be created using various inputs like −
● Lists
● dict
● Series
● Numpy ndarrays
● Another DataFrame
In the subsequent sections of this chapter, we will see how to
create a DataFrame using these inputs.
Create an Empty DataFrame
A basic DataFrame, which can be created is an Empty Dataframe.
Example
print df
Empty DataFrame
Columns: []
Index: []
Create a DataFrame from Lists
The DataFrame can be created using a single list or a list of lists.
Example 1
import pandas as pd
data = [1,2,3,4,5]
df = pd.DataFrame(data)
print df
import pandas as pd
data = [['Alex',10],['Bob',12],['Clarke',13]]
df = pd.DataFrame(data,columns=['Name','Age'])
print df
Name Age
0 Alex 10
1 Bob 12
2 Clarke 13
df = pd.DataFrame({'X':[78,85,96,80,86],
'Y':[84,94,89,83,86],'Z':[86,97,96,72,83]});
print(df)
Sample Output:
1. X Y Z
2. 0 78 84 86
3. 1 85 94 97
4. 2 96 89 96
5. 3 80 83 72
6. 4 86 86 83
import pandas as pd
import numpy as np
'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes']}
labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
df = pd.DataFrame(exam_data , index=labels)
print(df)
Copy
Sample Output:
The index in left most column now refers to data in the right
column.
>>> x[―c‖]
1 data
data takes various forms like ndarray, list, constants
2 index
Index values must be unique and hashable, same length
as data. Default np.arrange(n) if no index is passed.
3 dtype
dtype is for data type. If None, data type will be inferred
4 copy
Copy data. Default False
print s
0 a
1 b
2 c
3 d
dtype: object
We did not pass any index, so by default, it assigned the indexes
ranging from 0 to len(data)-1, i.e., 0 to 3.
Example 2
data = np.array(['a','b','c','d'])
100 a
101 b
102 c
103 d
dtype: object
We passed the index values here. Now we can see the customized
indexed values in the output.
Create a Series from dict
A dict can be passed as input and if no index is specified, then the
dictionary keys are taken in a sorted order to construct index.
If index is passed, the values in data corresponding to the labels in
the index will be pulled out.
Example 1
print s
a 0.0
b 1.0
c 2.0
dtype: float64
Observe − Dictionary keys are used to construct index.
print s
b 1.0
c 2.0
d NaN
a 0.0
dtype: float64
Observe − Index order is persisted and the missing element is filled
with NaN (Not a Number).
Create a Series from Scalar
If data is a scalar value, an index must be provided. The value will
be repeated to match the length of index
print s
0 5
1 5
2 5
import pandas as pd
s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])
1
Example 2
Retrieve the first three elements in the Series. If a : is inserted in
front of it, all items from that index onwards will be extracted. If
two parameters (with : between them) is used, items between the
two indexes (not including the stop index)
import pandas as pd
s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])
print s[:3]
import pandas as pd
s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])
c 3
d 4
e 5
dtype: int64
Retrieve Data Using Label (Index)
A Series is like a fixed-size dict in that you can get and set values
by index label.
Example 1
Retrieve a single element using index label value.
import pandas as pd
s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])
import pandas as pd
s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])
a 1
c 3
d 4
dtype: int64
Example 3
If a label is not contained, an exception is raised.
import pandas as pd
s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])
print s['f']
…
KeyError: 'f'
Prerequisite
Basic understanding of common data structure of Python like
list, tuple, dict, etc. is the prerequisite of Pandas.
The Basics:
To work with pandas in Python, the very first instruction
should be import directive which may be done as follow.
In [1]: import pandas as pd
Or may be more specific
In [2]: from pandas import DataFrame
DataFrame:
DataFrame has the methods add(), sub(), mul(), div() and related
functions radd(), rsub(), . . . for carrying out binary operations.
For broadcasting behavior, Series input is of primary interest.
Using these functions, you can use to either match on the index
or columns via the axis keyword:
In [15]: df
Out[15]:
one two three
a -1.101558 1.124472 NaN
b -0.177289 2.487104 -0.634293
c 0.462215 -0.486066 1.931194
d NaN -0.456288 -1.222918
pandas.DataFrame.add
Example
>>> a = pd.DataFrame([1, 1, 1, np.nan], index=['a', 'b', 'c', 'd'],
... columns=['one'])
>>> a
one
a 1.0
b 1.0
c 1.0
d NaN
>>> b = pd.DataFrame(dict(one=[1, np.nan, 1, np.nan],
... two=[np.nan, 2, np.nan, 2]),
... index=['a', 'b', 'd', 'e'])
>>> b
one two
pandas.DataFrame.sub
DataFrame.sub(other, axis='columns', level=None, fill_value=None)[
source]
Subtraction of dataframe and other, element-wise (binary
operator sub).
pandas.DataFrame.mul
DataFrame.mul(other, axis='columns', level=None, fill_value=None)
Multiplication of dataframe and other, element-wise (binary
operator mul).
Example:
pandas.DataFrame.div
DataFrame.div(other, axis='columns', level=None, fill_value=None)
Floating division of dataframe and other, element-wise
(binary operator truediv).
Example:
Example:
pandas.DataFrame.rsub
DataFrame.rsub(other, axis='columns', level=None, fill_value=None)
Subtraction of dataframe and other, element-wise (binary
operator rsub).
Example:
Example:
pandas.DataFrame.rdiv
DataFrame.rdiv(other, axis='columns', level=None, fill_value=None)
Floating division of dataframe and other, element-wise
(binary operator rtruediv).
Example:
import pandas as pd
import numpy as np
rng = np.random.RandomState(42)
ser = pd.Series(rng.randint(0, 10, 4))
ser
Output
0 6
1 3
2 7
3 4
dtype: int64
df = pd.DataFrame(rng.randint(0, 10, (3, 4)),
columns=['A', 'B', 'C', 'D'])
df
Output:
A B C D
0 6 9 2 6
1 7 4 3 7
2 7 2 5 4
If we apply a NumPy ufunc on either of these objects, the result will
be another Pandas object with the indices preserved:
np.sin(df * np.pi / 4)
Output:
A B C D
- -
7.071068e- 1.00000
01.00000 1.000000e+0
01 0
0 0
-
1.224647e- 0.70710 -7.071068e-
10.70710
16 7 01
7
- -
1.000000e+ 1.224647e-
20.70710 0.70710
00 16
7 7
In [6]:
area = pd.Series({'Alaska': 1723337, 'Texas': 695662,
'California': 423967}, name='area')
population = pd.Series({'California': 38332521, 'Texas': 26448193,
'New York': 19651127}, name='population')
Let's see what happens when we divide these to compute the
population density:
population / area
Output:
Alaska NaN
California 90.413926
New York NaN
Texas 38.018740
dtype: float64
The resulting array contains the union of indices of the two input
arrays, which could be determined using standard Python set
arithmetic on these indices:
area.index | population.index
Output:
Index(['Alaska', 'California', 'New York', 'Texas'], dtype='object')
Any item for which one or the other does not have an entry is
marked with NaN, or "Not a Number," which is how Pandas marks
missing data (see further discussion of missing data in Handling
Missing Data). This index matching is implemented this way for any
of Python's built-in arithmetic expressions; any missing values are
filled in with NaN by default:
A.add(B, fill_value=0)
Output:
0 2.0
1 5.0
2 9.0
3 5.0
dtype: float64
A B
0 1 11
1 5 1
B A C
0 4 0 9
1 5 8 0
2 9 2 6
In [13]:
A+B
Output:
A B C
In [14]:
fill = A.stack().mean()
A.add(B, fill_value=fill)
Output:
A B C
Python
Pandas Method(s)
Operator
+ add()
- sub(), subtract()
* mul(), multiply()
// floordiv()
% mod()
** pow()
import pandas as pd
import numpy as np
print (df)
import pandas as pd
import numpy as np
print (df['one'].isnull())
a False
b True
c False
d True
e False
f False
g True
h False
Name: one, dtype: bool
Example 2
import pandas as pd
import numpy as np
a True
b False
c True
d False
e True
f True
g False
h True
Name: one, dtype: bool
import pandas as pd
import numpy as np
print (df['one'].sum())
2.02357685917
Example 2
import pandas as pd
import numpy as np
df = pd.DataFrame(index=[0,1,2,3,4,5],columns=['one','two'])
print (df['one'].sum())
nan
Cleaning / Filling Missing Data
Pandas provides various methods for cleaning the missing values.
The fillna function can ―fill in‖ NA values with non-null data in a
couple of ways, which we have illustrated in the following sections.
import pandas as pd
import numpy as np
'two', 'three'])
print df
print (df.fillna(0))
Method Action
Example 1
import pandas as pd
import numpy as np
print (df.fillna(method='pad'))
import pandas as pd
import numpy as np
print (df.fillna(method='backfill'))
Series
● Array
● Dict
● Scalar value or constant
Example
Output.
Series([], dtype: float64)
import pandas as pd
ds1 = pd.Series([2, 4, 6, 8, 10])
ds2 = pd.Series([1, 3, 5, 7, 10])
print("Series1:")
print(ds1)
print("Series2:")
print(ds2)
print("Compare the elements of the said Series:")
print("Equals:")
print(ds1 == ds2)
print("Greater than:")
print(ds1 > ds2)
print("Less than:")
print(ds1 < ds2)
Output
Series1:
0 2
1 4
2 6
3 8
Series2:
0 1
1 3
2 5
3 7
4 10
dtype: int64
Equals:
0 False
1 False
2 False
3 False
4 True
dtype: bool
Greater than:
0 True
1 True
2 True
3 True
4 False
dtype: bool
Less than:
0 False
1 False
2 False
3 False
4 False
dtype: bool
DataFrames
● Lists
● dict
● Series
● Numpy ndarrays
● Another DataFrame
Combining DataFrames
import pandas as pd
df1 = pd.DataFrame({'HPI':[80,85,88,85],
'Int_rate':[2, 3, 2, 2],
'US_GDP_Thousands':[50, 55, 65, 55]},
df3 = pd.DataFrame({'HPI':[80,85,88,85],
'Unemployment':[7, 8, 9, 6],
'Low_tier_HPI':[50, 52, 50, 53]},
index = [2001, 2002, 2003, 2004])
print(pd.merge(df1,df3, on='HPI'))
Output:
df.to_csv('example.csv')
Load a csv
df = pd.read_csv('example.csv')
print(df)
df = pd.read_csv('example.csv', header=None)
print(df)
0 1 2 3
first_name last_name age TestScore
0 Mohan Kumar 42 4
import sqlite3
conn = sqlite3.connect('test.db')
print "Opened database successfully";
conn.commit()
print ("Records created successfully");
import pandas as pd
Algorithm :
● Read input number asking for length of the list
using input() or raw_input().
● Initialise an empty list lst = [].
● Read each number using a for loop.
● In the for loop append each number to the list.
● Now we use predefined function max() to find the largest
element in a list.
● Similarly we use another predefined function min() to find the
smallest element in a list.
Program :
lst = []
num = int(input('How many numbers: '))
for n in range(num):
Output :
How many numbers: 5
Enter number 10
Enter number 15
Enter number 5
Enter number 8
Enter number 30
Maximum number in a list : 30
Minimum number in a list : 5
theset = frozenset(thelist)
nums = [5, 4, 3, 2, 1]
print(nums[1])
[2,]
print(num[0])
print(num[3][0])
print(num[5])
words = ["hello"]
words.append("world")
print(words[1])
letters.append("d")
print(len(letters))
nums = [9, 8, 7, 6, 5]
nums.append(4)
nums.insert(2, 11)
print(len(nums))
letters.append("d")
print(len(letters))
nums = [9, 8, 7, 6, 5]
nums.append(4)
nums.insert(2, 11)
print(len(nums))
nums = list(range(5))
print(nums[4])
print(len(nums))
print(nums[2])
print(sqs[4:7])
print(sqs[1::4])
print(sqs[7:5:-1])
Program:
return sm
# Driven Program
n=4
print(squaresum(n))
return (n * (n + 1) * (2 * n + 1)) // 6
# Driven Program
n=4
print(squaresum(n))
if str1[m-1] == str2[n-1]:
return isKPalRec(str1, str2, m-1, n-1)
return res
==================================== END
================================
Program Code:
def power(base,exp):
if (exp==1):
return (base)
if (exp!=1):
return (base*power(base,exp-1))
base = int(input("Enter base: "))
exp = int(input("Enter exponential value: "))
print ("Result:",power(base,exp))
Case 1:
Enter base: 2
Enter exponential value: 5
Result: 32
Case 2:
Enter base: 5
Enter exponential value: 3
Result: 125
if (n == 0): return 1
elif (int(n % 2) == 0):
return (power(x, int(n / 2)) *
power(x, int(n / 2)))
# Driver Code
x = 2; y = 3
print(power(x, y))
# define a function
def computeGCD(x, y):
return hcf
num1 = 54
num2 = 24
# take input from the user
# num1 = int(input("Enter first number: "))
# num2 = int(input("Enter second number: "))
while(True):
if((greater % x == 0) and (greater % y == 0)):
lcm = greater
break
greater += 1
return lcm
The Pandas library has the broader goal of becoming the most
powerful and flexible open source data analysis and manipulation
tool available in any language.
That‘s all the more reason for you to get started on working with
this library and its expressive data structures straight away!
emp_id = c (1:5),
print(v)
For the data set show in the above image, I am trying to find the
three most populous states while only taking into consideration the
three most populous counties for each state.
I use CENSUS2010POP.
This function should return a list of string values(in order of highest
population to lowest population).
Below is My Code:
x=census_df.groupby('STNAME')['CENSUS2010POP'].nlargest(3)
In [3]: df
Out[3]:
a b
0 0 -3
1 -1 2
2 2 1
In [5]: df
Out[5]:
a b
0 0 0
1 0 2
2 2 1
In [3]: df
Out[3]:
a b c
In [6]: df
Out[6]:
a b c
0 0 0 foo
1 0 2 goo
2 2 1 bar
In [3]: df
Out[3]:
a b
0 0 days -3 days
1 -1 days 2 days
2 2 days 1 days
In [5]: df
Out[5]:
a b
0 0 days 0 days
1 0 days 2 days
import pandas as pd
In [21]: df
Out[21]:
a
0 -1
1 100
2 -2
In [22]: df.clip(lower=0)
Out[22]:
a
0 0
1 100
2 0
import pandas as pd
data = [['Rajiv',10],['Sameer',12],['Kapil',13]]
df = pd.DataFrame(data,columns=['Name','Age'])
print (df)
import pandas as pd
weather_data={
'day':['01/01/2018','01/02/2018','01/03/2018','01/04/2018','01/0
5/2018','01/01/2018'],
'temperature':[42,41,43,42,41,40],
'windspeed':[6,7,2,4,7,2],
'event':['Sunny','Rain','Sunny','Sunny','Rain','Sunny']
}
df=pd.DataFrame(weather_data)
print(df)
print(df.head())
print("Tail")
print(df.tail(2))
print("Print Everything")
print(df[:])
print(df['temperature'])
print("Maximum Temperature : ", df['temperature'].max())
print("According to index")
print(df.loc[3])
print("Changing of Index")
df.set_index('day',inplace=True)
print(df)
print("Sorting")
print(df.sort_values(by=['temperature'],ascending=False))
import pandas as pd
df=pd.read_csv("student.csv", nrows=3)
print("To display selected number of rows from beginning")
print(df)
df=pd.read_csv("student.csv")
print(df)
print(df.head())
print("Tail")
print(df.tail(2))
print("Print Everything")
print(df[:])
print(df['Marks'])
print("Maximum Marks : ", df['Marks'].max())
print("According to index")
print(df.loc[3])
print("Changing of Index")
df.set_index('Scno',inplace=True)
print(df)
print("Sorting")
print(df.sort_values(by=['Marks'],ascending=False))
print("Group By Operations")
print(df.groupby('Class')['Marks'].sum())
import pandas as pd
data = [['Rajiv',10],['Sameer',12],['Kapil',13]]
df = pd.DataFrame(data,columns=['Name','Age'])
print (df)
df.to_csv('new.csv')
df.to_csv('new1.csv', index=False)
df.to_csv('new2.csv', columns=['Name'])
df.to_csv('new4.csv', header=False)
data = np.array([['','Col1','Col2'],
['Row1',1,2],
['Row2',3,4]])
print(pd.DataFrame(data=data[1:,1:],
index=data[1:,0],
columns=data[0,1:]))
Data frames in Python are very similar: they come with the Pandas
library, and they are defined as a two-dimensional labeled data
structures with columns of potentially different types.
Let‘s take R for example. You use the [,] notation to access the data
frame‘s values. In Pandas DataFrames, this is not too much
Before you can get to the solution, it‘s first a good idea to grasp the
concept of loc and how it differs from other indexing attributes such
as .iloc and .ix:
● loc works on labels of your index. This means that if you give in
loc[2], you look for the values of your DataFrame that have an
index labeled 2.
● iloc works on the positions in your index. This means that if you
give in iloc[2], you look for the values of your DataFrame that are
at index ‘2`.
● ix is a more complex case: when the index is integer-based, you
pass a label to ix. ix[2] then means that you‘re looking in your
DataFrame for values that have an index labeled 2. This is just
Now that the difference between iloc, loc and ix is clear, you are
ready to give adding rows to your DataFrame a go!
Note that the observation that was made earlier about loc still stays
valid also for when you‘re adding columns to your DataFrame!
Tip: try changing the inplace argument in the first task (renaming
your columns) to False and see what the script now renders as a
result. You see that now the DataFrame hasn‘t been reassigned
when renaming the columns. As a result, the second task takes the
original DataFrame as input and not the one that you just got back
from the first rename() operation.
In such cases, you can construct your own parser to deal with this.
You could, for example, make a lambda function that takes your
DateTime and controls it with a format string.
***************************