Let’s say we have the following DataFrame:
A B C 0 1 4 7 1 2 5 8 2 3 6 9
The DataFrame has three rows and three columns. And the columns are labeled A, B, and C.
Now, we want to update the column labeled B. In Python pandas, we can update a DataFrame using the DataFrame.update() function. Let’s see an example first.
import pandas df1 = pandas.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}) df2 = pandas.DataFrame({'B': [10, 11, 12]}) print("df1 before update: \n", df1) print("df2: \n", df2) df1.update(df2) print("df1 after update: \n", df1)
Here, df1 and df2 are two DataFrames. df2 contains a column that has the same label as that of df1. So, if we call df1.update(df2), column B of df1 will get updated with the values of column B of df2.
So, the output of the above program will be like the following:






0 Comments