V-Lab @ ANDC

DATA FRAMES

Aim

To add, delete and update data in a DataFrame

Theory


What is Adding Rows in DataFrame

The addition of content in the data frame is referred to as adding rows in a dataframe . There are many ways to add rows in a data frame out of which we detail three different methods below::

Sample DataFrame:

import pandas as pd
info= {"Num":[12,14,13,12,14,13,15],"NAME":['John','Camili','Rheana','Joseph','Amanti','Alexa','Siri']}
data = pd.DataFrame(info)
print("Original Data frame:\n")
print(data)

Method to add data to data frame

Using Python loc() method to add a single row in the dataframe.

Python loc() method enables us to add one row at the specified location of the data frame at a time. In order to add a row at the end of a data frame, len(dataFrame.index) is used which gives the total number of rows in the data frame.
Syntax:

                                
                                    dataFrame.loc[len(dataFramef.index)]=' values '
                                
                            
Example:
dataFrame.loc[len(dataFrame.index)] = [18,'Amit']

What is deleting row in dataframe

The deleting of existing data in the data frame is referred to as deleting. Out of multiple ways for deleting the content of a data frame, we detail one methods for "Deleting the DataFrame" below:

Method to add data to data frame

Using python drop method to delete a column or row

The default way to use “drop” to remove columns is to provide the column names to be deleted along with specifying the “axis” parameter to be 1.

Syntax :
 DataFrame.drop(labels=None, axis=0, index=None, columns=None,level=None, inplace=False,errors='raise')
Example :

    data = data.drop(columns="anyName")  
    data = data.drop(columns=["anyName","anyOtherName"])
    data = data.drop(columns=data.columns[3])
    data=data.drop(labels=0, axis=0)
    data = data.drop(labels=[1,15,20], axis=0)
                             

What is updating single element of row in dataframe

The modification of existing data in the data frame is referred to as updating. Out of multiple ways for updating the content of a data frame, we detail four different methods forn "Updating the DataFrame" below:

Method to update data to data frame

Using Python at() method to update a single element of a row

Python at() method enables us to update the value of one element in a data frame at a time .
Syntax:
dataframe.at[index,'column-name']='new value' 
                          
Example:
df.at['Row1',,'Name']='Siya Agarwal' 
df.at['Row2', 'Name']='Nilesh Pandey' print(df)

Procedure

  1. The link for the colab has been provided below the practice section.
  2. To get access to the colab file . Click on the Google Colab Demo codes.
  3. Once You have opened the colab . You can see demo codes provided to you for practice.The codes in the colab file include all the methods taught to you in the theory section that can be used to add or delete in dataframe.
  4. You can work on those codes i.e update or delete accordingly for practice..
  5. You can also type your own code in the colab .
  6. It will be helpful if you run the above codes provided in theory to check their outputs.

Practice

Instructions:

Just click the next button to see which element goes to which position.

ADD A DATA IN A DATAFRAME

On runing: pandas.DataFrame([[1,2,3],[4,5,6],[7,8,9]], [0,1,2],['One','Two','Three'] )

[

[

1

,

2

,

3

]

,

[

4

,

5

,

6

]

,

[

7

,

8

,

9

]

]

Name of Columns
Row Index 'One' 'Two' 'Three'
0 1 2 3
1 4 5 6
2 7 8 9

Observation

The start of row 0

The element 1 of sub-list 1 is assigned to [ 0 , 0 ] in the DataFrame

The element 2 of sub-list 1 is assigned to [ 0 , 1 ] in the DataFrame

The element 3 of sub-list 2 is assigned to [ 0 , 2 ] in the DataFrame

The end of row 0

The start of row 1

The element 4 of sub-list 2 is assigned to [ 1 , 0 ] in the DataFrame

The element 5 of sub-list 2 is assigned to [ 1 , 1 ] in the DataFrame

The element 6 of sub-list 3 is assigned to [ 1 , 2 ] in the DataFrame

The end of row 1

The start of row 2

The element 7 of sub-list 3 is assigned to [ 2 , 0 ] in the DataFrame

The element 8 of sub-list 3 is assigned to [ 2 , 1 ] in the DataFrame

The element 9 of sub-list 4 is assigned to [ 2 , 2 ] in the DataFrame

The end of row 2

The end of row 3

DELETE A DATA IN A DATAFRAME

Python drop() method to delete rows/columns in a DataFrame.

Syntax:

df = pandas.DataFrame([[1,2,3],[4,5,6],[7,8,9]], [0,1,2],['One','Two','Three'] )

df.drop( 1, axis =0 , inplace = True)

Name of Columns
Row Index 'One' 'Two' 'Three'
0 1 2 3
1 4 5 6
2 7 8 9

Observation

click on delete to delete second row with the help of above dataframe delete code

UPDATE A DATA IN A DATAFRAME

Python replace() method to update values in a DataFrame.

Syntax:

df = pandas.DataFrame([[1,2,3],[4,5,6],[7,8,9]], [0,1,2],['One','Two','Three'] )

df.replace(to_replace =2,value =3)

Name of Columns
Row Index 'One' 'Two' 'Three'
0 1 2 3
1 4 5 6
2 7 8 9

Observation

chick on update to replace value 2 with 3 with the help of above dataframe replace code

Quiz


Q1. In given code dataframe 'D1' has ___ rows and ____ columns.
import pandas as pd 
ObjList = [{'a':10, 'b':20}, {'a':5, 'b':10,'c':20},{'a':7, 'd':10, 'e':20}]
D1 = pd.DataFrame(ObjList)

  • 3,3
  • 3,5
  • None of the above

Q2. Which method is used to add a new row to a DataFrame

  • rloc[]
  • iloc[]
  • loc[]

Q3. Which method is used to delete row or column in DataFrame?

  • drop()
  • delete()
  • loc[]

Q4. To delete a row, the parameter axis of function drop( ) is assigned the value ______

  • 2
  • 1
  • 3

Q5. We can use the __ method to merge two DataFrames

  1. join()
  2. drop()
  3. append()

Result

Hence we can add, delete or update data in a DataFrame by any of these methods. Data represented in table is more preferred over linear arrays.

Team

Mr. Pankaj Sahu, B.Sc Physical Sciences with Computer Science, II year,
Ms. Sakshi Garg, B.Sc Physical Sciences with Computer Science, II year.

Mentors:
Prof. Sharanjit Kaur,
Ms. Gunjan Rani

Text Copied