Update Dataframe in Pandas with Examples

How to Update Dataframe in Pandas with Examples

Popular Domains for just 99 Cents at Namecheap!

Sometimes you want to change or update the column data in the pandas dataframe. Then there is a function in pandas that allows you to update the records of the column. The function is pandas.DataFrame.update(). It easily updates the columns or the column records. In the entire tutorial, you will know how to update dataframe in pandas with examples.

Examples on how to update dataframe in Pandas

In this section, you will know how to apply pandas.DataFrame.update() function on dataframe to update the records.

Example 1: Updating an Entire Column

In this example, I will update the entire column of a dafarame with the other dataframe. You have to use the dot operator on the existing dataframe with the second dataframe as the argument inside the update() method. Run the below lines of code and see the output.

import pandas as pd
df1 = pd.DataFrame({'col1': ['a', 'b', 'c'],
'col2': [10,20,30]})
print("Original Dataframe\n",df1)
print("###########################")
df2 = pd.DataFrame({'col2':[1,2,3]})
df1.update(df2)
print("Changed Dataframe \n",df1)

Output

Update the entire column of the dataframe
Update the entire column of the dataframe

Example 2: Updating Dataframe with different data length

Now, let’s take a case when I have to update dataframe that has a record length greater than the original dataframe. If I will apply the update() method then records will be updated till the length equals the original dataframe. Execute the below lines of code and see the output.

import pandas as pd
df1 = pd.DataFrame({'col1': ['a', 'b', 'c'],
'col2': [10,20,30]})
print("Original Dataframe\n",df1)
print("###########################")
df2 = pd.DataFrame({'col2':[1,2,3,4,5,6,7]})
df1.update(df2)
print("Changed Dataframe \n",df1)

Output

Update the column of the dataframe with different length
Update the column of the dataframe with different length

You can see this in the above output. I have 3 records in the df1 for the col2 and df2 has seven records. But when I applied df1.update(df2) the first three rows of the df1 are only updated.

Example 3: Update Dataframe at a specific Location

In this example, I will update the values of a particular specific location. To do so you have to create a dataframe with the index argument and then apply the update method.

Execute the below lines of code and see the output.

import pandas as pd
df1 = pd.DataFrame({'col1': ['a', 'b', 'c'],
'col2': [10,20,30]})
print("Original Dataframe\n",df1)
print("###########################")
df2 = pd.DataFrame({'col2':[1,2]},index=[0,2])
df1.update(df2)
print("Changed Dataframe \n",df1)

Output

Update the column of the dataframe at specific location
Update the column of the dataframe at a specific location

In the above output, you can see only the zero and third rows are only updated. The second row’s value is the same.

Conclusion

Pandas Dataframe update is one of the best functions provided by pandas packages. It saves you time to make a new dataset every time some values changes in the existing dataset. These are examples of how to update dataframe in Pandas. I hope you have liked this tutorial and it has solved your queries on updating dataframe. Even if you have any doubt then you can contact us for more help.

Source:

Pandas Documentation

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner