In this article you will learn the 3 best ways to delete dataframe rows columns ip 12. If you are class 12 Informatics Practices student and looking for the contents how to delete rows or columns from dataframe you have landed on the correct page. So let’s begin!

3 best ways to delete dataframe rows columns ip 12

The 3 best ways to delete dataframe rows columns ip 12 are as following:

  1. Delete Rows
    1. Using drop function or method
    2. Using .index property
    3. Using != operator
  2. Delete Columns
    1. Using drop
    2. Using Labels
    3. Using Column list

Delete rows

First we will see 3 best ways to delete rows from dataframe for 3 best ways to delete dataframe rows columns ip 12.

Using drop function or method

Please go through this python documentation before using drop() function. 

Python offers drop() function to drop/delete data from dataframe. You can use drop() function in following ways to delete row(s) and columns(s).

Delete rows by index name

You can delete rows by its index name. 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=['Akshit','Bharat','Chetan','Dhaval','Gaurang'])
    df=df.drop('Chetan')
    print(df)
df_operations()
delete dataframe rows columns ip 12 delete row by index
delete dataframe rows columns ip 12 delete row by index

In the above code, Name of students assigned with index while creating a dadtaframe. Now according to the python documentation drop() function deletes the specified columns name by identifying its name. Here axis is not specified so it will take by default axis. The default axis is 0 for rows. The axis parameter is commonly used to delete columns with the value 1. Now observe the following code with the index parameter.

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=['Akshit','Bharat','Chetan','Dhaval','Gaurang'])
    df=df.drop(index="Bharat")
    print(df)
df_operations()
3 best ways to delete dataframe rows columns ip 12
delete dataframe row using index name

Delete rows using multiple indexes

You can delete multiple rows by passing multiple indexes as parameter values. 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=['Akshit','Bharat','Chetan','Dhaval','Gaurang'])
    df=df.drop(['Dhaval','Gaurang'])
    print(df)
df_operations()
3 best ways to delete dataframe rows columns ip 12
delete multiple rows from dataframe

Delete rows using multiple indexe lists along with inplace

Similarly, you can use multiple index lists as parameters values. 

Whenever rows deleted from a dataframe a new datafame is returned as output and the old dataframe remains intact. To avoid this problem you can pass inplace parameter with drop() function. 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=['Akshit','Bharat','Chetan','Dhaval','Gaurang'])
    df.drop(['Dhaval','Gaurang'], inplace=True)
    print(df)
df_operations()

Delete rows using .index property

The next part is you can delete row(s) through the index property also. Just provide the list of indexes to be deleted. 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)
    df=df.drop(df.index[[1,3]])
    print(df)
df_operations()
3 best ways to delete dataframe rows columns ip 12

In the above code, you can see that df.index[[1,3]] is removing the indexed rows. If you want to remove a single row then you can write the index property in the following manners:: 

  1. df.index[4]→ will delete the last row i.e. IP data. 
  2. df.index[-1]→ will delete the last row too.
  3. df[:2]→ will keep the top 2 rows
  4. df[:-2]→ will ignore the last 2 rows and displays rest rows.

Similarly, you can use more slices combination. So try different options and see the magic!!!!

Delete rows using != operator

You can also delete a row by using != relational operator lool in the following code:

import pandas as pd
def df_operations():
    dt=({‘Name’:[‘Suman’,’Gayatri’,’Vishruti’,’Alpa’,’Hetal’],
         ‘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)
    print(df[df.Name!=’Vishruti’])
df_operations()

Download py code

2. Delete Columns

Drop columns using column labels

You can delete a column by the passing column name as a parameter into drop() function. Observe the following code:

import pandas as pd
def df_operations():
    dt=({‘Name’:[‘Suman’,’Gayatri’,’Vishruti’,’Alpa’,’Hetal’],
         ‘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)
    print(df.drop(‘Chemistry’,axis=1))
df_operations()

Download the py code

To delete the column, column name and axis=1 parameter must be used to identify the column. You can delete multiple columns in a similar way by passing the column list as a parameter in drop() function. Just observe this code:

Delete multiple columns

def df_operations():

    dt=({‘Name’:[‘Suman’,’Gayatri’,’Vishruti’,’Alpa’,’Hetal’],
         ‘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)
    print(df.drop([‘English’,’IP’],axis=1))
df_operations()

Download the py code

Delete columns using columns parameter in drop() function

You can delete columns using columns parameter into drop() function.

import pandas as pd
def df_operations():
    dt=({‘Name’:[‘Suman’,’Gayatri’,’Vishruti’,’Alpa’,’Hetal’],
         ‘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)
    print(df.drop(columns=’Biology’))
df_operations()

Download the py code

When you use the columns parameter into a drop() function axis parameter is not required. Now look the code to delete multiple columns using the columns parameter.

import pandas as pd
def df_operations():
    dt=({‘Name’:[‘Suman’,’Gayatri’,’Vishruti’,’Alpa’,’Hetal’],
         ‘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)
    print(df.drop(columns=[‘Biology’,’IP’]))
df_operations()

Download the py code

Delete columns using columns list

You can delete multiple columns with a columns property list. Observe the following code:

import pandas as pd
def df_operations():
    dt=({‘Name’:[‘Suman’,’Gayatri’,’Vishruti’,’Alpa’,’Hetal’],
         ‘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)
         print(df.drop(df.columns[[1,5]],axis=1))
df_operations()

Download the py code

Now observe this code variation:

import pandas as pd
def df_operations():
    dt=({‘Name’:[‘Suman’,’Gayatri’,’Vishruti’,’Alpa’,’Hetal’],
         ‘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)
    print(df.drop(columns=df.columns[[2,3]]))
df_operations()

Download the py code

Predict the output yourself. 

3 Delete rows and columns together

import pandas as pd
def df_operations():
    dt=({‘Name’:[‘Suman’,’Gayatri’,’Vishruti’,’Alpa’,’Hetal’],
         ‘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)
    print(df.drop(index=[0,2,3],columns=[‘English’,’Biology’]))
df_operations()

Download the code

You can use the following ways also to do the same:

print(df.drop(index=df.index[[1, 3, 5]], columns=df.columns[[1, 2]]))

Follow this link to access delete row columns pandas questions.

Important Questions delete row columns IP Class 12

Thank you for reading the post. Comment your experience with us as well as code we provide with this website. Like our post and share with your friends who are interested in learning python.

One thought on “3 best ways to delete dataframe rows columns ip 12”

Leave a Reply