This tutorial will show you how to modify, add, delete values in a dataframe in Python with examples.
How to Modify a Single Value in Dataframe
Let’s first create a dataframe from on a dictionary and then print it out.
import pandas as pd
cars = {'Brand': ['Tesla', 'Ford'], 'Count': [3, 4]}
df = pd.DataFrame(data=cars)
print(df)
Brand Count 0 Tesla 3 1 Ford 4
We can then change a value within the dataframe. For instance, we can change Ford with Toyota.
df.Brand[1]='Toyota'
print(df)
0 Tesla 3 1 Toyota 4
You can also modify a value using loc[ ]
and iloc[ ]
to modify a value.
import pandas as pd
cars = {'Brand': ['Tesla', 'Ford'], 'Count': [3, 4]}
df=pd.DataFrame(data=cars)
print("Before the modification:")
print(df)
df.loc[1,'Brand']='Toyota'
print("After the modification:")
print(df)
Before the modification: Brand Count 0 Tesla 3 1 Ford 4 After the modification: Brand Count 0 Tesla 3 1 Toyota 4
How to Add a Column in Dataframe
The following code shows 3 different methods to add a column into a data frame.
import pandas as pd
cars = {'Brand': ['Tesla', 'Ford'], 'Count': [3, 4]}
df=pd.DataFrame(data=cars)
print("Before the modification:")
print(df)
print('\n')
print("Modification (Method 1):")
df['Model']=['Model 3','Escape']
print(df)
print('\n')
print("Modification (Method 2):")
df.loc[:,'Model']=['Model Y','Escape']
print(df)
print('\n')
print('Modification (Method 3):')
df=df.assign(Model=['Cybertruck','Escape'])
print(df)
print('\n')
Before the modification: Brand Count 0 Tesla 3 1 Ford 4 Modification (Method 1): Brand Count Model 0 Tesla 3 Model 3 1 Ford 4 Escape Modification (Method 2): Brand Count Model 0 Tesla 3 Model Y 1 Ford 4 Escape Modification (Method 3): Brand Count Model 0 Tesla 3 Cybertruck 1 Ford 4 Escape
How to Add a Row in Dataframe
The following code shows how you can add a row into a dataframe in Python.
import pandas as pd
cars = {'Brand': ['Tesla', 'Ford'], 'Count': [3, 4]}
df=pd.DataFrame(data=cars)
print("Before the modification:")
print(df)
print('\n')
print("After the modification:")
df.loc[3,:]=['Toyota','8']
print(df)
Before the modification: Brand Count 0 Tesla 3 1 Ford 4 After the modification: Brand Count 0 Tesla 3.0 1 Ford 4.0 3 Toyota 8
How to Delete Columns in Dataframe
We can use del
to delete a column in Dataframe in Python.
import pandas as pd
cars = {'Brand': ['Tesla', 'Ford'], 'Count': [3, 4],'Model':['Model 3','Escape']}
df=pd.DataFrame(data=cars)
print("Before the deletion:")
print(df)
print('\n')
print("After the deletion:")
del df['Model']
print(df)
Before the deletion: Brand Count Model 0 Tesla 3 Model 3 1 Ford 4 Escape After the deletion: Brand Count 0 Tesla 3 1 Ford 4
We can also use drop
to delete not only one column, but multiple columns. There are parameters of axis
and inplace
. When axis=1
, it refers to columns; when axis=0
, it refers to rows.
Inplace is a Boolean parameter which is by default False. When default (i.e., inplace = False
), it does not change the dataframe itself. In contrast, if inplace = True
, it performs the operation and returns nothing; but at the same time, it changes the dataframe.
import pandas as pd
cars = {'Brand': ['Tesla', 'Ford'], 'Count': [3, 4],'Model':['Model 3','Escape']}
df=pd.DataFrame(data=cars)
print("Before the deletion:")
print(df)
print('\n')
print("When inplace=False:")
print(df.drop(['Count','Model'],axis=1,inplace=False))
print(df)
print('\n')
print("When inplace=True:")
print(df.drop(['Count','Model'],axis=1,inplace=True))
print(df)
The following is the output. Note that, “print(df.drop(['Count','Model'],axis=1,inplace=True))
” return “none
.”
Before the deletion: Brand Count Model 0 Tesla 3 Model 3 1 Ford 4 Escape When inplace=False: Brand 0 Tesla 1 Ford Brand Count Model 0 Tesla 3 Model 3 1 Ford 4 Escape When inplace=True: None Brand 0 Tesla 1 Ford
How to Delete Rows in Dataframe
We also set axis = 0
to delete one or multiple rows.
import pandas as pd
cars = {'Brand': ['Tesla', 'Ford'], 'Count': [3, 4],'Model':['Model 3','Escape']}
df=pd.DataFrame(data=cars)
print("Before the deletion:")
print(df)
print('\n')
print("When inplace=False:")
print(df.drop([0],axis=0,inplace=False))
print(df)
print('\n')
print("When inplace=True:")
print(df.drop([0],axis=0,inplace=True))
print(df)
Before the deletion: Brand Count Model 0 Tesla 3 Model 3 1 Ford 4 Escape When inplace=False: Brand Count Model 1 Ford 4 Escape Brand Count Model 0 Tesla 3 Model 3 1 Ford 4 Escape When inplace=True: None Brand Count Model 1 Ford 4 Escape