Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2026 Crash Course
NEET 2026 Crash Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
302 views
in Information Technology by (51.9k points)
closed by

Operations on rows and columns in DataFrames.

1 Answer

+1 vote
by (49.6k points)
selected by
 
Best answer

We can perform some basic operations on rows and columns of a DataFrame like selection, deletion, addition, and renaming, as discussed in this section.

(A) Adding a New Column to a DataFrame

We can easily add a new column to a DataFrame. Let us consider the DataFrame ResultDF defined earlier. In order to add a new column for another student ‘Preeti’, 

we can write the following statement:

>>> ResultDF['Preeti']=[89,78,76] 

>>> ResultDF

Assigning values to a new column label that does not exist will create a new column at the end. 

If the column already exists in the DataFrame then the assignment statement will update the values of the already existing column, for example:

>>> ResultDF['Ramit']=[99, 98, 78]

>>> ResultDF

(B) Adding a New Row to a DataFrame

We can add a new row to a DataFrame using the DataFrame.loc[ ] method. Consider the DataFrame ResultDF that has three rows for the three subjects – Maths, Science and Hindi. Suppose, we need to add the marks for English subject in ResultDF, 

we can use the following statement:

We cannot use this method to add a row of data with already existing (duplicate) index value (label). 

In such case, a row with this index label will be updated, for example:

>>> ResultDF.loc['English'] = [95, 86, 95, 80, 95,99] 

>>> ResultDF

DataFRame.loc[] method can also be used to change the data values of a row to a particular value. For example, the following statement sets marks in 'Maths' for all columns to 0:

>>> ResultDF.loc['Maths']=0 

>>> ResultDF

If we try to add a row with lesser values than the number of columns in the DataFrame, it results in a ValueError, with the error message: ValueError: Cannot set a row with mismatched columns. 

Similarly, if we try to add a column with lesser values than the number of rows in the DataFrame, it results in a ValueError, with the error message: ValueError: Length of values does not match length of index. 

Further, we can set all values of a DataFrame to a particular value, for example:

>>> ResultDF[: ] = 0 # Set all values in ResultDF to 0 

>>> ResultDF

(C) Deleting Rows or Columns from a DataFrame 

We can use the DataFrame.drop() method to delete rows and columns from a DataFrame. We need to specify the names of the labels to be dropped and the axis from which they need to be dropped. To delete a row, the parameter axis is assigned the value 0 and for deleting a column,the parameter axis is assigned the value 1. 

Consider the following DataFrame:

The following example shows how to delete the row with label 'Science':

>>> ResultDF = ResultDF.drop('Science', axis=0) 

>>> ResultDF

The following example shows how to delete the columns having labels 'Samridhi', 'Ramit' and 'Riya':

If the DataFrame has more than one row with the same label, the DataFrame.drop() method will delete all the matching rows from it. 

For example, consider the following DataFrame:

>>> ResultDF

To remove the duplicate rows labelled ‘Hindi’, we need to write the following statement:

>>> ResultDF= ResultDF.drop('Hindi', axis=0) 

>>> ResultDF

(D) Renaming Row Labels of a DataFrame

We can change the labels of rows and columns in a DataFrame using the DataFrame.rename() method. Consider the following DataFrame. 

To rename the row indices Maths to sub1, Science to sub2, Hindi to sub3 and English to sub4 we can write the following statement:

>>> ResultDF

>>> ResultDF=ResultDF.rename({'Maths':'Sub1',

‘Science':'Sub2','English':'Sub3', 

'Hindi':'Sub4'}, axis='index') 

>>> print(ResultDF)

(E) Renaming Column Labels of a DataFrame

To alter the column names of ResultDF we can again use the rename() method, as shown below. 

The parameter axis 'columns' implies we want to change the column labels:

>>> ResultDF=ResultDF.rename({'Arnab':'Student1','Ramit':'Student2','

Samridhi':'Student3','Mallika':'Student4'},axis='columns') 

>>> print(RsultDF)

Note that the column Riya remains unchanged since we did not pass any new label.

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...