Chapter 2 Python Pandas - II
Chapter 2 Python Pandas - II
Python Pandas – II
Introduction – In this chapter we shall talk more about dataframes, basic
operations of dataframe, descriptive statistics, pivoting, handling missing data,
combining/merging etc.
Iterating Over a DataFrame
iterrows() – Iterates over dataframe row-wise where each horizontal subset
is in the form of (row index, Series) where Series contains all column values for
that row-index.
Example 1 : Write a program to print the DataFrame df. One row at a time.
Import pandas as pd
dict = {'Name':['Ram','Mohan','Sachin'],
'Marks':[95,88,89]}
df = pd.DataFrame(dict, index = ['Rno 1','Rno 2','Rno 3'])
print(df)
Ans :
Ans :
Example : Consider the DataFrame (dfmks) given above. Write a program to print
the maximum marks scored in each subject across all sections.
Example : Consider the DataFrame (dfmks) given above. Write a program to print
the maximum marks scored in a section, across all subject.
mode() – returns the mode value (i.e. the value that appears most often in a
given set of values).
lst1 = [99,94,95,94,97]
lst2 = [94,94,89,87,100]
lst3 = [92,92,91,99,99]
lst4 = [99,97,89,94,99]
dict = {'A':lst1,'B':lst2,'C':lst3,'D':lst4}
mksdf = pd.DataFrame(dict)
mksdf.index = ['Acct','Eco','Eng','IP','Math']
print(mksdf)
Homework –
4. What are Binary operations ? Name the function that let you perform
binary operation on a DataFrame.
df1 = pd.DataFrame({'Sub_id':[1,2,3,4,5],
'Fname':['amit','ajay','vikas','vaibhav','jia'],
'Lname':['shukla','tiwari','madan','parmar','jain']})
df2 = pd.DataFrame({'Sub_id':[4,5,6,7,8],
'Fname':['anil','rita','akshay','kapil','ms'],
'Lname':['gupta','sharma','sinha','dev','dhoni']})
df3 = pd.DataFrame({'Sub-id':[1,2,3,4,5,7,8,9,10,11],
'Test-id':[51,15,15,61,16,14,15,1,61,16]})
By default concat() concatenate along the row. To concatenate along the
column we can give argument axis = 1.
1. Inner Join – Take the rows having common indexes from both the
dataframes.
df1 = pd.DataFrame(
{'Name':['Khushboo','Prarthana','Aman','Kamal']},
index = [1,2,3,4])
3. Right join – Take all the rows from the right (second) dataframe and
join with it only those rows from the first dataframe that have common
indexes as dataframe 2.
4. Outer join – Take all rows from both the dataframe and join them.
Joining on a Column : we can provide column name of dataframe 1 with
on argument of join method.
Df1 = pd.DataFrame({'Cust_id':[1,2,3,4,5,6],
'Product':['Oven','AC','AC','Speaker','Tablet','Smartphone']})
Df2 = pd.DataFrame({'P_id':[2,4,6],
'State':['Delhi','Goa','kerala']
})
Now, change the column name from P_id to Cust_id :
Now join :
Where :