Comprehensive Notes Dataframe functions Class 12 IP

In this article, Comprehensive Notes Dataframe functions Class 12 IP we will discuss some important function given in your CBSE curriculum for Python Pandas Dataframe.

Comprehensive Notes Dataframe functions Class 12 IP

This post covers the topic of how to use rename(), head(), tail(), concat() and merge functions in dataframe? Now we have already covered creating dataframes, add rows and columns in a dataframe, seleect/access dataframe, delete or drop row/column from dataframe and import/export data between dataframe and csv files. So now as per the syllabus we will following topics:

Rename columns using rename() function

You can rename a column, row, and both using rename() function.  You can use both indexes or columns or anyone as a parameter in rename function. Observe the following code illustrating rename row and columns together:
import pandas as pd
def df_operations():
    dt=({'English':[74,79,48,53,68],
         'Physics':[76,78,80,76,73],
         'Chemistry':[57,74,55,89,70],
         'Biology':[76,85,63,68,59],
         'IP':[82,93,69,98,79]})
    df=pd.DataFrame(dt, index=[1201,1202,1203,1204,1205])
    print("Dataframe before rename:")
    print(df)
    print("Dataframe after rename:")
    df=df.rename(columns={'English':'Eng','Physics':'Phy','Chemistry':'Chem','Biology':'Bio'},
    index={1201:'Akshit',1202:'Bhavin',1203:'Chetan'})
    print(df)
df_operations()

Download the pycode

Output:

renaming rows and columns in Pandas dataframe
renaming rows and columns in Pandas dataframe
In the above code, I have created a dataframe with roll number as an index and printed before rename. In the next line, we used rename() function and passed a column list as the first parameter and Old column names with new column names as values as well as indexes with old values and new values. I have renamed the first four columns as short names and rows with names in place of the roll number. If you want to rename columns only specify the column names only. You can provide inplace=True to change the original dataframe so a new dataframe object is not required to assign at the time of rename the column.

Rename columns using columns properties

You can rename columns using columns properties also. But while using this method you have to specify all the column names. Observe this code:
import pandas as pd
def df_operations():
    dt=({'English':[74,79,48,53,68],
         'Physics':[76,78,80,76,73],
         'Chemistry':[57,74,55,89,70],
         'Biology':[76,85,63,68,59],
         'IP':[82,93,69,98,79]})
    df=pd.DataFrame(dt, index=[1201,1202,1203,1204,1205])
    print("Dataframe before rename:")
    print(df)
    print("Dataframe after rename:")
    df.columns=['Eng','Phy','Chem','Bio','Info.Prac']
    print(df)
df_operations()

Download the pycode

Output:
rename columns using columns properties Python Pandas dataframe
rename columns using columns properties Python Pandas dataframe

Rename row/indexes using index properties

It is almost similar like columns properties. Observe the following code:
import pandas as pd
def df_operations():
    dt=({'English':[74,79,48,53,68],
         'Physics':[76,78,80,76,73],
         'Chemistry':[57,74,55,89,70],
         'Biology':[76,85,63,68,59],
         'IP':[82,93,69,98,79]})
    df=pd.DataFrame(dt, index=[1201,1202,1203,1204,1205])
    print("Dataframe before rename:")
    print(df)
    print("Dataframe after rename:")
    df.index=['Aman','Bhavik','Chandu','Dhaval','Eshan']
    print(df)
df_operations()
Output:
rename rows-indexes in python pandas using index properties
rename rows-indexes in python pandas using index properties

head() function

The head() function is used to retrieve top rows from dataframe. Have a look on the following code:
import pandas as pd
def df_operations():
    dt=({'English':[74,79,48,53,68,44,65,67],
         'Physics':[76,78,80,76,73,55,49,60],
         'Chemistry':[57,74,55,89,70,50,60,80],
         'Biology':[76,85,63,68,59,79,49,69],
         'IP':[82,93,69,98,79,88,77,66]})
    df=pd.DataFrame(dt, index=[1201,1202,1203,1204,1205,1206,1207,1208])
    print("All data from Dataframe:")
    print(df)
    print(df.head())
df_operations()
Output:
head function in python pandas dataframe
head function in python pandas dataframe
By default head() function returns top 5 records from dataframe. You can reduce the records by specifying any value assigned to n into bracket followed by head function. I have fetched 4 top rows by using the following code, let’s have a look:
import pandas as pd
def df_operations():
    dt=({'English':[74,79,48,53,68,44,65,67],
         'Physics':[76,78,80,76,73,55,49,60],
         'Chemistry':[57,74,55,89,70,50,60,80],
         'Biology':[76,85,63,68,59,79,49,69],
         'IP':[82,93,69,98,79,88,77,66]})
    df=pd.DataFrame(dt, index=[1201,1202,1203,1204,1205,1206,1207,1208])
    print("All data from Dataframe:")
    print(df)
    print(df.head(n=4))
df_operations()
Output:
head function using n=4 python pandas dataframe
head function using n=4 python pandas dataframe

tail() Function

The tail() function returns bottom rows from dataframe. Observe this code:
import pandas as pd
def df_operations():
    dt=({'English':[74,79,48,53,68,44,65,67],
         'Physics':[76,78,80,76,73,55,49,60],
         'Chemistry':[57,74,55,89,70,50,60,80],
         'Biology':[76,85,63,68,59,79,49,69],
         'IP':[82,93,69,98,79,88,77,66]})
    df=pd.DataFrame(dt, index=[1201,1202,1203,1204,1205,1206,1207,1208])
    print("All data from Dataframe:")
    print(df)
    print(df.tail())
df_operations()
Output:
tail function in pandas
tail function in pandas

concat() function

The concat() function is used to join more than one datframe into one unit. You can combine dataframes having similar structures. Observe this code:
import pandas as pd
def df_operations():
    dt_sc=({'English':[74,79,48,53,68,44,65,67],
         'Physics':[76,78,80,76,73,55,49,60],
         'Chemistry':[57,74,55,89,70,50,60,80],
         'Biology':[76,85,63,68,59,79,49,69],
         'IP':[82,93,69,98,79,88,77,66]})
    xii_1=pd.DataFrame(dt_sc, index=[1201,1202,1203,1204,1205,1206,1207,1208])
    dt_co=({'English':[74,79,48,53,68,44,65,67],
         'Physics':[76,78,80,76,73,55,49,60],
         'Chemistry':[57,74,55,89,70,50,60,80],
         'Biology':[76,85,63,68,59,79,49,69],
         'IP':[82,93,69,98,79,88,77,66]})
    xii_2=pd.DataFrame(dt_co, index=[1209,1210,1211,1212,1213,1214,1215,1216])
    xii=pd.concat([xii_1,xii_2])
    print(xii)
df_operations()
Output:
concate pandas data frame
concate pandas data frame
You can add ignore_index = true to avoid using the same original index of dataframes. Have a look on this:
import pandas as pd
def df_operations():
    dt_sc=({'English':[74,79,48,53,68,44,65,67],
         'Physics':[76,78,80,76,73,55,49,60],
         'Chemistry':[57,74,55,89,70,50,60,80],})
    xii_1=pd.DataFrame(dt_sc)
    dt_co=({'English':[66,65,87,56,86,44,56,76],
         'Physics':[67,87,80,67,77,55,45,80],
         'Chemistry':[75,47,55,98,70,50,60,80],})
    xii_2=pd.DataFrame(dt_co)
    xii=pd.concat([xii_1,xii_2])
    print(xii)
df_operations()
Output:
Comprehensive Notes Dataframe functions Class 12 IP
Now just change the following code to concat:
xii=pd.concat([xii_1,xii_2],ignore_index=True)
You will get this output:
ignore_index = True pd.concat
The concat() function joins the dataframe along with rows. If you want to join dataframes using column you can add axis=1 parameter in the concat() function. Observe this code:
import pandas as pd
def df_operations():
    dt_sc=({'English':[74,79,48,53,68,44,65,67],
         'Physics':[76,78,80,76,73,55,49,60],
         'Chemistry':[57,74,55,89,70,50,60,80],})
    xii_1=pd.DataFrame(dt_sc)
    dt_co=({'English':[66,65,87,56,86,44,56,76],
         'Physics':[67,87,80,67,77,55,45,80],
         'Chemistry':[75,47,55,98,70,50,60,80],})
    xii_2=pd.DataFrame(dt_co)
    xii=pd.concat([xii_1,xii_2], axis=1)
    print(xii)
df_operations()
Output:
concate dataframes along with columns
concate dataframes along with columns

merge() Function

It is used to merge two dataframes that have some common values. You can specify the fields as on parameter in the merge() function. It follows the concept of RDBMS having parent column and child columns in the dataframe. One column should have common data. Have a look at this code:
import pandas as pd
def df_operations():
    p1=({'P_ID':[1,2,3,4,5],
         'First_Name':['Sachin','Saurav','Virendra','Mahendra Sinh','Gautam'],
         'Last_Name':['Tendulker','Ganguly','Sehvag','Dhoni','Gambhir']})
    d1=pd.DataFrame(p1)
    p2=({'P_ID':[1,2,3,4,5],
         'Runs':[18987,12120,11345,10345,12789]})
    d2=pd.DataFrame(p2)
    players=pd.merge(d1,d2)
    print(players)
df_operations()
Output:
merge in pandas dataframe
merge in pandas dataframe

Leave a Reply