02. Python Pandas - 2 2020-21
02. Python Pandas - 2 2020-21
PYTHON PANDAS
DataFrame Data Structure – II
Syntax:
Dataframe_object.iterrows( )
row_index
Name Marks Grade City
1
ii. iteritems( ) method: This method accesses the elements of a DataFrame
vertically i.e. column wise. Each vertical subset is in the form (col_index,
Series) where Series contains the values in the column for the col_index.
The syntax of iteritems( ) method is as follows:
Syntax:
Dataframe_object.iteritems( )
col_index
Name Marks Grade City
2
BINARY OPERATIONS / CALCULATIONS ON DATAFRAME
In binary operations, the data from 2 DataFrames are aligned on the basis of their row
and column indexes. For matching row and column index, the given operation is performed
and for non-matching row and column index NaN value is stored in the result. The different
operations which can be performed on DataFrame are:
3
Sample Program 1:
Create two DataFrames term1 and term2 to store marks of students in three
subjects for Term-I and Term-II exams. DataFrame term1 contains marks of 4
students in 3 subjects and DataFrame term2 contains marks of 5 students in 3
subjects. Add marks of Term-I and Term-II and store in third DataFrame total and
display the output.
4
Sample Program 2:
Create two DataFrames term1 and term2 to store marks of students in three
subjects (Phy, Chem, Maths) for Term-I and Term-II exams. DataFrame term1
contains marks of 4 students in 3 subjects (Phy, Chem, Comp) and DataFrame term2
contains marks of 4 students in 3 subjects. Find difference in marks of Term-I and
Term-II and store in third DataFrame diff and display the output.
5
Sample Program 3:
Create two DataFrames term1 and term2 to store marks of students in three
subjects (Phy, Chem, Maths) for Term-I and Term-II exams. DataFrame term1
contains marks of 4 students in 3 subjects (Phy, Chem, Comp) and DataFrame term2
contains marks of 5 students in 3 subjects. Find multiplication of marks of Term-I and
Term-II and store in third DataFrame product and display the output.
6
DESCRIPTIVE STATISTICS WITH PANDAS
Python Pandas is widely used data science library and it offers many useful
functions. Among many other functions, Pandas also offer many useful Statistical and
Aggregate functions. Some of these functions are as follows:
1. min( ) function: The min( ) function finds out the minimum value from a given
DataFrame.
Syntax:
DataFrame.min( axis = None, skipna = None, numeric_only = None)
Parameters:
7
2. max( ) function: The max( ) function finds out the maximum value from a given
DataFrame.
Syntax:
DataFrame.max( axis = None, skipna = None, numeric_only = None)
Parameters:
8
3. mode( ) function: The mode( ) function returns the mode value (i.e. the value that
appears most number of times) from a set of values.
Syntax:
Parameters:
9
4. mean( ) function: The mean( ) function returns the computed mean(average) from a
set of values.
Syntax:
Parameters:
10
5. median( ) function: This function returns the middle number from a set of numbers.
Syntax:
Parameters:
Note: To find the Median first the values are arranged in some order (ascending or
descending) and then middle value is marked. If the number of values are even then
median is calculated as average of two middle values.
11
6. count( ) function: This function counts the non-NaN values for each row or column.
Syntax:
Parameters:
12
7. sum( ) function: This function returns the sum of the values for the requested axis.
Syntax:
Parameters:
13
OTHER FUNCTIONS:
8. info( ) function: The info( ) function is used to get the basic information about the
DataFrame. This function gives information about its type, index values, number of
rows, data columns, num of values in columns, data type of each column and memory
usage.
Syntax:
DataFrame.info( )
14
9. head( ) function: The head( ) function is used to fetch the specified number of rows
from the top of the DataFrame. The syntax is as follows:
Syntax:
Here n is the number of rows to be displayed from the top, if n is not specified then it
displays top 5 rows from the DataFrame.
15
10. tail( ) function: The tail( ) function is used to fetch the specified number of rows
from the bottom of the Dataframe: The syntax is as follows:
Syntax:
Here n is the number of rows to be displayed from the bottom, if n is not specified
then it displays bottom 5 rows from the DataFrame.
16
APPLYING FUNCTIONS ON A SUBSET OF DATAFRAME:
Sometimes, you need to apply a function on a selective column or a row or a
subset of the DataFrame. For doing this the concept of accessing a single row, single
column or a subset is used.
Syntax:
DataFrame[Column_name].functionname
df1[“Phy”].min( )
17
ii. Applying Functions on Multiple Columns of a DataFrame:
Syntax:
df1[[“Phy” , “Maths”]].count( )
18
iii. Applying Functions on a Single Row of a DataFrame:
Syntax:
19
iv. Applying Functions on Multiple Rows of a DataFrame:
Syntax:
Example: Program to find max marks for Roll number 11253 to 11256.
20
v. Applying Functions on a Subset of a DataFrame:
Syntax:
Example: Program to find max marks from Roll number 11252 to 11257 in
the subjects of Chem and Maths.
---- x ----
21