Python Basic and Advanced-Day 8
Python Basic and Advanced-Day 8
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
A - 'python'
B - False
C - Name error
D - True
Python Basics
Quiz
A - 'python'
B - False
C - Name error
D - True
A. <class 'tuple'>
B. <class 'int'>
C. <class 'set'>
D. <class 'complex'>
E. <class 'list'>
Python Basics
Quiz
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
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
#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
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.
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
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 −
.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