100% found this document useful (1 vote)
69 views

Python Basic and Advanced-Day 8

The document provides an overview of Python basics and advanced concepts, covering topics ranging from Python environment setup and basic syntax to classes, objects, regular expressions, CGI programming, databases, networking, email, threading, XML processing, and GUI programming. It also includes examples of Python basics like variables, operators, decision making, loops, numbers, strings, lists, tuples, dictionaries, dates, times, functions, modules, files I/O, and exceptions. Advanced Python concepts like classes, objects, regular expressions, and database access are also covered.

Uploaded by

Ashok Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
69 views

Python Basic and Advanced-Day 8

The document provides an overview of Python basics and advanced concepts, covering topics ranging from Python environment setup and basic syntax to classes, objects, regular expressions, CGI programming, databases, networking, email, threading, XML processing, and GUI programming. It also includes examples of Python basics like variables, operators, decision making, loops, numbers, strings, lists, tuples, dictionaries, dates, times, functions, modules, files I/O, and exceptions. Advanced Python concepts like classes, objects, regular expressions, and database access are also covered.

Uploaded by

Ashok Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Python Basics & Advanced

1 Python Basics
o Python – Overview
2 Python Advanced
o Python - Classes/Objects
o Python - Environment Setup
Content
o Python - Basic Syntax o Python - Reg Expressions

o Python - Variable Types o Python - CGI Programming

o Python - Basic Operators o Python - Database Access

o Python - Decision Making o Python - Networking


o Python - Loops o Python - Sending Email
o Python - Numbers o Python - Multithreading
o Python - Strings o Python - XML Processing
o Python - Lists o Python - GUI Programming
o Python - Tuples
o Python - Dictionary
o Python - Date & Time
o Python - Functions
o Python - Modules
o Python - Files I/O
o Python - Exception
o Quick recap
Day-8 o Pandas Library.
Agenda
Python Basics
Quiz

Quiz1. Q 1 - What is output for −' ' in 'python' ?

A - 'python'

B - False

C - Name error

D - True
Python Basics
Quiz

Quiz1. Q 1 - What is output for −''in'python'?

A - 'python'

B - False

C - Name error

D - True

The correct answer is D


Explanation: To strings connected by ‘in’ operator gives true and false.
Python Basics
Quiz

Quiz2. what is the output of the following code?


print(type([1,2]))

A. <class 'tuple'>
B. <class 'int'>
C. <class 'set'>
D. <class 'complex'>
E. <class 'list'>
Python Basics
Quiz

Quiz2. what is the output of the following code?


print(type([1,2]))

A. <class 'tuple'>
B. <class 'int'>
C. <class 'set'>
D. <class 'complex'>
E. <class 'list'>
The correct answer is D
Explanation: [] denotes the list.
Python Basics
Quiz

Quiz3. What will be the output of the following code?


minidict = { 'name': 'BigDataFactory', 'name': 'website'}
print(minidict['name'])
A - BigDataFactory
B - Website
C - ('BigDataFactory' , 'website')
D - It will show an Error.
Python Basics
Quiz

Quiz3. What will be the output of the following code?


minidict = { 'name': 'BigDataFactory', 'name': 'website'}
print(minidict['name'])
A - BigDataFactory
B - Website
C - ('BigDataFactory' , 'website')
D - It will show an Error.

The correct answer is B


Explanation: Dictionary gets updated by the above code as the key has been assigned a new value.
Python Basics
Pandas
A panel is a 3D container of data. The names for the 3 axes are intended to give some semantic meaning to
describing operations involving panel data.

o items − axis 0, each item corresponds to a DataFrame contained inside.


o major_axis − axis 1, it is the index (rows) of each of the DataFrames.
o minor_axis − axis 2, it is the columns of each of the DataFrames.

Create Panel
import pandas as pd
import numpy as np
data = {'Item1' : pd.DataFrame(np.random.randn(4, 3))}
p = pd.Panel(data)
print(p)
print(p['Item1'])
print(p.major_xs(1))
print(p.minor_xs(1))
Python Basics
Pandas
Series Basic Functionality
Attribute or
Sr.No. Description
Method
1 axes Returns a list of the row axis labels
2 dtype Returns the dtype of the object.
3 empty Returns True if series is empty.
4 ndim Returns the number of dimensions of the underlying data, by definition 1.
5 size Returns the number of elements in the underlying data.
6 values Returns the Series as ndarray.
7 head() Returns the first n rows.
8 tail() Returns the last n rows.

s = pd.Series(np.random.randn(4))
print(s)
print(s.axes)
print(s.empty)
print(s.ndim)
print(s.size)
print(s.values)
print(s.head(2))
print(s.tail(2))
Python Basics
Pandas
Pandas Basic Functionality
Sr.No. Attribute or Method Description
1 T Transposes rows and columns.
Returns a list with the row axis labels and column axis labels as the only
2 axes
members.
3 dtypes Returns the dtypes in this object.
4 empty True if NDFrame is entirely empty [no items]; if any of the axes are of length 0.
5 ndim Number of axes / array dimensions.
6 shape Returns a tuple representing the dimensionality of the DataFrame.
7 size Number of elements in the NDFrame.
8 values Numpy representation of NDFrame.
9 head() Returns the first n rows.
10 tail() Returns last n rows.
#Create a Dictionary of series
d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Smith','Jack']),
'Age':pd.Series([25,26,25,23,30,29,23]),
'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])}
#Create a DataFrame
df = pd.DataFrame(d)
print("Our data series is:")
print(df)
print(df.T)
print(df.axes)
print(df.dtypes)
print(df.empty)
print(df.ndim)
print(df.shape)
print(df.size)
print(df.values)
print(df.head(2))
print(df.tail(2))
Python Basics
Pandas-Descriptive Statistics

Pandas - Descriptive Statistics


A large number of methods collectively compute descriptive statistics and other related operations on DataFrame

DataFrame − “index” (axis=0, default), “columns” (axis=1)


Sr.No. Function Description
1 count() Number of non-null observations

2 sum() Sum of values

3 mean() Mean of Values

4 median() Median of Values

5 mode() Mode of values

6 std() Standard Deviation of the Values

7 min() Minimum Value

8 max() Maximum Value

9 abs() Absolute Value

10 prod() Product of Values

11 cumsum() Cumulative Sum

12 cumprod() Cumulative Product


Python Basics
Pandas-Descriptive Statistics

Pandas - Descriptive Statistics


#Create a Dictionary of series
d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Smith','Jack',
'Lee','David','Gasper','Betina','Andres']),
'Age':pd.Series([25,26,25,23,30,29,23,34,40,30,51,46]),
'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8,3.78,2.98,4.80,4.10,3.65])
}

#Create a DataFrame
df = pd.DataFrame(d)
print(df.count())
print(df.sum())
print(df.mean())
print(df.median())
print(df.mode())
print(df.std())
print(df.min())
print(df.max())
print(df.prod())
print(df.cumsum())
print(df.describe())
Python Basics
Pandas-Summarizing Data

The describe() function computes a summary of statistics pertaining to the DataFrame columns and this function gives the mean,
std and IQR values.
#Create a Dictionary of series
d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Smith','Jack',
'Lee','David','Gasper','Betina','Andres']),
'Age':pd.Series([25,26,25,23,30,29,23,34,40,30,51,46]),
'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8,3.78,2.98,4.80,4.10,3.65])
}

#Create a DataFrame
df = pd.DataFrame(d)
print df.describe()
Python Basics
Pandas-Text Data Operations
Sr.No Function Description

• Pandas provides a 1 lower() Converts strings in the Series/Index to lower case.

2 upper() Converts strings in the Series/Index to upper case.


set of string functions
3 len() Computes String length().
which make it easy to
4 strip() Helps strip whitespace(including newline) from each string in the Series/index from both the sides.
operate on string
5 split(' ') Splits each string with the given pattern.
data. Most
6 cat(sep=' ') Concatenates the series/index elements with given separator.
importantly, these
7 get_dummies() Returns the DataFrame with One-Hot Encoded values.
functions ignore (or
8 contains(pattern) Returns a Boolean value True for each element if the substring contains in the element, else False.
exclude) missing/ 9 replace(a,b) Replaces the value a with the value b.
NaN values. 10 repeat(value) Repeats each element with specified number of times.

11 count(pattern) Returns count of appearance of pattern in each element.

12 startswith(pattern) Returns true if the element in the Series/Index starts with the pattern.

13 endswith(pattern) Returns true if the element in the Series/Index ends with the pattern.

14 find(pattern) Returns the first position of the first occurrence of the pattern.

15 findall(pattern) Returns a list of all occurrence of the pattern.

16 swapcase Swaps the case lower/upper.

17 islower() Checks whether all characters in each string in the Series/Index in lower case or not. Returns Boolean

18 isupper() Checks whether all characters in each string in the Series/Index in upper case or not. Returns Boolean.

19 isnumeric() Checks whether all characters in each string in the Series/Index are numeric. Returns Boolean.
Python Basics
Pandas-Text Data Operations

Output Part-1 Output Part-2


s = pd.Series(['Tom', 'William Rick@']) 0 Tom dtype: object
print(s) 1 William Rick@ 0 TomTomTom
dtype: object 1 William Rick@William Rick@William Rick@
print(s.str.lower()) 0 tom dtype: object
print(s.str.upper()) 1 william rick@ 0 1
print(s.str.len()) dtype: object 1 1
print(s.str.strip()) 0 TOM dtype: int64
print(s.str.split('')) 1 WILLIAM RICK@ 0 True
dtype: object 1 False
print(s.str.cat(sep='_')) 0 3 dtype: bool
print(s.str.get_dummies()) 1 13 0 1
print(s.str.contains('a')) dtype: int64 1 -1
print(s.str.replace('@','$')) 0 Tom dtype: int64
print(s.str.repeat(3)) 1 William Rick@ 0 []
dtype: object 1 [a]
print(s.str.count('m')) 0 [, T, o, m, ] dtype: object
print(s.str. startswith ('T')) 1 [, W, i, l, l, i, a, m, , R, i, c, k, @, ] 0 tOM
print(s.str.find('o')) dtype: object 1 wILLIAM rICK@
print(s.str.findall('a')) Tom_William Rick@ dtype: object
print(s.str.swapcase()) Tom William Rick@ 0 False
0 1 0 1 False
print(s.str.islower()) 1 0 1 dtype: bool
print(s.str.isupper()) 0 False 0 False
print(s.str.isnumeric()) 1 True 1 False
dtype: bool dtype: bool
0 Tom 0 False
1 William Rick$ 1 False
dtype: object dtype: bool
Python Basics
Pandas-Indexing

The Python and NumPy indexing operators "[ ]" and attribute operator "." provide quick and easy access to Pandas data structures
across a wide range of use cases. However, since the type of the data to be accessed isn’t known in advance, directly using
standard operators has some optimization limits. For production code, we recommend that you take advantage of the optimized
pandas data access methods explained in this chapter.
Pandas now supports three types of Multi-axes indexing; the three types are mentioned in the following table −

Sr.No Indexing Description


1 .loc() Label based
2 .iloc() Integer based

.loc()
Pandas provide various methods to have purely label based indexing. When slicing, the start bound is also included. Integers are
valid labels, but they refer to the label and not the position.

.iloc()
Pandas provide various methods in order to get purely integer based indexing. Like python and numpy, these are 0-
based indexing.
Python Basics
Pandas-Indexing
A B C D
a -0.509153 0.208807 0.050562 -0.525807
b -0.234639 0.294372 0.621539 -0.700649
c 1.691475 -1.044881 -0.624499 0.454731
d -1.062153 -0.569152 0.925342 -1.272488
e -0.453962 -0.875664 0.978884 -0.090639

df = f -0.378311 -0.469225 -0.971526 0.766966


g 0.037255 -0.760434 0.345085 0.805445
h -0.247631 0.520440 0.615495 0.276841
pd.DataFrame(np.random. a -0.509153
b -0.234639
c 1.691475
randn(8, 4),index = d -1.062153
e -0.453962
['a','b','c','d','e','f','g','h'], f -0.378311
g 0.037255
h -0.247631
columns = ['A', 'B', 'C', 'D']) Name: A, dtype: float64
A C

print(df) a -0.509153 0.050562


b -0.234639 0.621539
c 1.691475 -0.624499
#select all rows for a d -1.062153 0.925342
e -0.453962 0.978884

specific column f -0.378311 -0.971526


g 0.037255 0.345085
h -0.247631 0.615495
print(df.loc[:,'A']) A B C D
a -0.509153 0.208807 0.050562 -0.525807
b -0.234639 0.294372 0.621539 -0.700649
print(df.loc[:,['A','C']]) c 1.691475 -1.044881 -0.624499 0.454731
d -1.062153 -0.569152 0.925342 -1.272488
print(df.iloc[:4]) C D
b 0.621539 -0.700649
c -0.624499 0.454731
print(df.iloc[1:5, 2:4]) d 0.925342 -1.272488
e 0.978884 -0.090639
A B C D
a -0.509153 0.208807 0.050562 -0.525807
b -0.234639 0.294372 0.621539 -0.700649
c 1.691475 -1.044881 -0.624499 0.454731
d -1.062153 -0.569152 0.925342 -1.272488
Thank you

You might also like