To add, delete and update data in a 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::
import pandas as pdinfo= {"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)
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 '
dataFrame.loc[len(dataFrame.index)] = [18,'Amit']
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:
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')
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)
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:
dataframe.at[index,'column-name']='new value'
df.at['Row1',,'Name']='Siya Agarwal'
df.at['Row2', 'Name']='Nilesh Pandey'
print(df)
Just click the next button to see which element goes to which position.
Name of Columns | |||
Row Index | 'One' | 'Two' | 'Three' |
0 | 1 | 2 | 3 |
1 | 4 | 5 | 6 |
2 | 7 | 8 | 9 |
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
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 |
click on delete to delete second row with the help of above dataframe delete code
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 |
chick on update to replace value 2 with 3 with the help of above dataframe replace code
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)
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.
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